您的位置:首页 > 理论基础 > 计算机网络

HDU-6153---A Secret (扩展kmp)(2017ccpc网络赛)

2017-08-20 10:22 483 查看

A Secret

[align=center]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 803    Accepted Submission(s): 315

[/align]

[align=left]Problem Description[/align]
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.

 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
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.
 

[align=left]Sample Input[/align]

2
aaaaa
aa
abababab
aba

 

[align=left]Sample Output[/align]

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.

 
题意:给出两个字符串,求第二个字符串的每个后缀在第一个字符串中出现的次数与后缀长度的乘积和;

思路:当时比赛硬是没想到啊,把两个字符串都反转一下然后扩展kmp求出extand数组,这里需要好好理解extand数组表示的是什么,然后对于每个extend求(1~extand)的和就行;

举个例子:第二组样例翻转后为

str1:babababa

str2:aba

extand:03030301;

extand[1]=3;表示从str1[1]开始能与str2的前三个字符匹配,所以在这个点a,ab,aba都出现了一次,次数乘以长度的和便为1+2+3,讲到这应该是很好懂了;

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define mod 1000000007
using namespace std;
const int K=1000005;
int nt[K],extand[K];
char S[K],T[K];
//***********模板**********
void Getnext(char *T,int *next)
{
int len=strlen(T),a=0;
next[0]=len;
while(a<len-1 && T[a]==T[a+1]) a++;
next[1]=a;
a=1;
for(int k=2; k<len; k++)
{
int p=a+next[a]-1,L=next[k-a];
if( (k-1)+L >= p)
{
int j = (p-k+1)>0 ? (p-k+1) : 0;
while(k+j<len && T[k+j]==T[j]) j++;
next[k]=j;
a=k;
}
else
next[k]=L;
}
}

void GetExtand(char *S,char *T,int *next)
{
Getnext(T,next);
int slen=strlen(S),tlen=strlen(T),a=0;
int MinLen = slen < tlen ? slen : tlen;
while(a<MinLen && S[a]==T[a]) a++;
extand[0]=a;
a=0;
for(int k=1; k<slen; k++)
{
int p=a+extand[a]-1, L=next[k-a];
if( (k-1)+L >= p)
{
int j= (p-k+1) > 0 ? (p-k+1) : 0;
while(k+j<slen && j<tlen && S[k+j]==T[j]) j++;
extand[k]=j;
a=k;
}
else
extand[k]=L;
}
}
//*************
long long add(long long int x)
{
long long ans;
ans=((((1+x)%mod)*(x%mod))/2)%mod;//开始这里少加了一个括号wa了一发
return ans;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",S,T);
int len1=strlen(S);
int len2=strlen(T);
reverse(S,S+len1);
reverse(T,T+len2);
GetExtand(S,T,nt);
long long ans=0;
for(int i=0; i<len1; i++)
{
if(extand[i])
ans=(ans+add(extand[i])%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: