https://www.acmicpc.net/problem/5582
문자열 + dp 기본 문제
너무 좋은 LCS 알고리즘 설명글도 아래에 공유
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <set>
#include <sstream>
using namespace std;
string str1, str2;
int idx1, idx2, ans = 0;
int dp[4001][4001] = { 0, };
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin >> str1 >> str2;
int len1 = str1.length(), len2 = str2.length();
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
if (str1.at(i) == str2.at(j)) {
dp[i + 1][j + 1] = dp[i][j] + 1;
if (ans < dp[i + 1][j + 1])
ans = dp[i + 1][j + 1];
}
}
}
cout << ans;
return 0;
}
'알고리즘 > 알고리즘' 카테고리의 다른 글
[C++] 백준 20166번 : 문자열 지옥에 빠진 호석 (0) | 2022.05.13 |
---|---|
[C++] 백준 2866번 : 문자열 잘라내기 (0) | 2022.05.13 |
[C++] 백준 20413번 : MVP 다이아몬드 (Easy) (0) | 2022.05.08 |
[C++] 프로그래머스 : 소수찾기 (0) | 2022.03.10 |
[C++] 프로그래머스 : 코딩테스트연습_정렬 (0) | 2022.02.21 |