Cod sursa(job #2985724)

Utilizator gabriel10tm@gmail.comGabriel Marian [email protected] Data 26 februarie 2023 22:35:49
Problema Subsir Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
using namespace std;
const int nmx = 1025;
/// lungimea celui mai lung subsir comun care se termina in poz i a sirului a si poz j a sirului b
int dp[nmx][nmx];
array<int,2> t[nmx][nmx];

#if 1
#define cin fin
#define cout fout
ifstream fin("subsir.in");
ofstream fout("subsir.out");
#endif // 1
int main(){
    string a,b;
    cin >> a >> b;
    a = " " + a;
    b = " " + b;
    int n = a.size()-1, m = b.size()-1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(a[i]==b[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
                t[i][j] = {i-1,j-1};
            }
            else if(dp[i-1][j]>dp[i][j-1]){
                dp[i][j] = dp[i-1][j];
                t[i][j] = {i-1,j};
            }
            else {
                dp[i][j] = dp[i][j-1];
                t[i][j] = {i,j-1};
            }
        }
    int mx = 0, cnt = 0;
    vector<array<int,2>> mxi;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(mx<dp[i][j]){
                mx = dp[i][j];
                mxi = {{i,j}};
            }
            else if (mx == dp[i][j]){
                mxi.push_back({i,j});
            }
    set<string> ans;
    for (array<int,2> it : mxi){
        string w;
        while(it[0] && it[1]){
            if(a[it[0]] == b[it[1]])
                w += a[it[0]];
            it = t[it[0]][it[1]];
        }
        ans.insert(w);
    }
    cout << ans.size();
}