Hdu 6153 A Secret【KMP】
来源:互联网 发布:电影制作软件 编辑:程序博客网 时间:2024/06/06 00:46
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 1062 Accepted Submission(s): 406
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
1319Hintcase 2: Suffix(S2,1) = "aba",Suffix(S2,2) = "ba",Suffix(S2,3) = "a".N1 = 3,N2 = 3,N3 = 4.L1 = 3,L2 = 2,L3 = 1.ans = (3*3+3*2+4*1)%1000000007.
题目大意:
求串2的后缀在串1中出现的次数*后缀长度的总和。
思路:
①我们首先将两个字符串倒过来,然后再最末尾各加一个不同的字符。
②然后考虑KMP匹配的过程中,每拓展一个长度的匹配,就会多匹配长度的价值。
③如果我们失败匹配了,那么j=next【j】之后,我们发现之前匹配上的部分还需要加上len*(len+1)/2的价值,过程维护一下就行了。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define ll __int64int nex[1200005];char a[1500000];char b[1500000];int lena,lenb;ll ans;ll mod=1e9+7;void set_naxt()//子串的next数组{ int i=0,j=-1; nex[0]=-1; while(i<lenb) { if(j==-1||b[i]==b[j]) { i++; j++; nex[i]=j; } else j=nex[j]; }}int KMP(){ int i=0,j=0; set_naxt(); while(i<lena) { if(j==-1||a[i]==b[j]) { // printf("%d %d-------\n",i,j); ans+=j+1; ans%=mod; i++;j++; } else { if(j==0)i++; else { j=nex[j]; // printf("%d %d--\n",i,j); ans+=(ll)j*(j+1)/2; ans%=mod; } } }}int main(){ int t; scanf("%d",&t); while(t--) { ans=0; scanf("%s%s",a,b); lena=strlen(a); lenb=strlen(b); reverse(a,a+lena); reverse(b,b+lenb); a[lena]='X'; lena++;a[lena]='\0'; b[lenb]='Y'; lenb++;b[lenb]='\0'; KMP(); printf("%I64d\n",ans); }}
阅读全文
0 0
- 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||扩展KMP】
- hdu 6153 A Secret KMP&&扩展KMP
- HDU 6153-A Secret(kmp&&ccpc)
- HDU 6153 A Secret(KMP)
- HDU 6153 A Secret (扩展KMP)
- HDU 6153A Secret(kmp)
- HDU 6153 A Secret 经典扩展KMP
- python动态加载子模块 根据字符串绑定子模块 如加载os.path
- 学习笔记之orcle
- JavaScript版—贪吃蛇小组件
- MYSQL updatexml报错注入
- 2017ccpc网络选拔赛 1005 CaoHaha's staff
- Hdu 6153 A Secret【KMP】
- 来自CSDN的互联网相关知识图谱汇总(含知识结构图)
- Servlet组件学习总结
- Java注释详解-Java文档注释生成Java API文档
- 平衡二叉树
- 9.3 概率分布
- 【ZZULIOJ】 1792 有点Low的时光机
- Jmeter之性能测试工具知识点汇总
- Dubbo源码介绍