Cod sursa(job #1239921)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 9 octombrie 2014 23:12:16
Problema Iv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <cstring>
#define DIM 505
#define MOD 3210121
#define infile "iv.in"
#define outfile "iv.out"

using namespace std;

ifstream f(infile);
ofstream g(outfile);

int D1[DIM][DIM], D2[DIM][DIM];

char a[DIM], b[DIM];

int main () {
    f >> a + 1;
    f >> b + 1;
    int n = strlen(a + 1);
    int m = strlen(b + 1);
    int len = n + m;
    int SOL = 0;
    D1[0][0] = 1;
    for (int i = 0; i <= n; ++i) {
        memset(D2, 0, sizeof(D2));
        for (int j = 0; i + j <= n; ++j)
            for (int k = 0; k <= m; ++k) {
                int p = i - j + k;
                if (p < 0 || p > m - k)
                    continue;
                if (len - 1 <= i + j + k + p && i + j + k + p <= len) {
                    SOL += D1[j][k];
                    SOL %= MOD;
                    continue;
                }
                if (a[i + 1] == a[n - j])
                    D2[j + 1][k] = (D2[j + 1][k] + D1[j][k]) % MOD;
                if (b[k + 1] == a[n - j])
                    D1[j + 1][k + 1] = (D1[j + 1][k + 1] + D1[j][k]) % MOD;
                if (m > p && a[i + 1] == b[m - p])
                    D2[j][k] = (D2[j][k] + D1[j][k]) % MOD;
                if (m > p && b[k + 1] == b[m - p])
                    D1[j][k + 1] = (D1[j][k + 1] + D1[j][k]) % MOD;
            }
        memcpy(D1, D2, sizeof(D1));
    }
    g << SOL;
    return 0;
}

//This room. This bullet. There's a bullet for everyone. And a time. And a place. Yes... maybe this is how it has to be. - 47