알고리즘 공부

백준 9251번 LCS C++

빵어 2024. 10. 31. 17:53
#include <iostream>
#include <string>
using namespace std;

int dp[1001][1001]{};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	string s1, s2;
	cin >> s1 >> s2;

	for (int i = 1; i <= s1.length(); ++i) {
		for (int j = 1; j <= s2.length(); ++j) {
			if (s1[i - 1] == s2[j - 1])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
		}
	}

	cout << dp[s1.length()][s2.length()];

	return 0;
}

 

DP에 이차원 배열 활용

 

반례 

SNPJDMD

SD

 

https://www.acmicpc.net/problem/9251