hdu6153(kmp) A Secret
来源:互联网 发布:qq群发广告软件 编辑:程序博客网 时间:2024/06/05 01:03
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0
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.【大佬】的题解,借大佬的题解补一下题目#include<cstdio>#include<iostream>#include<cstring>using namespace std;#define ll long longconst int mn=1e6+5;int n,m,nextval[mn];char s[mn],t[mn];ll id[mn];const int mod=1e9+7;void getNextval() { int i=0,j=-1; nextval[0]=-1; while(i<m) { if(j==-1||t[i]==t[j]) nextval[++i]=++j; else j=nextval[j]; }}void KMP() { int i=0,j=0; while(i<n) { if(j==-1||s[i]==t[j]) ++i,++j; else j=nextval[j]; id[j]++; if(j==m) j=nextval[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); for(int i=0;i<=(n-1)/2;++i) swap(s[i],s[n-1-i]); for(int i=0;i<=(m-1)/2;++i) swap(t[i],t[m-1-i]); getNextval(); KMP(); ll ans=0; for(int i=m;i>0;--i) { id[nextval[i]]+=id[i]; ans=(ans+id[i]*i)%mod;} printf("%lld\n",ans); } return 0;}
【KMP的原样】:(我自己搜出来的,请指教啊)#include <stack>#include <vector>#include <set>#include <map>#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<string>using namespace std;#define MAX_SIZE 100010typedef char ElemType;void CptPfFunc(ElemType Pattern[], int PrefixFunc[]);void KMPstrMatching(ElemType Target[], ElemType Pattern[]);char p[MAX_SIZE];char t[MAX_SIZE];int cnt; ///记录匹配的的个数int main(){ int re; scanf("%d",&re); while(re--){ printf("please input string t:"); cin>>t;// t.length=strlen(t.string); printf("please input string p:"); cin>>p;// p.length=strlen(p.string);// getnext(p,next);// printf("\n%d\n",kmp(t,p,next)); cnt=0; KMPstrMatching(t,p); } return 0;}/// Compute Prefix functionvoid CptPfFunc( ElemType Pattern[], int PrefixFunc[] ){ register int iLen = 0; /// Length of Pattern[] while('\0'!=Pattern[iLen]) iLen++; int LOLP=0; /// Lenth of longest prefix PrefixFunc[1]=0; for(int NOCM=2;NOCM<iLen+1;NOCM++) /// NOCM represent the number of characters matched { while(LOLP>0&&(Pattern[LOLP]!=Pattern[NOCM-1])) LOLP=PrefixFunc[LOLP]; if(Pattern[LOLP]==Pattern[NOCM-1]) LOLP++; PrefixFunc[NOCM]=LOLP; }}void KMPstrMatching(ElemType Target[],ElemType Pattern[]){ int PrefixFunc[MAX_SIZE]; register int TarLen=0; register int PatLen=0; /// Compute the length of array Target and Pattern while('\0'!=Target[TarLen]) TarLen++; while('\0'!=Pattern[PatLen]) PatLen++; /// Compute the prefix function of Pattern CptPfFunc(Pattern,PrefixFunc); int NOCM=0; /// Number of characters matched for(int i=0;i<TarLen;i++ ) { while(NOCM>0&&Pattern[NOCM]!=Target[i]) NOCM=PrefixFunc[NOCM]; if(Pattern[NOCM]==Target[i]) NOCM++; if(NOCM==PatLen ) { cout<<"KMP String Matching,pattern occurs with shift "<<i-PatLen+1<<endl; cnt++; NOCM=PrefixFunc[NOCM]; } } cout<<"cnt=: "<<cnt<<endl;}
阅读全文
0 0
- hdu6153(kmp) A Secret
- hdu6153(kmp) A Secret
- [kmp] hdu6153 A Secret
- hdu6153-A Secret (kmp/扩展kmp)
- hdu6153 A Secret (拓展KMP)
- HDU6153-A Secret ex-kmp
- HDU6153 A Secret 扩展KMP
- 【HDU6153】A Secret(扩展KMP)
- [扩展kmp] hdu6153 A Secret
- HDU6153 A Secret(扩展KMP)2017中国大学生程序设计竞赛
- hdu6153 A Secret CCPC1004 扩展KMP
- HDU6153-A Secret
- HDU6153-A Secret
- hdu6153-exkmp&&脑洞-A Secret
- HDU6153(KMP)
- HDU 6153-A Secret(kmp&&ccpc)
- HDU 6153 A Secret(KMP)
- HDU 6153 A Secret (扩展KMP)
- spring mvc 结果跳转方式
- 超强IIS站点工具一键设置PHP,支持多个PHP同时运行
- css基础选择器和盒模型
- NJU 1017 [JSCPC2016]Heresy 莫比乌斯反演
- 腺体类
- hdu6153(kmp) A Secret
- 响应式布局
- Jobdu1448 Legal or Not (拓扑排序)
- 正则匹配模式汇总
- 进程&线程
- 最强自定义PHP集成环境,系统缺失dll和vc也能正常运行
- 套接字的阻塞模式和非阻塞模式
- 最新php环境搭建,2017年最新PHP环境搭建
- 我们到底该选择一个什么性质的公司,对我们职业生涯有帮助