Cod sursa(job #2945009)

Utilizator bem.andreiIceman bem.andrei Data 23 noiembrie 2022 12:12:09
Problema Iv Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.57 kb
#include <bits/stdc++.h>

using namespace std;
ifstream r("iv.in");
ofstream w("iv.out");

const int mod = 3210121;
int a, b, dp[2][505][505], rez;
string s1, s2;

int main()
{
    r >> s1 >> s2;
    a = s1.size();
    b = s2.size();
    s1 = '$' + s1;
    s2 = '$' + s2;
    dp[0][0][0] = 1;
    for(int i = 0; i <= a; i++)
    {
        for(int j = 0; i + j <= a; j++)
        {
            for(int x = 0; x <= b; x++)
            {
                int y = i + x - j;
                if(y < 0 || x + y > b)
                {
                    continue;
                }
                int ci = i % 2, ni = (i + 1) % 2, curr = dp[i % 2][j][x];
                dp[ci][j][x] = 0;
                if(i + j + x + y == a + b || i + j + x + y == a + b - 1)
                {
                    rez = (rez + curr) % mod;
                    continue;
                }
                if(s1[i + 1] == s1[a - j])
                {
                    dp[ni][j + 1][x] = (dp[ni][j + 1][x] + curr) % mod;
                }
                if(y < b && s1[i + 1] == s2[b - y])
                {
                    dp[ni][j][x] = (dp[ni][j][x] + curr) % mod;
                }
                if(s2[x + 1] == s1[a - j])
                {
                    dp[ci][j + 1][x + 1] = (dp[ci][j + 1][x + 1] + curr) % mod;
                }
                if(y < b && s2[x + 1] == s2[b - y])
                {
                    dp[ci][j][x + 1] = (dp[ci][j][x + 1] + curr) % mod;
                }
            }
        }
    }
    w << rez;
    return 0;
}