Pagini recente » Cod sursa (job #2835174) | Cod sursa (job #1267882) | Cod sursa (job #1228522) | Cod sursa (job #1586613) | Cod sursa (job #2179306)
#include <fstream>
#include <cstring>
using namespace std;
ifstream cin ("subsir.in");
ofstream cout ("subsir.out");
const int LIM = 505;
char s[LIM], t[LIM];
int n, m, mat[LIM][LIM], dp[LIM], viz[LIM][30];
int main()
{
cin >> s+1 >> t+1;
n = strlen(s+1);
m = strlen(t+1);
dp[0] = 1;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(s[i] == t[j])
{
mat[i][j] = mat[i-1][j-1]+1;
if(viz[ mat[i][j] ][ s[i]-'a' ] == 0)
dp[ mat[i][j] ] += dp[ mat[i-1][j-1] ];
viz[ mat[i][j] ][ s[i]-'a' ] = 1;
}
else
mat[i][j] = max(mat[i-1][j], mat[i][j-1]);
cout << dp[ mat[n][m] ] << '\n';
return 0;
}