Cod sursa(job #2084691)

Utilizator borcanirobertBorcani Robert borcanirobert Data 9 decembrie 2017 11:30:40
Problema Subsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.35 kb
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

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

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

void Read();
void Cmlsc();

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

    int res{0}, lmax{c[N][M]};
    for (int i = 0; i < MAXC; ++i)
        res = (res + nr[lmax][i]) % MOD;

    fout << res << '\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();
}

void 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;

                int l = c[i][j];
                int ch = a[i] - 'a';

                if (l == 1)
                {
                    nr[1][ch] = 1;
                    continue;
                }

                nr[l][ch] = 0;
                for (int k = 0; k < MAXC; ++k)
                    nr[l][ch] = (nr[l][ch] + nr[l - 1][k]) % MOD;
            }
            else
            {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
}