Cod sursa(job #945343)

Utilizator addy01adrian dumitrache addy01 Data 1 mai 2013 18:40:06
Problema Subsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include<cstdio>
#include<cstring>
#define mod 666013
using namespace std;
int best[505][505],best2[505][505];
    char s1[505],s2[505];
    char sir[505][505];
int maxim(int a,int b)
{

    if(a>b)
        return a;

    return b;
}
int main()
{
    freopen("subsir.in","r",stdin);
    freopen("subsir.out","w",stdout);

    gets(s1);
    gets(s2);
int j,n,m,i,i1,j1,k=1,ans=0;
    n=strlen(s1);
    m=strlen(s2);



   for(i=0;i<n;i++)
        for(j=0;j<m;j++)
      if(s1[i]==s2[j])
                best2[i][j]=1+best2[i-1][j-1];
            else
                best2[i][j]=maxim(best2[i-1][j],best2[i][j-1]);





    for(i=0;i<n;i++)
        for(j=0;j<m;j++)
         {
      if(s1[i]==s2[j])
                best[i][j]=1+best[i-1][j-1];
            else
                best[i][j]=maxim(best[i-1][j],best[i][j-1]);



int bst=0;
        for (i1 = i , j1 = j ; i1>=0; )
            if (s1[i1] == s2[j1])
                sir[k][++bst] = s1[i1], --i1, --j1;
            else
                if (best[i1-1][j1] < best[i1][j1-1])
                    --j1;
                else
                    --i1;
if(bst==best2[n-1][m-1])
    {int ok=0;
        for(int w=1;w<k;w++)
            if(strcmp(sir[w]+1,sir[k]+1)==0)
                {
                    ok=1;
                    break;
                }

        if(ok==0)
         ans++;

}
        k++;
        }
cout<<ans;


    return 0;
}