Cod sursa(job #2179327)

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

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], nr, ans;

struct sir
{
    char val[LIM];
} v[LIM];

bool cmp(sir a, sir b) 
{
    return strcmp(a.val+1, b.val+1) <= 0;
}
int main()
{
    cin >> s+1 >> t+1;
    n = strlen(s+1);
    m = strlen(t+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;
            else
                mat[i][j] = max(mat[i-1][j], mat[i][j-1]);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            if(mat[i][j] == mat[n][m] and s[i] == t[j])
            {
                nr++;
                int len = mat[n][m], x = i, y = j;  
                for(int k = len; k >= 1; --k)
                {
                        while(s[x] != t[y])
                        {
                                if(mat[x-1][y] > mat[x][y-1])
                                        x--;
                                else    
                                        y--;
                        }               
                        v[nr].val[k] = s[x];
                        x--, y--;
                }
            }
    sort(v+1, v+nr+1, cmp);
    for(int i = 2; i <= nr; ++i)
    {
        //cout << v[i].val+1 << '\n';
        if(strcmp(v[i].val+1, v[i-1].val+1) != 0)
            ans++;
    }
    cout << ans+1 << '\n';
    return 0;
}