您的位置:首页 > 其它

bzoj 3670: [Noi2014]动物园 KMP

2016-02-20 11:08 399 查看
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3670

[b]题解:[/b]

对于每一位,先求出它的next和que数组

que表示的是对于字符串的前i个字符所构成的子串,需要多少次k=next[k]可以得到k=0:que[i]=que[next[i]]+1;

之后再用next数组往下匹配k=next[k],当i-k>k+1时,num[i]=que[k]

这道题就搞完了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 1000010
#define mod 1000000007
using namespace std;
typedef long long ll;
ll ans;
int n;
int a
;
int next
,que
;
char str
;

int main()
{
int tot;
cin>>tot;
while(tot--)
{
scanf("%s",str+1);
int n=strlen(str+1);
for(int i=1;i<=n;i++)a[i]=str[i]-'a'+1;
int k=0,p=0;
ans=1,que[1]=1;
for(int i=2;i<=n;i++)
{
while(k>0&&a[i]!=a[k+1])k=next[k];
if(a[i]==a[k+1])k++;
next[i]=k;
que[i]=que[k]+1;
while(p>0&&(a[i]!=a[p+1]||i-p<=p+1))p=next[p];
if(a[i]==a[p+1])p++;
ans=(ll)ans*(que[p]+1)%mod;
}
cout<<ans<<endl;
memset(str,0,sizeof(str));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bzoj noi kmp