Cod sursa(job #2179306)

Utilizator armandpredaPreda Armand armandpreda Data 20 martie 2018 09:13:53
Problema Subsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream cin ("subsir.in");
ofstream cout ("subsir.out");

const int LIM = 505;
char s[LIM], t[LIM];
int n, m, mat[LIM][LIM], dp[LIM], viz[LIM][30];

int main()
{
    cin >> s+1 >> t+1;
    n = strlen(s+1);
    m = strlen(t+1);
    dp[0] = 1;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            if(s[i] == t[j])
            {
                mat[i][j] = mat[i-1][j-1]+1;
                if(viz[ mat[i][j] ][ s[i]-'a' ] == 0)
                    dp[ mat[i][j] ] += dp[ mat[i-1][j-1] ];
                viz[ mat[i][j] ][ s[i]-'a' ] = 1;
            }
            else
                mat[i][j] = max(mat[i-1][j], mat[i][j-1]);
    cout << dp[ mat[n][m] ] << '\n';
    return 0;
}