Cod sursa(job #3346652)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 14 martie 2026 19:49:16
Problema Subsir Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
// hai sa nu trollam labubu de pa
#include <bits/stdc++.h>

using namespace std;

const long long max_size = 5e3 + 20, mod = 666013;

int dp[max_size][max_size], ways[max_size][max_size];

void solve()
{
    string a, b;
    cin >> a >> b;
    int n = a.size(), m = b.size();
    a = " " + a;
    b = " " + b;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i] == b[j])
            {
                dp[i][j] = dp[i - 1][j - 1] + 1;
                ways[i][j] = ways[i - 1][j - 1];
                if (ways[i][j] == 0)
                {
                    ways[i][j] = 1;
                }
            }
            else
            {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                if (dp[i][j] == dp[i - 1][j])
                {
                    ways[i][j] = (ways[i][j] + ways[i - 1][j]) % mod;
                }
                if (dp[i][j] == dp[i][j - 1])
                {
                    ways[i][j] = (ways[i][j] + ways[i][j - 1]) % mod;
                }
            }
        }
    }
    cout << ways[n][m];
}

signed main()
{
#ifdef LOCAL
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
#else
    freopen("subsir.in", "r", stdin);
    freopen("subsir.out", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    long long tt;
    //cin >> tt;
    tt = 1;
    while (tt--)
    {
        solve();
    }
    return 0;
}