HDU 6153 A Secret 扩展kmp
来源:互联网 发布:日式家具 知乎 编辑:程序博客网 时间:2024/06/06 06:58
题目链接:HDU6153
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 1413 Accepted Submission(s): 521
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.
题意:问t串的每个后缀在s串中出现的次数*后缀长度之和
题目分析:之前用ac自动机发现需要把所有后缀都扔到字典树里,显然会超时,然后变通一下把s和t串都翻转过来就变成了普通的后缀和匹配问题,跑一下kmp或者扩展kmp就行了。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 1000010;char str2[N],str1[N];int nexts[N],extand[N];char s[N],t[N];int T;int mod=1e9+7;int aa[1000010];void init(){ aa[0]=0; aa[1]=1; for(int i=2;i<=1000000;i++) { aa[i]=aa[i-1]+i; aa[i]%=mod; }}void getnext(char *T){// next[i]: 以第i位置开始的子串 与 T的公共前缀 int i,length = strlen(T); nexts[0] = length; for(i = 0;i<length-1 && T[i]==T[i+1]; i++); nexts[1] = i; int a = 1; for(int k = 2; k < length; k++){ int p = a+nexts[a]-1, L = nexts[k-a]; if( (k-1)+L >= p ){ int j = (p-k+1)>0? (p-k+1) : 0; while(k+j<length && T[k+j]==T[j]) j++; nexts[k] = j, a = k; } else nexts[k] = L; }}void getextand(char *S,char *T){ memset(nexts,0,sizeof(nexts)); getnext(T); int Slen = strlen(S), Tlen = strlen(T), a = 0; int MinLen = Slen>Tlen?Tlen:Slen; while(a<MinLen && S[a]==T[a]) a++; extand[0] = a, a = 0; for(int k = 1; k < Slen; k++){ int p = a+extand[a]-1, L = nexts[k-a]; if( (k-1)+L >= p ){ int j = (p-k+1)>0? (p-k+1) : 0; while(k+j<Slen && j<Tlen && S[k+j]==T[j] ) j++; extand[k] = j;a = k; } else extand[k] = L; }}int main(){ scanf("%d",&T); init(); while(T--) { scanf("%s %s",s,t); int len2=strlen(t); for(int i=0;i<len2;i++) { str2[i]=t[len2-1-i]; } str2[len2]='\0'; int len1=strlen(s); for(int i=0;i<len1;i++) { str1[i]=s[len1-1-i]; } str1[len1]='\0'; getextand(str1,str2); long long int ans=0; for(int i = 0; i < len1; i++) { ans+=aa[extand[i]]; ans%=mod; } printf("%lld\n",ans); }}
阅读全文
0 0
- HDU 6153 A Secret【KMP||扩展KMP】
- hdu 6153 A Secret KMP&&扩展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)
- A Secret(HDU 6153 扩展KMP)
- HDU 6153 A Secret(KMP/扩展KMP)
- hdu 6153 A Secret(kmp||扩展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
- Hdu 6153 A Secret【KMP】
- lucene
- 【转载】10个最佳ES6特性
- Midland.Valley.Move.v2017.2.0.build.21565结构建模
- [LeetCode 318] Maximum Product of Word Lengths(Python)
- MonkeyDev
- HDU 6153 A Secret 扩展kmp
- C++11:std::function
- java多线程编程-volatile与synchronized之前的比较
- CentOS7网络配置
- android 部分驱动修改的编译指令
- QT5模块
- expdp/impdp (ORACLE 11.2.0.4)
- SVN服务器搭建和使用(二)
- 深度解析队列操作