λ°μν
π€λ¬Έμ ν΄κ²°
-
D3 | DP (LCS)
π¨ μμ SWEAμ λμ΄λλ λ―Ώμκ² λͺ»λλ€. μ΄κ² κ²¨μ° D3λΌλ...
π¨ [python] λ°±μ€ - 9251. LCS (μ°Έκ³ )
π¨ μ§λλ²μ λ°±μ€μμ νλ² νμκ³ , μ½λ©ν μ€νΈν λ νλ² νμκ³ , μ΄λ²μ λ€μ νΈλκΉ μ΄λμ λ μ΄ν΄κ° λκ±° κ°λ€.
π»μμ€ μ½λ
# μ
λ ₯
T = int(input())
Ns = [input() for _ in range(T)]
# νμ΄ - LCS
results = []
for tc in range(T):
A, B = Ns[tc].split()
dp = [[0] * (len(A) + 1) for _ in range(len(B) + 1)]
for i in range(1, len(dp)):
for j in range(1, len(dp[i])):
# κ°μ λ
if B[i - 1] == A[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
# λ€λ₯Ό λ
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
results.append((dp[-1][-1]))
# μΆλ ₯
for tc in range(T):
print(f'#{tc + 1} {results[tc]}')
πλ¬Έμ νμΈ
μΆμ²: SW Expert Academy
λ°μν
'Algorithm Problem > Python' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[python] SWEA - 3376. νλλ° μμ΄ (0) | 2020.12.20 |
---|---|
[python] SWEA - 3307. μ΅μ₯ μ¦κ° λΆλΆ μμ΄ (0) | 2020.12.19 |
[python] SWEA - 3282. 0/1 Knapsack (0) | 2020.12.17 |
[python] SWEA - 3975. μΉλ₯ λΉκ΅νκΈ° (0) | 2020.12.16 |
[python] SWEA - 4371. νꡬμ λ€μ΄μ€λ λ°° (0) | 2020.12.15 |