[Gold IV] Title: LCS 2, Time: 492 ms, Memory: 57556 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-03-05 11:24:09 +09:00
parent ee7d17fbc2
commit a9581d91fc
2 changed files with 75 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
A = list(map(str, input().rstrip()))
B = list(map(str, input().rstrip()))
len_A = len(A)
len_B = len(B)
arr = [[0 for i in range(len_B+1)] for j in range(len_A+1)]
for i in range(1, len_A+1):
for j in range(1, len_B+1):
if(A[i-1] == B[j-1]):
arr[i][j] = arr[i-1][j-1] + 1
else:
arr[i][j] = max(arr[i-1][j], arr[i][j-1])
print(arr[len_A][len_B])
result = []
def getText(r, c):
if r==0 or c==0:
return
if A[r-1] == B[c-1]:
result.append(A[r-1])
getText(r-1, c-1)
else:
if arr[r-1][c] > arr[r][c-1]:
getText(r-1,c)
else:
getText(r, c-1)
getText(len_A, len_B)
result_reverse = []
for i in range(len(result)):
result_reverse.append(result.pop())
print(('').join(result_reverse))

View File

@@ -0,0 +1,32 @@
# [Gold IV] LCS 2 - 9252
[문제 링크](https://www.acmicpc.net/problem/9252)
### 성능 요약
메모리: 57556 KB, 시간: 492 ms
### 분류
다이나믹 프로그래밍, 문자열
### 제출 일자
2025년 3월 5일 11:23:42
### 문제 설명
<p>LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, 모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다.</p>
<p>예를 들어, ACAYKP와 CAPCAK의 LCS는 ACAK가 된다.</p>
### 입력
<p>첫째 줄과 둘째 줄에 두 문자열이 주어진다. 문자열은 알파벳 대문자로만 이루어져 있으며, 최대 1000글자로 이루어져 있다.</p>
### 출력
<p>첫째 줄에 입력으로 주어진 두 문자열의 LCS의 길이를, 둘째 줄에 LCS를 출력한다.</p>
<p>LCS가 여러 가지인 경우에는 아무거나 출력하고, LCS의 길이가 0인 경우에는 둘째 줄을 출력하지 않는다.</p>