A Secret(HDU 6153 扩展KMP)
来源:互联网 发布:网络教育入学时间 编辑:程序博客网 时间:2024/06/13 10:09
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 2587 Accepted Submission(s): 955
Total Submission(s): 2587 Accepted Submission(s): 955
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.
//题意:给定母串str1,匹配串str2,str2[i-n]在str1中出现的次数为num[i],str2[i-n]的长度为len[i],求∑ (num[i]*len[i]) (0<=i<=n,n为strlen(str2)-1) 。
//思路:显然,如果str1里的某一段和str2里的某一段能匹配的话,将str1和str2逆序后,这两段还是能够匹配的。所以,将str1和str2同时逆序,那么现在就是str2的所有前缀去str1里匹配。还有1个点就是,如果str1里有个子串为“abc”,str2里也有一个前缀为“abc”,那么这个前缀能产生的价值就是(1+2+3)=(3+1)*3/2,因为“abc”能去匹配“abc”,同时“bc”能去匹配“bc”,“c”能去匹配“c”,所以,假设这个前缀能匹配的长度为m的话,产生的价值就是m*(m+1)/2 。而前缀能匹配的长度可以通过扩展KMP来求。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <algorithm>using namespace std;const int MAX=1e6+10;const int mod=1e9+7;char str1[MAX],str2[MAX];int NEXT[MAX],EXTEND[MAX];void GetNext(char *s, int *Next){ int i = 0, j, po, len = strlen(s); Next[0] = len; while (s[i] == s[i + 1] && i + 1 < len) i++; Next[1] = i; po = 1; for (i = 2; i < len; i++) { if (Next[i - po] + i < Next[po] + po) Next[i] = Next[i - po]; else { j = Next[po] + po - i; if (j < 0) j = 0; while (i + j < len&&s[j] == s[i + j]) j++; Next[i] = j; po = i; } }}//扩展KMP模板,s1是母串,s2是匹配串void ExKmp(char *s1, char *s2, int *Next, int *extend){ int i = 0, j, po, len1 = strlen(s1), len2 = strlen(s2); GetNext(s2,Next); while (s1[i] == s2[i] && i < len2&&i < len1) i++; extend[0] = i; po = 0; for (i = 1; i < len1; i++) { if (Next[i - po] + i < extend[po] + po) extend[i] = Next[i - po]; else { j = extend[po] + po - i; if (j < 0) j = 0; while (i + j < len1&&j < len2&&s1[i + j] == s2[j]) j++; extend[i] = j; po = i; } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%s",str1); scanf("%s",str2); int len1=strlen(str1); int len2=strlen(str2); memset(NEXT,0,sizeof(NEXT)); memset(EXTEND,0,sizeof(EXTEND)); //逆序一开始自己写TLE了(首尾互换),用reverse()函数就可以... reverse(str1,str1+len1); reverse(str2,str2+len2); ExKmp(str1,str2,NEXT,EXTEND); long long sum=0; for(int i=0;i<len1;i++) { if(EXTEND[i]>0) sum=(sum+(1LL*EXTEND[i]*(EXTEND[i]+1)/2)%mod)%mod; } printf("%lld\n",sum%mod); } return 0;}
阅读全文
1 0
- 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/扩展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+数学推导)
- HDU 6153-A Secret(kmp&&ccpc)
- HDU 6153 A Secret(KMP)
- HDU 6153A Secret(kmp)
- HDU 6153 A Secret(kmp)
- LeetCode编程练习
- HTML(三)— 综合实例
- 创建动态库时,建议使用的链接选项Bsymbolic
- numpy 碎碎念
- Constructor类概述与使用
- A Secret(HDU 6153 扩展KMP)
- 301重定向
- 【仿】新闻App练习
- 数据库的索引以及在哪些列上创建索引
- 最大连续子序列和-动态规划
- 频道管理和Tablayout
- 关于工作的一点感悟
- Lua—迭代器
- Spring AOP不拦截从对象内部调用的方法原因