VK Cup 2012 Round 2 A. Substring and Subsequence(DP)

题目链接
在这里插入图片描述
题意:给定两个串s1,s2,求s1的子串(连续)和s2的子序列(不连续)相同的个数
思路:dp[i][j]表示s1从[1,i]的子串和s2从[1,j]的子序列中相同的个数。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5e3+5; 
const int mod=1e9+7; 
char s1[maxn],s2[maxn];
ll dp[maxn][maxn];
int main()
{
	scanf("%s",s1+1);
	scanf("%s",s2+1);
	int len1=strlen(s1+1),len2=strlen(s2+1);
	for(int i=1;i<=len1;++i)
	{
		for(int j=1;j<=len2;++j)
		{
			dp[i][j]=dp[i][j-1];
			if(s1[i]==s2[j]) dp[i][j]=(dp[i][j]+dp[i-1][j-1]+1)%mod;
		}
	}
	ll ans=0;
	for(int i=1;i<=len1;++i) ans=(ans+dp[i][len2])%mod;
	printf("%lld\n",ans);
}
发布了391 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105374723
今日推荐