Cod sursa(job #201880)
#include<iostream>
#include<string>
#include<set>
using namespace std;
int a[501][501];
set<string> sol;
string s1;
string s2;
void parc(int i, int j, string s)
{
if(i == 0 || j == 0)
{
sol.insert(s);
return;
}
int max = a[i-1][j];
bool up = true;
if(a[i][j-1] > max)
{
max = a[i][j-1];
up = false;
}
if(a[i][j] > max)
{
string st = s;
st += s1.at(i-1);
parc(i-1,j-1, st);
}else
{
if(a[i][j-1] == a[i-1][j])
{
parc(i-1, j, s);
parc(i, j-1, s);
}else
{
if(up)
parc(i-1, j, s);
else
parc(i,j-1,s);
}
}
}
int main(void)
{
freopen("subsir.in", "r", stdin);
freopen("subsir.out", "w", stdout);
cin >> s1;
cin >> s2;
int l1 = s1.length();
int l2 = s2.length();
for(int i=0;i<=l1;++i)
{
a[i][0] = 0;
}
for(int i=0;i<=l2;++i)
{
a[0][i] = 0;
}
for(int i=1; i<=l1;++i)
{
for(int j=1;j<=l2;++j)
{
if(s1.at(i-1) == s2.at(j-1))
{
a[i][j] = a[i-1][j-1]+1;
}else
{
if(a[i-1][j] >= a[i][j-1])
{
a[i][j] = a[i-1][j];
}else
{
a[i][j] = a[i][j-1];
}
}
}
}
parc(l1,l2,"");
cout << sol.size() << endl;
return 0;
}