HDU 6153A Secret(kmp)
来源:互联网 发布:2站域名www.tt69.com 编辑:程序博客网 时间:2024/05/29 20:02
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 803 Accepted Submission(s): 315
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.
Source
2017中国大学生程序设计竞赛 - 网络选拔赛
想法:
kmp变形
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;#define ll long longconst int mn=1e6+5;int n,m;int next1[mn];char s[mn],t[mn];ll id[mn];const int mod=1e9+7;void get_Next(){ int i=0,j=-1; next1[0]=-1; while(i<m) { if(j==-1||t[i]==t[j]) next1[++i]=++j; else j=next1[j]; }}void KMP(){ int i=0,j=0; while(i<n) { if(j==-1||s[i]==t[j]) ++i,++j; else j=next1[j]; id[j]++; if(j==m) j=next1[j]; }}int main(){ int T; scanf("%d",&T); while(T--) { memset(id,0,sizeof(id)); scanf("%s%s",s,t); n=strlen(s),m=strlen(t); reverse(s,s+n); reverse(t,t+m); get_Next(); KMP(); ll ans=0; for(int i=m;i>0;i--) { id[next1[i]]+=id[i]; ans=(ans+id[i]*i)%mod; } printf("%lld\n",ans); } return 0;}
代码二:
exkmp
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const int mod = 1000000007;int cnt[1000005];inline ll Add(ll n){ ll m=((n%mod)*((n+1)%mod)/2)%mod; return m;}char t[1000005],p[1000005];int Next[1000005],ex[1000005];void pre(char p[]){ int m=strlen(p); Next[0]=m; int j=0,k=1; while(j+1<m&&p[j]==p[j+1]) j++; Next[1]=j; for(int i=2; i<m; i++) { int P=Next[k]+k-1; int L=Next[i-k]; if(i+L<P+1) Next[i]=L; else { j=max(0,P-i+1); while(i+j<m&&p[i+j]==p[j]) j++; Next[i]=j; k=i; } }}void exkmp(char p[],char t[]){ int m=strlen(p),n=strlen(t); pre(p); int j=0,k=0; while(j<n&&j<m&&p[j]==t[j]) j++; ex[0]=j; for(int i=1;i<n;i++) { int P=ex[k]+k-1; int L=Next[i-k]; if(i+L<P+1) ex[i]=L; else { j=max(0,P-i+1); while(i+j<n&&j<m&&t[i+j]==p[j]) j++; ex[i]=j; k=i; } }}int main(){ int ss; scanf("%d",&ss); while(ss--) { scanf("%s %s",&t,&p); int Ti=strlen(t); int Pi=strlen(p); reverse(t,t+Ti); reverse(p,p+Pi); pre(p); memset(Next, 0,sizeof(Next)); memset(ex, 0, sizeof(ex)); exkmp(p,t); ll ans=0; for(int i=0; i<Ti;++i) { if(ex[i]) { //printf("%d\t",ex[i]); ans=(ans+Add(ex[i])%mod)%mod; } } printf("%d\n",ans%mod); } return 0;}
阅读全文
0 0
- 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)
- HDU 6153 A Secret(扩展KMP)
- A Secret(HDU 6153 扩展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
- 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)
- 关于servlet的一些总结
- 函数exec
- 如何在HTML中使用JavaScript
- IMDB 5000 Movie Dataset 数据分析学习记录
- HDU-6153---A Secret (扩展kmp)(2017ccpc网络赛)
- HDU 6153A Secret(kmp)
- 【hdu 2063 过山车 二分图】
- 《数据库SQL实战》查找employees表
- 日常工作积累
- poj 3259 Wormholes 【最短路之负权环的判断】
- web学习笔记18-spring使用
- 命名空间别名的问题
- Party
- 剑指offer:孩子们的游戏(圆圈中最后剩下的数)