Cod sursa(job #3331282)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 26 decembrie 2025 13:51:33
Problema Subsir Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <bits/stdc++.h>
#define ll long long

using namespace std;

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

const int NMAX = 500;
const int MOD = 666013;

int n, m, answer;
string a, b;
int dp[NMAX + 1][NMAX + 1][NMAX + 1];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    fin >> a >> b;
    n = a.size();
    m = b.size();
    a = "%" + a;
    b = "%" + b;

    for(int i = 0; i <= n; i++) {
        for(int j = 0; j <= m; j++) {
            dp[0][i][j] = 1;
        }
    }
    for(int l = 1; l <= min(n, m); l++) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++) {
                if(a[i] == b[j]) {
                    dp[l][i][j] = dp[l - 1][i - 1][j - 1];
                }
                else {
                    dp[l][i][j] = (dp[l][i][j] + dp[l][i - 1][j] + dp[l][i][j - 1] - dp[l][i - 1][j - 1]) % MOD;
                }
            }
        }
    }

//    fout << dp[1][3][2] << '\n';
//    fout << dp[0][2][1] << '\n';

    for(int l = 1; l <= min(n, m); l++) {
        if(dp[l][n][m]) {
            answer = dp[l][n][m];
        }
    }
    fout << answer << '\n';
    return 0;
}