Cod sursa(job #2253088)

Utilizator robx12lnLinca Robert robx12ln Data 3 octombrie 2018 17:00:15
Problema Iv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.79 kb
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("iv.in");
ofstream fout("iv.out");
const int MOD = 3210121;
int N, M, Dp[2][505][505], t, Sol;
char A[505], B[505];

inline int modulo( int x ){

    if( x > MOD )
        x -= MOD;

    return x;
}

int main(){

    fin >> A + 1;
    N = strlen( A + 1 );

    fin >> B + 1;
    M = strlen( B + 1 );

    if( N % 2 == 1 && M % 2 == 1 ){
        fout << "0\n";
        return 0;
    }

    Dp[0][0][N + 1] = 1;
    for( int st_A = 0, t = 0; st_A <= N; st_A++, t = 1 - t ){

        memset( Dp[1 - t], 0, sizeof(Dp[1 - t]) );

        for( int st_B = 0; st_B <= M; st_B++ ){
            for( int sf_A = N + 1; sf_A > st_A; sf_A-- ){
                int sf_B = N + M + 2 - st_A - st_B - sf_A;

                if( Dp[t][st_B][sf_A] == 0 )
                    continue;

                if( A[st_A + 1] == A[sf_A - 1] && st_A + 1 < sf_A - 1 )
                    Dp[1 - t][st_B][sf_A - 1] = modulo( Dp[1 - t][st_B][sf_A - 1] + Dp[t][st_B][sf_A] );

                if( B[st_B + 1] == B[sf_B - 1] && st_B + 1 < sf_B - 1 )
                    Dp[t][st_B + 1][sf_A] = modulo( Dp[t][st_B + 1][sf_A] + Dp[t][st_B][sf_A] );

                if( A[st_A + 1] == B[sf_B - 1] && st_A + 1 < sf_A && st_B < sf_B - 1 )
                    Dp[1 - t][st_B][sf_A] = modulo( Dp[1 - t][st_B][sf_A] + Dp[t][st_B][sf_A] );

                if( B[st_B + 1] == A[sf_A - 1] && st_A < sf_A - 1 && st_B + 1 < sf_B )
                    Dp[t][st_B + 1][sf_A - 1] = modulo( Dp[t][st_B + 1][sf_A - 1] + Dp[t][st_B][sf_A] );

                if( sf_A - st_A == 1 + N % 2 && sf_B - st_B == 1 + M % 2 )
                    Sol = modulo( Sol + Dp[t][st_B][sf_A] );

            }
        }
    }

    fout << Sol << "\n";

    return 0;
}