您的位置:首页 > 其它

A Secret(HDU 6153 扩展KMP)

2017-09-21 15:27 344 查看

A Secret

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

[/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.

 

//题意:给定母串str1,匹配串str2,str2[i-n]在str1中出现的次数为num[i],str2[i-n]的长度为len[i],求∑ (num[i]*len[i])  (0<=i<=n,n为strlen(str2)-1) 。

//思路:显然,如果str1里的某一段和str2里的某一段能匹配的话,将str1和str2逆序后,这两段还是能够匹配的。所以,将str1和str2同时逆序,那么现在就是str2的所有前缀去str1里匹配。还有1个点就是,如果str1里有个子串为“abc”,str2里也有一个前缀为“abc”,那么这个前缀能产生的价值就是(1+2+3)=(3+1)*3/2,因为“abc”能去匹配“abc”,同时“bc”能去匹配“bc”,“c”能去匹配“c”,所以,假设这个前缀能匹配的长度为m的话,产生的价值就是m*(m+1)/2
。而前缀能匹配的长度可以通过扩展KMP来求。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include
bcf1
<algorithm>

using namespace std;

const int MAX=1e6+10;
const int mod=1e9+7;

char str1[MAX],str2[MAX];
int NEXT[MAX],EXTEND[MAX];

void GetNext(char *s, int *Next)
{
int i = 0, j, po, len = strlen(s);
Next[0] = len;
while (s[i] == s[i + 1] && i + 1 < len)
i++;
Next[1] = i;
po = 1;
for (i = 2; i < len; i++)
{
if (Next[i - po] + i < Next[po] + po)
Next[i] = Next[i - po];
else
{
j = Next[po] + po - i;
if (j < 0)
j = 0;
while (i + j < len&&s[j] == s[i + j])
j++;
Next[i] = j;
po = i;
}
}
}

//扩展KMP模板,s1是母串,s2是匹配串
void ExKmp(char *s1, char *s2, int *Next, int *extend)
{
int i = 0, j, po, len1 = strlen(s1), len2 = strlen(s2);
GetNext(s2,Next);
while (s1[i] == s2[i] && i < len2&&i < len1)
i++;
extend[0] = i;
po = 0;
for (i = 1; i < len1; i++)
{
if (Next[i - po] + i < extend[po] + po)
extend[i] = Next[i - po];
else
{
j = extend[po] + po - i;
if (j < 0)
j = 0;
while (i + j < len1&&j < len2&&s1[i + j] == s2[j])
j++;
extend[i] = j;
po = i;
}
}
}

int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str1);
scanf("%s",str2);
int len1=strlen(str1);
int len2=strlen(str2);
memset(NEXT,0,sizeof(NEXT));
memset(EXTEND,0,sizeof(EXTEND));
//逆序一开始自己写TLE了(首尾互换),用reverse()函数就可以...
reverse(str1,str1+len1);
reverse(str2,str2+len2);
ExKmp(str1,str2,NEXT,EXTEND);
long long sum=0;
for(int i=0;i<len1;i++)
{
if(EXTEND[i]>0)
sum=(sum+(1LL*EXTEND[i]*(EXTEND[i]+1)/2)%mod)%mod;
}
printf("%lld\n",sum%mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: