hdu6153-A Secret (kmp/扩展kmp)
来源:互联网 发布:什么软件可以学鬼步舞 编辑:程序博客网 时间:2024/05/17 03:05
题目描述
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.
题意:
给出两组字符串(A串,B串),从A串中匹配出B串中长度为 L(1~len(B)) 的后缀出现的次数 Ls,最后再求和
L*Ls(L = 1~len(B)) 。
题解:
比赛时是用了 字典树 的求法,结果超时,事后看了题解才知道原来有 KMP 与 扩展KMP 这种算法!!!
关于 KMP : http://blog.csdn.net/joylnwang/article/details/6778316/
关于 扩展KMP : http://blog.csdn.net/dyx404514/article/details/41831947
之后是 AC代码:
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;const ll MOD = 1e9+7;char s1[1000010];char s2[1000010];int nx[1000010];int extend[1000010];/* 以下是 扩展KMP 部分 *//************************************************************/void getnx(char s[],int l){int i,j,po;i = 0;nx[0] = l;while(s[i]==s[i+1] && i+1<l)i++;nx[1] = i;po = 1;for(i=2;i<l;i++){if(nx[i-po]+i < nx[po]+po)nx[i] = nx[i-po];else{j = nx[po]+po-i;if(j < 0)j = 0;while(i+j<l && s[j]==s[j+i])j++;nx[i] = j;po = i;}}}void getextend(){int i,j,po;int l1,l2;l1 = strlen(s1);l2 = strlen(s2);getnx(s2,l2);i = 0;while(s1[i]==s2[i] && i<l2 && i<l1)i++;extend[0] = i;po = 0;for(i=1;i<l1;i++){if(nx[i-po]+i < extend[po]+po)extend[i] = nx[i-po];else{j = extend[po]+po-i;if(j < 0)j = 0;while(i+j<l1 && j<l2 && s1[i+j]==s2[j])j++;extend[i] = j;po = i;}}}/************************************************************/ll sum(ll n){return ((n+1)*n/2)%MOD;}int main(){int T;scanf("%d",&T);while(T--){memset(s1,0,sizeof(s1));memset(s2,0,sizeof(s2));memset(nx,0,sizeof(nx));memset(extend,0,sizeof(extend));scanf("%s",s1);scanf("%s",s2);int l1,l2;l1 = strlen(s1);l2 = strlen(s2);strrev(s1);strrev(s2);getextend();ll ans=0;int t;for(t=0;t<l1;t++){if(extend[t] != 0)ans = (ans+sum(extend[t]))%MOD;}printf("%lld\n",ans);}return 0;}
阅读全文
0 0
- hdu6153-A Secret (kmp/扩展kmp)
- HDU6153 A Secret 扩展KMP
- 【HDU6153】A Secret(扩展KMP)
- [扩展kmp] hdu6153 A Secret
- hdu6153(kmp) A Secret
- hdu6153(kmp) A Secret
- [kmp] hdu6153 A Secret
- hdu6153 A Secret CCPC1004 扩展KMP
- HDU6153 A Secret(扩展KMP)2017中国大学生程序设计竞赛
- hdu6153 A Secret (拓展KMP)
- HDU6153-A Secret ex-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/扩展KMP)
- hdu 6153 A Secret(kmp||扩展kmp)
- HDU 6153 A Secret【KMP||扩展KMP】
- Java知识:抽象类与接口
- 更新windows10系统PS无法打开,安装PS软件后无法直接拖动图片到ps里面的解决方案
- MYSQL 第一篇 MYSQL下载、启动、安装、设置编码集
- css文本溢出省略号 遮罩层
- 计算机网络
- hdu6153-A Secret (kmp/扩展kmp)
- MyBatis示例---存储过程
- SpringMVC详解(三)------基于注解的入门实例
- C++标准转换运算符reinterpret_cast
- JavaScript 表单验证
- 一分钟告诉你究竟DevOps是什么鬼?
- LintCode-[容易] 496. 玩具工厂
- Comparable接口和Comparator接口
- 激活函数