File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ https://www.acmicpc.net/problem/5582
3+ # 유형 : dp , LCS(longest common sequence)
4+ LCSubstring 특징
5+ 1. 무조건 "연속된" 공통 문자열 -> 한번이라도 다르면 dp =0로 초기화,
6+ LCS(subsequence)
7+ - 부분수열은 연속된 값이 아님 -> 최대 공통부분수열은 계속 유지됨
8+ 조건
9+ - 한글자씩 비교함
10+ (1) x[j] == y[i] 같으면 -> dp[i][j] = max(대각선 +1 , 위) => dp
11+ (2) 다르면 ->0 ()
12+
13+ """
14+ import sys
15+ x = list (sys .stdin .readline ())[:- 1 ]
16+ y = list (sys .stdin .readline ())[:- 1 ]
17+ dp = [[0 for _ in range (len (y ))] for j in range (len (x ))]
18+
19+ #2. Dp , LCS(longest-common-sequence)
20+
21+ for i in range (len (x )) :
22+ for j in range (len (y )) :
23+
24+ if i == 0 or j == 0 :
25+ if x [i ] == y [j ] : # 예외 처리
26+ dp [i ][j ] = 1
27+ continue
28+ if x [i ] == y [j ] :
29+ dp [i ][j ] = dp [i - 1 ][j - 1 ] + 1
30+ else :
31+ dp [i ][j ] = 0 #dp[i-1][j]
32+
33+ #3. answer이 꼭 뒤로 오지 않음
34+ answer = 0
35+ for i in range (len (x )):
36+ print (dp [i ])
37+ answer = max (max (dp [i ]),answer )
38+
39+ print (answer )
You can’t perform that action at this time.
0 commit comments