【NOI 2014】 动物园

【题目链接】

             https://www.lydsy.com/JudgeOnline/problem.php?id=3670

【算法】

           KMP

【代码】

              

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000010
const int P = 1e9 + 7;

int T,i,j,ans,len,tmp;
int num[MAXN],nxt[MAXN],cnt[MAXN];
char s[MAXN];

int main() {
        
        scanf("%d",&T);
        while (T--)
        {
                scanf("%s",s+1);
                len = strlen(s+1);
                nxt[1] = 0;
                cnt[1] = 1;
                j = 0;
                for (i = 2; i <= len; i++)
                {
                        tmp = nxt[i-1];
                        while (tmp && s[tmp+1] != s[i]) tmp = nxt[tmp];
                        if (s[tmp+1] == s[i]) 
                        {
                                nxt[i] = tmp + 1;
                                cnt[i] = cnt[tmp+1] + 1;
                        }
                        else 
                        {
                                nxt[i] = 0;
                                cnt[i] = 1;
                        } 
                        while (j && s[j+1] != s[i]) j = nxt[j];
                        if (s[j+1] == s[i]) j++;
                        while (j > i / 2) j = nxt[j];
                        num[i] = cnt[j];
                }        
                ans = 1;
                for (i = 1; i <= len; i++) ans = 1ll * ans * (num[i] + 1) % P;
                printf("%d\n",ans);
        }
        
        return 0;
    
}

 

转载于:https://www.cnblogs.com/evenbao/p/9253891.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值