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
Source
2017中国大学生程序设计竞赛 - 网络选拔赛
题目大意
求一个字符串T每一个后缀在另一个字符串S中出现次数与其长度的乘积之和。
解题思路
将两个字符串反转,再使用扩展KMP求得字符串S每个后缀与字符串T的最长公共前缀,假设extend[i]=t,则代表字符串T的 T1、T1T2、……T1T2...Tt 均在字符串S中出现了一次,则对答案贡献了 1+2+……t=t∗(t+1)2 。所以最后枚举一次extend[]数组求和便可得最终答案。
代码实现
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 1000007
#define mod 1000000007
#define ll long long
int nex[maxn],extend[maxn];
char S[maxn],T[maxn];
void GetNext(char *T,int m,int nex[])
{
int a=0,p=0;
nex[0]=m;
for(int i=1;i<m;i++)
{
if(i>=p||i+nex[i-a]>=p)
{
if (i>=p)
p=i;
while(p<m&&T[p]==T[p-i])
p++;
nex[i]=p-i;
a=i;
}
else
nex[i]=nex[i-a];
}
}
void GetExtend(char *S,int n,char *T,int m,int extend[],int nex[])
{
int a=0,p=0;
GetNext(T,m,nex);
for(int i=0; i<n; i++)
{
if (i>=p||i+nex[i-a]>=p)
{
if(i>=p)
p=i;
while(p<n&&p-i<m&&S[p]==T[p-i])
p++;
extend[i]=p-i;
a=i;
}
else
extend[i]=nex[i-a];
}
}
int main()
{
int n, m;
int t;
scanf("%d%*c",&t);
while(t--)
{
gets(S);
gets(T);
n=strlen(S);
m=strlen(T);
reverse(S,S+n);
reverse(T,T+m);
GetExtend(S, n, T, m, extend, nex);
ll ans=0;
for(int i=0; i<n; i++)
ans=((1LL*extend[i]*(extend[i]+1)/2)%mod+ans)%mod;
printf("%lld\n",ans);
}
return 0;
}