CodeForces - 163A dp, a串的子串,b串的子序列看多少相同的对数

One day Polycarpus got hold of two non-empty strings s and t, consisting of lowercase Latin letters. Polycarpus is quite good with strings, so he immediately wondered, how many different pairs of "x y" are there, such that x is a substring of string sy is a subsequence of string t, and the content of x and y is the same. Two pairs are considered different, if they contain different substrings of string sor different subsequences of string t. Read the whole statement to understand the definition of different substrings and subsequences.

The length of string s is the number of characters in it. If we denote the length of the string s as |s|, we can write the string as s = s1s2... s|s|.

substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings or "codeforces", while "coders" is not. Two substrings s[a... b] and s[c... d] are considered to be different if a ≠ cor b ≠ d. For example, if s="codeforces", s[2...2] and s[6...6] are different, though their content is the same.

subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces". Two subsequences u = s[p1p2... p|u|] and v = s[q1q2... q|v|] are considered different if the sequences p and q are different.

Input

The input consists of two lines. The first of them contains s (1 ≤ |s| ≤ 5000), and the second one contains t (1 ≤ |t| ≤ 5000). Both strings consist of lowercase Latin letters.

Output

Print a single number — the number of different pairs "x y" such that x is a substring of string sy is a subsequence of string t, and the content of x and y is the same. As the answer can be rather large, print it modulo 1000000007 (109 + 7).

Example
Input
aa
aa
Output
5
Input
codeforces
forceofcode
Output
60
Note

Let's write down all pairs "x y" that form the answer in the first sample: "s[1...1]t[1]", "s[2...2] t[1]", "s[1...1] t[2]","s[2...2] t[2]", "s[1...2] t[1 2]".


题意是,s的子串(连续的)和t的子序列(可以不连续)相同的对数;

用动规做:

dp[i][j]  当a数组(串)长度为i时,j数组(串)长度为j时且以a[i]结尾的子串相等的对数;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Mod 1000000007
int dp[5005][5005]; //dp[i][j] 当a数组长度为i时,b数组长度为j时且以a[i]为结尾的相等子串个数; 
char a[5005],b[5005];
int main()
{
	int i,j,k,t,n,m;
	for(i=0;i<=5000;i++)
	{
		dp[i][0]=0;
		dp[0][i]=0;
	}
	while(~scanf("%s%s",a+1,b+1))
	{
		n=strlen(a+1);
		m=strlen(b+1);
		int ans=0;
		
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				dp[i][j]=dp[i][j-1];//j以前以a[i]结尾的字串个数; 
				if(a[i]==b[j]) 
				{
					dp[i][j]+=dp[i-1][j-1]+1;//看看有没有以a[i-1]结尾的子串个数,若有的话所有的加上
									// 加上a[i]就成了以a[i]结尾的子串了,加上1,就是a[i]==b[j]这个单字母子串 
					dp[i][j]%=Mod;
				}
				
			}
			ans=(ans+dp[i][m])%Mod; 
		} 
		printf("%d\n",ans);
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值