CodeForces 163A

Problem:

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 s, y 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|.

A 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.

A 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 s, y 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]".

题目大意:

两个字符串,求第一个的所有子串(连续)与第二个的所有子序列(不一定连续)相同的情况之和;如果用最容易想到的办法:先枚举一个串的每一个子串,用枚举的这个子串与另一字符串的子串匹配,依次进行下去,肯定会超时。

状态方程:  dp[i][j]表示2号串中以i结尾的子序列与1号串中以j结尾的子串相等的所有情况数目;
状态转移方程:1,str1[j]!=str2[i],因为没有匹配上,dp[i][j]也就可以表示为2号串中以i-1结尾的子序列与1号串中以j结尾的子串相匹配的状况;即:dp[i][j]=dp[i-1][j];

       2,str1[j]=str2[i]时:情况的数目会增加:dp[i-1][j-1]+1;dp[i][j]=未匹配前状态下的情况数目+匹配成功增加的情况数目。
       那么结果就等于,对每次匹配成功后新增加的情况数求和;ans+=dp[i-1][j-1]+1;

// #include<bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue> //priority_queue
#include <map>
#include <set> //multiset set<int,greater<int>>大到小
#include <vector> // vector<int>().swap(v);清空释放内存
#include <stack>
#include <cmath> // auto &Name : STLName  Name.
#include <utility>
#include <sstream>
#include <string> //__builtin_popcount(ans);//获取某个数二进制位1的个数
using namespace std;
#define rep(i,a,n) 	for(int i=a;i<=n;i++)
#define per(i,a,n) 	for(int i=n;i>=a;i--)
#define read_a_int(x) 	scanf("%d",&x)
#define Read(x,y) 	scanf("%d%d",&x,&y)
#define INF 0x3f3f3f3f
#define MAX_N 5005
typedef long long ll;

const int MOD=1000000007;
char a[MAX_N],b[MAX_N];
int lensa,lensb;
int dp[MAX_N][MAX_N];

int main(void)
{	
	while(~scanf("%s%s",b+1,a+1))
	{
		lensa=strlen(a+1);
		lensb=strlen(b+1);
		ll ans=0;
		for(int i=1;i<=lensa;i++){
			for(int j=1;j<=lensb;j++)
			{
				dp[i][j]=dp[i-1][j];
				if(a[i]==b[j]){
					dp[i][j]=(dp[i-1][j-1]+dp[i][j]+1)%MOD;
					ans=(ans+dp[i-1][j-1]+1)%MOD;
				}
			}
		}
		printf("%lld\n",ans);
	}

	return 0;
}

  

 

猜你喜欢

转载自www.cnblogs.com/jaszzz/p/12814985.html
163