题目: 把A的子串和B的子串 穿插 起来,问用多少种方案的到C, 如Now suppose there are two subsequences “abc” and “de”. By combining them you can get the following strings “abcde”, “abdce”, “abdec”, “adbce”, “adbec”, “adebc”, “dabce”, “dabec”, “daebc” and “deabc”.
分析:
偏难的DP题,参考了相关资料才做出的。 资料里讲的很清楚,关键的理解状态转移方程。
代码:
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MOD=10007;
const int maxn=65;
char s1[maxn],s2[maxn],s3[maxn];
int f1[maxn][maxn][maxn],f2[maxn][maxn][maxn],f[maxn][maxn][maxn];
int N,M,L;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s%s%s",s1+1,s2+1,s3+1);
N=strlen(s1+1),M=strlen(s2+1),L=strlen(s3+1);
for(int i=0; i<=N; i++)
{
for(int j=0; j<=M; j++)
{
f1[i][j][0]=f2[i][j][0]=f[i][j][0]=1;
}
}
int i,j,k;
for(k=1; k<=L; k++)
{
for(i=0; i<=N; i++)
{
for(j=0; j<=M; j++)
{
if(i)
{
f1[i][j][k] = f1[i-1][j][k];
if(s1[i]==s3[k]) f1[i][j][k] += f[i-1][j][k-1];
f1[i][j][k] %= MOD;
}
if(j)
{
f2[i][j][k] = f2[i][j-1][k];
if(s2[j]==s3[k]) f2[i][j][k] += f[i][j-1][k-1];
f2[i][j][k] %= MOD;
}
f[i][j][k] = (f1[i][j][k] + f2[i][j][k]) % MOD;
}
}
}
printf("%d\n",f[N][M][L]);
}
return 0;
}