https://vjudge.net/problem/CodeForces-163A
題目大意:給出字符串,求從中取出任意一個子串,從中取出任意一個子序列,且兩者相等的方案數。
思路:表示以結尾的子串和中子序列匹配的方案數。當與不相等時,顯然有,當時,除了上述情況外,以結尾的子串可以加上字符,對應的子序列可以加上字符,它們依是匹配的,而又可以單獨拿出來匹配,所以有。
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pr pair<int,int>
using namespace std;
typedef long long ll;
const int maxn=5005;
const int mod=1e9+7;
char a[maxn],b[maxn];
int dp[maxn][maxn];
int main()
{
scanf("%s%s",a+1,b+1);
int n1=strlen(a+1),n2=strlen(b+1);
int ans=0;
for(int i=1;i<=n1;i++)
{
for(int j=1;j<=n2;j++)
{
dp[i][j]=dp[i][j-1];
if(a[i]==b[j])
{
dp[i][j]+=dp[i-1][j-1]+1;
dp[i][j]%=mod;
}
}
ans=(ans+dp[i][n2])%mod;
}
printf("%d\n",ans);
return 0;
}