Cod sursa(job #2086014)

Utilizator borcanirobertBorcani Robert borcanirobert Data 11 decembrie 2017 09:13:54
Problema Subsir Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream fin("subsir.in");
ofstream fout("subsir.out");

const int MAX = 510;
const int MOD = 666013;
int N, M;
string a, b;
int c[MAX][MAX];
int nr[MAX][MAX];

void Read();
int Cmlsc();
int Solve();

int main()
{
    Read();
    Cmlsc();

    fout << Solve() << '\n';

    fin.close();
    fout.close();
    return 0;
}

void Read()
{
    fin >> a >> b;
    a = ' ' + a;
    b = ' ' + b;
    N = a.size() - 1;
    M = b.size() - 1;

   // cout << N << ' ' << M; cin.get();
}

int Cmlsc()
{
    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= M; ++j)
            if (a[i] == b[j])
                c[i][j] = c[i - 1][j - 1] + 1;
            else
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);

    return c[N][M];
}

int Solve()
{
    for (int i = 0; i <= N; ++i)
        nr[i][0] = 1;
    for (int j = 0; j <= M; ++j)
        nr[0][j] = 1;

    for (int i = 1; i <= N; ++i)
        for (int j = 1; j <= M; ++j)
        {
            if (a[i] == b[j])
            {
                nr[i][j] = nr[i - 1][j - 1];
                continue;
            }
            if (c[i - 1][j - 1] == c[i - 1][j] && c[i - 1][j - 1] == c[i][j - 1])
            {
                nr[i][j] = (nr[i - 1][j] + nr[i][j - 1] - nr[i - 1][j - 1] + MOD) % MOD;
                continue;
            }
            if (c[i - 1][j] == c[i][j - 1])
            {
                nr[i][j] = (nr[i - 1][j] + nr[i][j - 1]) % MOD;
                continue;
            }
            if (c[i - 1][j] > c[i][j - 1])
                nr[i][j] = nr[i - 1][j];
            else
                nr[i][j] = nr[i][j - 1];
        }

    return nr[N][M];
}