首页 > 其他 > 详细

hdu 3746 Cyclic Nacklace(KMP)

时间:2015-09-03 23:17:50      阅读:283      评论:0      收藏:0      [点我收藏+]

题意:

求最少需要在后面补几个字符能凑成两个循环。

分析:

最小循环节的应用,i-next[i]为最小循环节。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int f[N];
char str[N];
void getnext(int n){
    int i=0,j=-1;
    f[0]=-1;
    while(i<n){
        if(j==-1||str[i]==str[j]){
            i++;
            j++;
            f[i]=j;
        }
        else j=f[j];
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",str);
        int len=strlen(str);
        getnext(len);
        int tmp=len-f[len];
        if(tmp==len)
            printf("%d\n",len);
        else  if(len%tmp==0)
            printf("0\n");
         else
            printf("%d\n",tmp-len%tmp);
    }
return 0;
}

 

hdu 3746 Cyclic Nacklace(KMP)

原文:http://www.cnblogs.com/zsf123/p/4780771.html

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