Pagini recente » Cod sursa (job #1842425) | Cod sursa (job #1539202) | Cod sursa (job #2084595)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin("subsir.in");
ofstream fout("subsir.out");
const int MAX = 510;
const int MOD = 666013;
int N, M;
string a, b;
int c[MAX][MAX];
int cnt[MAX][MAX];
void Read();
void Cmlsc();
int main()
{
Read();
Cmlsc();
fout << cnt[N][M] << '\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 = 0; i <= N; ++i)
cnt[i][0] = 1;
for (int j = 0; j <= M; ++j)
cnt[0][j] = 1;
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;
cnt[i][j] = cnt[i - 1][j - 1];
}
else
{
if (c[i - 1][j] > c[i][j - 1])
{
c[i][j] = c[i - 1][j];
cnt[i][j] = cnt[i - 1][j];
}
else
if (c[i - 1][j] < c[i][j - 1])
{
c[i][j] = c[i][j - 1];
cnt[i][j] = cnt[i][j - 1];
}
else
{
c[i][j] = c[i][j - 1];
if (c[i][j] != 0)
cnt[i][j] = (cnt[i - 1][j] + cnt[i][j - 1]) % MOD;
else
cnt[i][j] = 1;
}
}
}