baekjoon 20251229
This commit is contained in:
parent
4a77cd5636
commit
60f343066c
38
code_study/Baekjoon/python/1516.py
Normal file
38
code_study/Baekjoon/python/1516.py
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import sys
|
||||||
|
from collections import deque
|
||||||
|
input = sys.stdin.readline
|
||||||
|
INF = float("inf")
|
||||||
|
|
||||||
|
N = int(input())
|
||||||
|
|
||||||
|
indegree = [0]*(N+1)
|
||||||
|
build_info = [[0] for _ in range(N+1)]
|
||||||
|
|
||||||
|
for i in range(1,N+1) :
|
||||||
|
line = list(map(int, input().split()))
|
||||||
|
build_info[i][0] = line[0]
|
||||||
|
for n in line[1:] :
|
||||||
|
if n == -1 :
|
||||||
|
break
|
||||||
|
build_info[n].append(i)
|
||||||
|
indegree[i] += 1
|
||||||
|
|
||||||
|
dp = [0]*(N+1)
|
||||||
|
qu = deque()
|
||||||
|
|
||||||
|
for i in range(1,N+1) :
|
||||||
|
if indegree[i] == 0 :
|
||||||
|
qu.append(i)
|
||||||
|
dp[i] = build_info[i][0]
|
||||||
|
|
||||||
|
while qu :
|
||||||
|
now = qu.popleft()
|
||||||
|
|
||||||
|
for nxt in build_info[now][1:] :
|
||||||
|
indegree[nxt] -= 1
|
||||||
|
dp[nxt] = max(dp[nxt], dp[now] + build_info[nxt][0])
|
||||||
|
|
||||||
|
if indegree[nxt] == 0 :
|
||||||
|
qu.append(nxt)
|
||||||
|
|
||||||
|
print("\n".join(list(map(str, dp[1:]))))
|
||||||
27
code_study/Baekjoon/python/9252.py
Normal file
27
code_study/Baekjoon/python/9252.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
s1, s2 = input(), input()
|
||||||
|
l1, l2 = len(s1), len(s2)
|
||||||
|
dp = [[0]*(l2+1) for _ in range(l1+1)]
|
||||||
|
|
||||||
|
for i in range(l1) :
|
||||||
|
for j in range(l2) :
|
||||||
|
if s1[i] == s2[j] :
|
||||||
|
dp[i+1][j+1] = dp[i][j] + 1
|
||||||
|
else :
|
||||||
|
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1])
|
||||||
|
|
||||||
|
print(dp[l1][l2])
|
||||||
|
|
||||||
|
if dp[l1][l2] != 0 :
|
||||||
|
ans = []
|
||||||
|
p1, p2 = l1, l2
|
||||||
|
|
||||||
|
while p1 > 0 and p2 > 0 :
|
||||||
|
if dp[p1][p2-1] == dp[p1][p2] :
|
||||||
|
p2 -= 1
|
||||||
|
elif dp[p1-1][p2] == dp[p1][p2] :
|
||||||
|
p1 -= 1
|
||||||
|
else :
|
||||||
|
ans.append(s2[p2-1])
|
||||||
|
p1, p2 = p1-1, p2-1
|
||||||
|
|
||||||
|
print("".join(ans[::-1]))
|
||||||
Loading…
x
Reference in New Issue
Block a user