HDU 6153 A Secret (扩展KMP)

网友投稿 264 2022-08-31

HDU 6153 A Secret (扩展KMP)

Description

Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:Suffix(S2,i) = S2[i…len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.

Input

Input contains multiple cases.The first line contains an integer T,the number of cases.Then following T cases.Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.

Output

For each test case,output a single line containing a integer,the answer of test case.The answer may be very large, so the answer should mod 1e9+7.

Sample Input

2aaaaaaaabababababa

Sample Output

1319

题意

给出两个字符串 A 和 B ,求 B 的所有后缀在 A 中的出现次数与后缀长度的乘积和。

思路

枚举所有后缀显然不是理想的方案,但是我们把两个串倒过来,问题就变成了求 B 的所有前缀在 A 中出现次数与后缀长度的乘积和。

于是便是 扩展 KMP 的基础题目啦。

对于 ​​extend[i]​​​ 所保存的 ​​y[i..n-1]​​​ 与 ​​x[0..m-1]​​ 的最长公共前缀。

我们可以枚举所有的 ​​i​​​ ,对于每一个 ​​i​​ ,结果便是 1+2+3+...+extend[i] ,即 extend[i]×(extend[i]+1)2

累计求和即可。

AC 代码

#includeusing namespace std;const int maxn = 1e6+10;typedef __int64 LL;const int mod = 1e9+7;void pre_ekmp(char *x,int m,int next[]){ next[0]=m; int j=0; while(j+1>T; while(T--) { cin>>str1>>str2; int n=strlen(str1); int m=strlen(str2); reverse(str1,str1+n); reverse(str2,str2+m); ekmp(str2,m,str1,n,nex,extend); LL ans=0; for(int i=0; i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Codeforces 913 C. Party Lemonade (思维)
下一篇:如何从整合营销入手,提升用户转化率?(提高转化率的销售技巧)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~