首页 > 其他 > 详细

HDU3336 Count the string

时间:2017-10-09 13:52:03      阅读:286      评论:0      收藏:0      [点我收藏+]

居然一A了,说明对朴素的KMP还是有一定理解。

主要就是要知道next数组的作用,然后就可以计算每个i结尾的满足题意的串个数。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<memory.h>
using namespace std;
char b[200010];
int Next[200010],L,ans;
void _next()
{
    int i,k,tmp;
    Next[1]=0;
    for(i=2,k=0;i<=L;i++){
         while(k&&b[i]!=b[k+1])k=Next[k];
         if(b[i]==b[k+1]) {
           k++;
         }
         Next[i]=k;
         tmp=Next[i];
         while(tmp) {
                ans++;
                tmp=Next[tmp];
         }//计算以i结尾的个数 
         ans%=10007;
    }
}
int main()
{
    int T,i;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&L);
        ans=0;
        scanf("%s",b+1);
        _next();
        printf("%d\n",(ans+L)%10007);
    }
    return 0;
}

 

HDU3336 Count the string

原文:http://www.cnblogs.com/hua-dong/p/7640546.html

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