您的位置:首页 > 其它

HDU - 6153——A Secret

2017-09-04 20:15 288 查看


A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)

Total Submission(s): 2423    Accepted Submission(s): 892


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中国大学生程序设计竞赛 - 网络选拔赛

改个一个晚上,才发现 应该使用long long ,泪奔

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int LEN=1e6+8;
const int mod=1e9+7;
long long kmp_next[LEN],ans[LEN];
char x1[LEN],x2[LEN];
void KMP_next(char *a)
{
int i=0,j=-1;
kmp_next[0]=-1;
while(a[i])
if(j==-1||a[i]==a[j])
kmp_next[++i]=++j;
else
j=kmp_next[j];
}
void KMP(char *a,char *b)
{
int i=0,j=0,m=strlen(b);
while(a[i])
{
if(j==-1||a[i]==b[j])
++i,++j,ans[j]++;
else
j=kmp_next[j];
if(j==m)
j=kmp_next[j];
}
}
int main()
{
int N;
cin>>N;
while(N--)
{
scanf("%s%s",x1,x2);
int n=strlen(x1),m=strlen(x2);
reverse(x1, x1 + strlen(x1));
reverse(x2, x2 + strlen(x2));
memset(ans,0,sizeof(ans));
KMP_next(x2);
KMP(x1,x2);
long long ANS=0;
for(int i=m; i>0; i--)
ans[kmp_next[i]]+=ans[i];
for(int i=1; i<=m; i++)
ANS=(ANS+ans[i]*i)%mod;
printf("%lld\n",ANS);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: