首页 > 其他 > 详细

Luogu P2375 [NOI2014]动物园

时间:2019-09-12 13:28:02      阅读:61      评论:0      收藏:0      [点我收藏+]


Luogu P2375 [NOI2014]动物园

解析

  • KMP 的神奇应用

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int N=1e6+5;
const LL mod=1e9+7;
int n,p[N];
char ch[N];
LL ans,num[N];
LL kmp(char s[])
{
    int j=0,len=strlen(s+1);
    num[1]=1;
    for(int i=1;i<len;i++)
    {
        while(j!=0&&s[i+1]!=s[j+1]) j=p[j];
        if(s[i+1]==s[j+1]) j++;
        p[i+1]=j;
        num[i+1]=num[p[i+1]]+1;
    }
    j=0;
    LL res=1;
    for(int i=1;i<len;i++)
    {
        while(j!=0&&s[i+1]!=s[j+1]) j=p[j];
        if(s[i+1]==s[j+1]) j++;
        while(j>((i+1)>>1)) j=p[j];
        res=(res*(num[j]+1))%mod;
    }
    return res;
}
int main()
{
    scanf("%d",&n);
    while(n--)
    {
        cin>>(ch+1);
        ans=kmp(ch);
        printf("%lld\n",ans);
    }
    return 0;
}

Luogu P2375 [NOI2014]动物园

原文:https://www.cnblogs.com/Hawking-llfz/p/11511037.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!