Cod sursa(job #2146998)

Utilizator ajeccAjechiloae Eugen ajecc Data 28 februarie 2018 13:08:01
Problema Subsir Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.63 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 n, m;
char s[NMAX], t[NMAX];
int dp[NMAX][NMAX], next_s[NMAX][30], next_t[NMAX][30], sol[NMAX][NMAX];
bool trecut[30];


int main()
{
    FASTIO;
    string S, T;
    fin >> S >> T;
    for1(i, S.size()) s[i] = S[i - 1];
    for1(i, T.size()) t[i] = T[i - 1];
    n = S.size(); m = T.size();

    for1(i, n)
        for1(j, m)
        {
            if(s[i] == t[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }

    print(dp[n][m]);

    for1(i, n)
    {
        for0(j, 30) next_s[i][j] = next_s[i - 1][j];
        next_s[i][s[i] - 'a'] = i;
    }
    for1(i, m)
    {
        for0(j, 30) next_t[i][j] = next_t[i - 1][j];
        next_t[i][t[i] - 'a'] = i;
    }

    for1(i, n)
        for1(j, m) if(s[i] == t[j])
        {
            if(dp[i][j] == 1)
            {
                sol[i][j] = 1;
                continue;
            }

            for0(k, 30)
            {
                int ns = next_s[i - (s[i] == k)][k];
                int nt = next_t[j - (t[j] == k)][k];
                if(ns && nt && dp[ns][nt] == dp[i][j] - 1)
                    sol[i][j] = (sol[ns][nt] + sol[i][j]) % MOD;
            }
        }
    int SOL = 0;
    for(int i = n; i >= 1; i--) for(int j = m; j >= 1; j--) if(dp[i][j] == dp[n][m] && s[i] == t[j] && !trecut[s[i] - 'a'])
    {
        SOL = (SOL + sol[i][j]) % MOD;
        trecut[s[i] - 'a'] = 1;
    }
    fout << SOL;

    return 0;
}