백준 문제풀이

[BOJ C++] 백준 9251번: LCS

audxkawjd17 2025. 10. 1. 19:38

 📚 문제

[BOJ C++] 백준 9251번: LCS
https://www.acmicpc.net/problem/9251

 


 📝 입력 및 출력

 


 🔎 문제 풀이


 ⌨️ C++ 코드

더보기
#include <bits/stdc++.h>
using namespace std;

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

    string str1, str2;
    cin >> str1 >> str2;
    int len1 = str1.size(), len2 = str2.size();
    vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1));

    for (int i = 1; i <= len1; i++) {
        for (int j = 1; j <= len2; 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]);
        }
    }
    cout << dp[len1][len2];
}