A Secret
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 1666 Accepted Submission(s): 614
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
2
aaaaa
aa
abababab
aba
aaaaa
aa
abababab
aba
Sample Output
13
19
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.
扩展kmp 模板题
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define mod 1000000007
typedef long long ll;
int T;
char s[],t[];
int extend[],nex[];
void pre_ex_kmp(char x[],int m,int nex[])
{
nex[]=m;
int j=;
while(j+<m&&x[j]==x[j+])
j++;
nex[]=j;
int k=;
for(int i=; i<m; i++)
{
int p=nex[k]+k-;
int L=nex[i-k];
if(i+L<p+)
nex[i]=L;
else
{
j=max(,p-i+);
while(i+j<m&&x[i+j]==x[j])
j++;
nex[i]=j;
k=i;
}
}
}
void ex_kmp(char x[],int m,char y[],int n,int nex[],int extend[])
{
pre_ex_kmp(x,m,nex);
int j=;
while(j<n&&j<m&&x[j]==y[j])
j++;
extend[]=j;
int k=;
for(int i=; i<n; i++)
{
int p=extend[k]+k-;
int L=nex[i-k];
if(i+L<p+)
extend[i]=L;
else
{
j=max(,p-i+);
while(i+j<n&&j<m&&y[i+j]==x[j])
j++;
extend[i]=j;
k=i;
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
memset(extend,,sizeof(extend));
memset(nex,,sizeof(nex));
scanf("%s",s);
scanf("%s",t);
int len1=strlen(s);
int len2=strlen(t);
reverse(s,s+len1);
reverse(t,t+len2);
ex_kmp(t,len2,s,len1,nex,extend);
ll ans=;
ll n;
for(int i=; i<len1; i++)
{
if(extend[i])
{
n=extend[i]%mod;
ans=ans+(n*(n+)/)%mod;
ans%=mod; }
}
cout<<ans<<endl;
}
return ;
}