Cod sursa(job #605883)

Utilizator crushackPopescu Silviu crushack Data 2 august 2011 17:24:14
Problema Iv Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <stdio.h>
#include <string.h>
#define LMax 510

const char IN[]="iv.in",OUT[]="iv.out";
const int mod=3210121;

int La,Lb;
char a[LMax] , b[LMax];
int T[2][LMax][LMax];

int solve()
{
    int p1,p2,p3,p4;

    for (p1=La-1;p1>=0;--p1)
    {
        for (p2=p1;p2<La;++p2)
            for (p3=Lb-1;p3>=0;--p3)
            {
                p4= La - p3 -1 + 1 + Lb + 1 - p1-1 - p2-1 -1;

                if (p1==p2 && p3==p4) {T[0][p2][p3]=2;continue;}

                T[0][p2][p3]=0;
                if (a[p1]==b[p4]) T[0][p2][p3]+= T[1][p2][p3];
                if (a[p1]==a[p2]) T[0][p2][p3]+= T[1][p2-1][p3];
                if (b[p3]==b[p4]) T[0][p2][p3]+= T[0][p2][p3+1];
                if (a[p2]==b[p3]) T[0][p2][p3]+= T[0][p2-1][p3+1];

                T[0][p2][p3]%=mod;
            }
            memcpy(T[1],T[0],sizeof(T[1]));
    }
    return T[1][La-1][0];
}

int main()
{
    freopen(IN,"r",stdin);
    fgets( a , LMax , stdin );
    fgets( b , LMax , stdin );
    fclose(stdin);

    La=strlen(a)-1;Lb=strlen(b)-1;
    int r=solve();

    freopen(OUT,"w",stdout);
    printf("%d\n",r);
    fclose(stdout);

    return 0;
}