Cod sursa(job #3360354)

Utilizator rhyhrrhy1Dinca Matei rhyhrrhy1 Data 12 iulie 2026 21:55:43
Problema Cel mai lung subsir comun Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cmlsc.in");
ofstream fout("cmlsc.out");
pair<int,string> dp[1005][1005];
int main() {
    string s, s2;
    fin >> s >> s2;
    int n = s.length();
    int m = s2.length();
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (s[i-1] == s2[j-1]) {
                dp[i][j].first = 1 + dp[i - 1][j - 1].first;
                dp[i][j].second = dp[i - 1][j - 1].second + s[i-1];
            }
            else {
                if (dp[i-1][j].first > dp[i][j-1].first) {
                    dp[i][j] = dp[i-1][j];
                } else {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }
    }

    fout << dp[n][m].first << endl;
    fout << dp[n][m].second << endl;

    return 0;
}