题目描述
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
Sample Output
13 19Hintcase 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 这种算法!!!
之后是 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;
}