We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
소스 코드
dp[i][j]를 다음과 같이 정의한다.
dp[i][j]
str1의 i번째 까지의 부분 문자열과 str2의 i번째 까지의 부분 문자열의 LCS(길이)
두 문자열 ACAYKP, CAPCAK에 대해서 살펴보자
ACAYKP
CAPCAK
ACAY와 CAPCA ACAY와 CAPCA의 LCS는 ACA이고, ACAYK와 CAPC의 LCS는 AC이다. 이때 A, K로 다른 문자이므로, 두 경우 중 더 길이가 긴 것이 ACAYK CAPCA의 LCS라고 할 수 있다.
ACAY
CAPCA
ACA
ACAYK
CAPC
AC
A
K
ACAYK와 CAPCAK ACAY와 CAPCA의 LCS에 K를 추가하는 경우 이므로 ACAY, CAPCA의 LCS에 K가 추가 된 문자열이 ACAYK와 CAPCAK의 LCS이다.
따라서 LCS의 길이는 다음과 같이 정리할 수 있다. ( string은 0-based, dp 배열은 1-based)
str1[i-1] == str2[j-1]
dp[i][j] = dp[i-1][j-1] + 1
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
i = 1 일 때 (string idx = 0)
반복
for(int i = 1; i<=l1; ++i) { for(int j = 1; j<=l2; ++j) { if(str1[i-1] == str2[j-1]) { dp[i][j] = dp[i-1][j-1] + 1; }else { dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } }
The text was updated successfully, but these errors were encountered:
minsoo0715
No branches or pull requests
9251: LCS
소스 코드
아이디어
dp[i][j]
를 다음과 같이 정의한다.두 문자열
ACAYKP
,CAPCAK
에 대해서 살펴보자ACAY
와CAPCA
ACAY
와CAPCA
의 LCS는ACA
이고,ACAYK
와CAPC
의 LCS는AC
이다.이때
A
,K
로 다른 문자이므로, 두 경우 중 더 길이가 긴 것이ACAYK
CAPCA
의 LCS라고 할 수 있다.ACAYK
와CAPCAK
ACAY
와CAPCA
의 LCS에 K를 추가하는 경우 이므로ACAY
,CAPCA
의 LCS에 K가 추가 된 문자열이ACAYK
와CAPCAK
의 LCS이다.따라서 LCS의 길이는 다음과 같이 정리할 수 있다. ( string은 0-based, dp 배열은 1-based)
str1[i-1] == str2[j-1]
일때dp[i][j] = dp[i-1][j-1] + 1
dp[i][j] = max(dp[i-1][j], dp[i][j-1])
구현
The text was updated successfully, but these errors were encountered: