Recent Posts
Notice
No Rules Rules
LCS 2 (feat. 백준, 9252번) 본문
728x90
반응형
LCS 2
https://www.acmicpc.net/problem/9252
9252번: LCS 2
LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, 모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다. 예를 들어, ACAYKP와 CAPCAK의 LCS는 ACAK가 된다.
www.acmicpc.net
반응형
// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
int dp[1001][1001]{ 0 };
stack<char> ans;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL);
string str1, str2;
cin >> str1 >> str2;
register int str1_size = str1.size(), str2_size = str2.size();
for (register int i = 0, j; i <= str1_size; ++i)
for (j = 0; j <= str2_size; ++j)
dp[i][j] = 0;
for (register int i = 1, j; i <= str1_size; ++i)
for (j = 1; j <= str2_size; ++j)
if (str1.at(i - 1) == str2.at(j - 1))
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
while (str1_size > 0 && str2_size > 0)
if (dp[str1_size][str2_size] == dp[str1_size - 1][str2_size])
--str1_size;
else if (dp[str1_size][str2_size] == dp[str1_size][str2_size - 1])
--str2_size;
else
--str1_size, --str2_size, ans.push(str1.at(str1_size));
cout << dp[str1.size()][str2.size()] << "\n";
while (!ans.empty())
cout << ans.top(), ans.pop();
return 0;
}
// *&)*@*
이전 'LCS' 문제와 동일 유형입니다.
LCS (feat. 백준, 9251번)
LCS https://www.acmicpc.net/problem/9251 9251번: LCS LCS(Longest Common Subsequence, 최장 공통 부분 수열)문제는 두 수열이 주어졌을 때, 모두의 부분 수열이 되는 수열 중 가장 긴 것을 찾는 문제이다. 예..
kim519620.tistory.com
728x90
반응형
'생활 > 코테' 카테고리의 다른 글
숨바꼭질 4 (feat. 백준, 13913번) (0) | 2022.08.25 |
---|---|
플로이드 2 (feat. 백준, 11780번) (2) | 2022.08.25 |
가장 긴 증가하는 부분 수열 5 (feat. 백준, 14003번) (0) | 2022.08.25 |
1로 만들기 2 (feat. 백준, 12852번) (0) | 2022.08.25 |
냅색문제 (feat. 백준, 1450번) (0) | 2022.08.25 |
Comments