HDU6513/CCPC2017--A Secret(KMP)
A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total Submission(s): 461 Accepted Submission(s): 182
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.
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.
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.
Sample Input
2 aaaaa aa abababab aba
Sample Output
13 19
Hint
case 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中國大學生程序設計競賽 - 網絡選拔賽
題意:
給出兩個字符串S1,S2,求S2的所有後綴在S1中匹配到的次數與其後綴長度的乘積之和
思路:
由於乘上了後綴長度,所以對某個後綴的匹配而言,每個字母對答案的貢獻是1
由此,我們可以將字符串反轉,然後跑一遍kmp,在kmp的過程中統計有多少匹配,並加上他們的貢獻即可
代碼:
1 /* 2 * @FileName: D:\代碼與算法\2017訓練比賽\CCPC網絡賽\1004-bt.cpp 3 * @Author: Pic 4 * @Date: 2017-08-19 16:30:09 5 * @Last Modified time: 2017-08-19 20:54:48 6 */ 7 #include <bits/stdc++.h> 8 using namespace std; 9 /* 10 * next[]的含義: x[i-next[i]...i-1]=x[0...next[i]-1] 11 * next[i]為滿足x[i-z...i-1]=x[0...z-1]的最大z值(就是x的自身匹配) 12View Code*/ 13 long long sum=0; 14 const long long mod=1e9+7; 15 const long long MAXN=2e6+30; 16 char x[MAXN],y[MAXN]; 17 int nxt[MAXN]; 18 void kmp_pre(char x[],int m) 19 { 20 long long i, j; 21 j = nxt[0] = -1; 22 i = 0; 23 while (i < m) 24 { 25 while (-1 != j && x[i] != x[j])j = nxt[j]; 26 nxt[++i] = ++j; 27 } 28 } 29 /* 30 * kmpNext[]的意思: next‘[i]=next[next[...[next[i]]]] (直到next‘[i]<0或者 31 x[next‘[i]]!=x[i]) 32 * 這樣的預處理可以快一些 33 */ 34 void KMP_Count( char x[],int m, char y[],int n) 35 { //x是模式串, y是主串 36 long long i, j; 37 kmp_pre(x,m); 38 i = j = 0; 39 while (i < n) 40 { 41 while (-1 != j && y[i] != x[j]) { 42 sum = (sum+((1+j)*j/2)%mod)%mod; 43 j = nxt[j]; 44 } 45 i++; j++; 46 //sum=(sum+j)%mod; 47 if (j >= m) 48 { 49 sum = (sum+((1+j)*j/2)%mod)%mod; 50 j = nxt[j]; 51 } 52 } 53 while(j>=1){ 54 sum = (sum+((1+j)*j/2)%mod)%mod; 55 j=nxt[j]; 56 } 57 } 58 int main() 59 { 60 // clock_t startTime,endTime; 61 //startTime = clock(); 62 //freopen("data.in","r",stdin); 63 //freopen("out.txt","w",stdout); 64 int t; 65 scanf("%d",&t); 66 while(t--){ 67 scanf("%s%s",y,x); 68 int len1=strlen(y); 69 int len2=strlen(x); 70 reverse(x,x+len2); 71 reverse(y,y+len1); 72 sum=0; 73 KMP_Count(x,len2,y,len1); 74 printf("%I64d\n",sum); 75 } 76 //endTime = clock(); 77 //cout << "Totle Time : " <<(double)(endTime - startTime)<< "ms" << endl; 78 return 0; 79 }
HDU6513/CCPC2017--A Secret(KMP)