Cod sursa(job #2145955)

Utilizator ajeccAjechiloae Eugen ajecc Data 27 februarie 2018 18:34:08
Problema Subsir Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.35 kb
#include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#ifdef _WIN32
#include <windows.h>
#define print(x) PRINT(x, #x)
template<typename T> inline const void PRINT(T VARIABLE, string NAME)
{
#ifndef ONLINE_JUDGE /// ONLINE_JUDGE IS DEFINED ON CODEFORCES
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, 10);
    cerr << NAME << " = " << VARIABLE;
    SetConsoleTextAttribute(hConsole, 7);
    cerr << '\n';
#endif
}
#else
#define print(x) 0
#endif
typedef long long ll;
typedef unsigned long long ull;
const ll INFLL = 2 * (ll)1e18 + 100;
const int INFINT = 2 * (int)1e9 + 100;
const double PI = atan(1) * 4;
const double EPS = 1e-12;
const int SEED = 1e3 + 7;

const int MOD = 666013; /// careful here (7 or 9, 66.. etc)
const int NMAX = 500 + 5;

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

int mmax;
int dp[NMAX][NMAX];
bool calc[NMAX][NMAX];

string s, t;

void solve(int x, int y)
{
    if(calc[x][y]) return;
    if(x == 0 || y == 0)
    {
        calc[x][y] = 1;
        return;
    }
    if(s[x - 1] == t[y - 1])
    {
        if(!calc[x - 1][y - 1]) solve(x - 1, y - 1);
        dp[x][y] = 1 + dp[x - 1][y - 1];
    }
    else
    {
        if(!calc[x - 1][y]) solve(x - 1, y);
        if(!calc[x][y - 1]) solve(x, y - 1);
        dp[x][y] = max(dp[x - 1][y], dp[x][y - 1]);
    }
    calc[x][y] = 1;
}

int next_s[NMAX][30], next_t[NMAX][30];
bool calc2[NMAX][NMAX];
int sol[NMAX][NMAX];

void solve2(int x, int y)
{
    if(calc2[x][y]) return;
    if(x == 0 || y == 0)
    {
        calc2[x][y] = 1;
        return;
    }
    if(dp[x][y] == 1)
    {
        sol[x][y] = 1;
        calc2[x][y] = 1;
        return;
    }

    for(int k = (int)'a'; k <= (int)'z'; k++)
    {
        int ns = next_s[x - (s[x - 1] == k)][k - (int)'a'];
        int nt = next_t[y - (s[y - 1] == k)][k - (int)'a'];
        if(dp[x][y] == dp[ns][nt] + 1 && ns && nt)
        {
            if(!calc2[ns][nt]) solve2(ns, nt);
            sol[x][y] = (sol[x][y] + sol[ns][nt]) % MOD;
        }
    }
    calc2[x][y] = 1;
}

int main()
{
    FASTIO;
    fin >> s >> t;
    solve(s.size(), t.size());

    for0(i, NMAX) for0(j, 30) next_s[i][j] = next_t[i][j] = -1;

    for1(i, s.size())
    {
        if(i == 0)
        {
            next_s[i][s[i] - 'a'] = i;
            continue;
        }
        for0(j, 30) next_s[i][j] = next_s[i - 1][j];
        next_s[i][s[i] - 'a'] = i;
    }

    for1(i, t.size())
    {
        if(i == 0)
        {
            next_t[i][t[i] - 'a'] = i;
            continue;
        }
        for0(j, 30) next_t[i][j] = next_t[i - 1][j];
        next_t[i][t[i] - 'a'] = i;
    }
    for(int i = s.size(); i >= 1; i--)
        for(int j = t.size(); j >= 1; j--)
           if(!calc2[i][j])
               solve2(i, j);
    int SOL = 0;
    solve2(s.size(), t.size());
    for0(i, s.size() + 1) for0(j, t.size() + 1) SOL = (SOL + sol[i][j]) % MOD;
  //  cout << SOL << '\n';
    fout << sol[s.size()][t.size()];
    return 0;
}