HDU 6153 A Secret
来源:互联网 发布:淘宝不自动打开淘口令 编辑:程序博客网 时间:2024/06/05 11:29
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 1924 Accepted Submission(s): 704
Problem 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.
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.
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.
The answer may be very large, so the answer should mod 1e9+7.
Sample Input
2aaaaaaaabababababa
Sample Output
1319
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153
题意:给定两个字符串T和P,问P的所有后缀的长度与其在字符串T中出现次数的乘积的和对1e9+7取余的结果。
解题思路:问的是后缀的匹配,其实将两个字符串都翻转一次就变成了前缀的匹配,而前缀的匹配问题就是扩展KMP的应用,所以想到这个就直接是KMP求next加扩展KMP求前缀匹配的问题了,因为问的是P的所有后缀的长度与其在字符串T中出现次数的乘积的和对1e9+7取余的结果,仔细一想其实就是T的后缀与P的最长公共前缀的数组extend的一个等差数列求和,如输入T=abababab,P=aba,翻转后T=babababa,P=aba,T的扩展数组extend={0,3,0,3,0,3,0,1},extend[1]=3,说明T中有字串aba,这一个子串中a出现一次,ba出现一次,aba出现一次,所以在这一子串中的部分和为3+2+1,就是extend[1]=3作为末项,1为首项公差为1的一个等差数列求和,所以总的答案就是所有不为零的extend作为等差数列的末项1为首项公差为1求和。
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int MAXL = 1e6 + 10;const int MOD = 1e9 + 7;char T[MAXL];char P[MAXL];int fail[MAXL];ll extend[MAXL];void get_fail(int m) //子串本身的最长公共前缀数组 {memset(fail,0,sizeof(fail));fail[0]=m; //与自身的最长公共前缀为本身的长度 int p,a; //p为匹配到达的最远位置,a为达到最远位置的起始位置 for(int i=1,j=-1;i<m;i++,j--) {//第一种情况,i+fail[i-a]>=p if(j<0 || i+fail[i-a]>=p) {if(j<0) j=0,p=i;while(p<m && P[j]==P[p]){j++;p++;}fail[i]=j;a=i;}//第二种情况:i+fail[i-a]<p else fail[i]=fail[i-a];}}void get_extend(int n,int m) //解释同get_fail() {memset(extend,0,sizeof(extend));int a,p;for(int i=0,j=-1;i<n;i++,j--){if(j<0 || i+fail[i-a]>=p){if(j<0) j=0,p=i;while(p<n && j<m && T[p]==P[j]){j++;p++;}extend[i]=j;a=i;}else extend[i]=fail[i-a];}}ll count(int n){ll sum=0;for(int i=0;i<n;i++){if(extend[i]!=0) //不为零的extend[i],作为末项,1为首项公差为1求和 {ll item=(extend[i]*(extend[i]+1)/2)%MOD;sum=(sum+item)%MOD;}}return sum; //返回结果 }void solve(){int lenT=strlen(T); //母串的长度 int lenP=strlen(P); //子串的长度 reverse(T,T+lenT); //将字符串翻转 reverse(P,P+lenP); get_fail(lenP); //获取子串本身的最长公共前缀数组 get_extend(lenT,lenP); //获得母串对于子串的最长公共前缀数组 ll ans=count(lenT); //计算结果 printf("%lld\n",ans);}int main(void){int test; //测试组数 scanf("%d",&test);getchar();while(test--){gets(T); //输入母串 gets(P); //输入字串 solve(); //处理函数 } return 0;}
阅读全文
0 0
- HDU 6153 A Secret
- hdu-6153 A Secret
- hdu-6153 A Secret
- HDU 6153 A Secret
- hdu--6153(A Secret)
- hdu 6153 A Secret KMP
- HDU 6153 A Secret KMP
- HDU 6153 A Secret KMP
- Hdu 6153 A Secret【KMP】
- 【KMP】HDU 6153 A Secret
- HDU 6153 A Secret(KMP)
- hdu 6153 A Secret (kmp)
- hdu 6153 A Secret (KMP)
- hdu 6153 A Secret(KMP)
- hdu 6153 A Secret(KMP)
- HDU 6153 A secret(kmp)
- HDU 6153-A Secret(kmp&&ccpc)
- HDU 6153 A Secret(KMP)
- 微服务架构的优势与不足
- mybatis Generator 配置详解
- Oracle数据库中的时间日期函数的使用
- 【BigHereo 7】-----面 试 再 总 结
- nutch1.6安装使用中错误解决方法
- HDU 6153 A Secret
- 小明学c++系列之第一篇:网络篇
- Python Web入门:Django学习与实践一
- 交叉编译工具库添加
- 微信小程序开发(十五)获取位置
- webpack学习资料
- Cookie 与 网络通信
- 机器学习与神经网络(一):人工神经网络模型简介
- Java Server Page九大内置对象