Cod sursa(job #2478578)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 22 octombrie 2019 13:41:35
Problema Iv Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.11 kb
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;
const int MOD = 3210121;

int add(int a, int b)
{
        a += b;
        if (a >= MOD)
                return a - MOD;
        if (a < 0)
                return a + MOD;
        return a;
}

int mul(int a, int b)
{
        return a * (ll) b % MOD;
}

const int N = 500 + 7;
int n, a[N], m, b[N];
string s, t;
int dp[N][N], ndp[N][N];
int vals[N];

int main()
{
        freopen ("iv.in", "r", stdin);
        freopen ("iv.out", "w", stdout);

        cin >> s >> t;
        n = (int) s.size();
        m = (int) t.size();

        for (int i = 1; i <= n; i++)
                a[i] = s[i - 1];
        for (int i = 1; i <= m; i++)
                b[i] = t[i - 1];

        dp[1][n] = 1;
        int all = (n + m) / 2;

        for (int l = 1; l <= all; l++)
        {
                for (int p1 = 1; p1 <= n; p1++)
                        for (int p2 = 1; p2 <= n; p2++)
                                if (dp[p1][p2])
                                {
                                        int p3 = l - p1 + 1;
                                        int p4 = m - (n - p2);
                                      ///  cout << " : " << p1 << " " << p2 << "\n";
                                      ///  cout << " : " << p3 << " " << p4 << "\n";
                                        ///cout << " : " << p1 << " and " << p2 << " : " << p3 << " " << p4 << "\n";
                                        ///return 0;
                                        //if (p3 <= p4)
                                        {
                                                vals[1] = a[p1];
                                                vals[2] = a[p2];
                                                vals[3] = b[p3];
                                                vals[4] = b[p4];
                                                for (int f = 1; f <= 3; f += 2)
                                                        for (int s = 2; s <= 4; s += 2)
                                                                if (vals[f] == vals[s])
                                                                {
                                                                 //       cout << p1 + (f == 1) << ", " << p2 - (s == 2) << "\n";
                                                                        ndp[p1 + (f == 1)][p2 - (s == 2)] = add(ndp[p1 + (f == 1)][p2 - (s == 2)], dp[p1][p2]);
                                                                }
                                        }
                                }
                for (int i = 0; i <= n + 1; i++)
                        for (int j = 0; j <= n + 1; j++)
                        {
                                dp[i][j] = ndp[i][j];
                                ndp[i][j] = 0;
                        }
        }
        int ans = 0;
        for (int i = 0; i <= n + 1; i++)
                for (int j = 0; j <= n + 1; j++)
                        ans = add(ans, dp[i][j]);
        cout << ans << "\n";

        return 0;
}