A Secret HDU - 6153(扩展kmp)

题目链接:https://vjudge.net/problem/HDU-6153

这个题的思路很好,做这个题时想了好长时间也不能很好的利用扩展kmp数组的含义去解决这个题目。后来,还是看的题解,他们的思路是真好,我太菜想不到,所以写一篇博客来记录一下。

                                                             A Secret

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.

 

题意:求一个字符串的所有后缀在母串中的出现次数*后缀的长度的总和。

思路:将两个串倒过来

用这时的b串的前缀去匹配a的后缀

这时候在k处匹配的结果就是以k为终点,有最长可以匹配向后到extend[k]长度

对于每一个点等差数列求和 累加(在k点出会有extand[k]个字符串(包含k点)匹配,长度依次是extand[k],extand[k]-1,....,1)。

 只要明确了这个思路,就好做了,剩下的是扩展kmp的模板,具体见代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
int T;
const int INF=1e6+5;
char s1[INF],s2[INF];
ll nxt[INF],ex[INF];
const ll Max=1e9+7;
ll ans;
void get_next(){
	ll i,j,k,po;
	ll n=strlen(s2);
	nxt[0]=n;
	for(i=0;(i+1)<n&&s2[i]==s2[i+1];i++);
	nxt[1]=i;po=1;
	for(i=2;i<n;i++)
	{
		if(nxt[i-po]+i<nxt[po]+po){
			nxt[i]=nxt[i-po];
		}
		else {
			j=nxt[po]+po-i;
			if(j<0)j=0;
			for(;(i+j)<n&&s2[j]==s2[i+j];j++);
			nxt[i]=j;
			po=i;
		}
	}
}
void extand()
{
	get_next();
	ll i,j,po;
	ll n1=strlen(s1);
	ll n2=strlen(s2);
	for(i=0,j=0;i<n2&&j<n1&&s1[j]==s2[i];i++,j++);
	ex[0]=i;po=0;
	for(i=1;i<n1;i++)
	{
		if(nxt[i-po]+i<ex[po]+po){
			ex[i]=nxt[i-po];
		}
		else{
			j=ex[po]+po-i;
			if(j<0)j=0;
			for(;j<n2&&(i+j)<n1&&s1[i+j]==s2[j];j++);
			ex[i]=j;
			po=i;
		}
	}
}
int main()
{
	cin>>T;
	while(T--)
	{
		scanf("%s",s1);
		scanf("%s",s2);
		ll l1=strlen(s1);
		ll l2=strlen(s2);
		reverse(s1,s1+l1);
		reverse(s2,s2+l2);
		memset(nxt,0,sizeof(nxt));
		memset(ex,0,sizeof(ex));
		extand();	
		ans=0;
		for(ll i=0;i<l1;i++)//求和 
		{
			ll c=ex[i];
			ans=(ans+(1ll*(c+1)*c/2)%Max)%Max;		
		}
		cout<<ans<<endl;
	}
	return 0;
}

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值