HDU 3746 Cyclic Nacklace(KMP)

KMP求最短循环节的应用

//2100 KB	218 ms
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define M 100000+1000
char str[M];
int next2[M];
int len;
void getnext()
{
    len=strlen(str);
    int i=0,j;
    j=next2[0]=-1;
    while(i<len)
    {
        while(j!=-1&&str[i]!=str[j]) j=next2[j];
        j++;
        i++;
        next2[i]=j;
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",str);
        getnext();
        int ans=(len-next2[len])-len%(len-next2[len]);
        if(len%(len-next2[len])==0&&len/(len-next2[len])>1) printf("0\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-11 07:01:04

HDU 3746 Cyclic Nacklace(KMP)的相关文章

HDU 3746 Cyclic Nacklace (KMP最小循环节)

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11264    Accepted Submission(s): 4821 Problem Description CC always becomes very depressed at the end of this month, he has check

HDU 3746 Cyclic Nacklace(KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

hdu 3746 Cyclic Nacklace(kmp循环节)

题意:循环节 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using namespace std; #define MaxSize 100005 int _next[MaxSize]; void GetNext(char t[]){//求next数组 int j,k,len; j=0; k=-1; _next[0]=-1; len=strlen(t); while(j<len){ i

HDU - 3746 Cyclic Nacklace (KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

hdu 3746 Cyclic Nacklace(KMP)

题意: 求最少需要在后面补几个字符能凑成两个循环. 分析: 最小循环节的应用,i-next[i]为最小循环节. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <strin

hdu 3746 Cyclic Nacklace(求最少添加几个字符使得字符串是由n(n&gt;=2)个循环节组成的)

代码: #include<cstdio> #include<cstring> using namespace std; char a[100005]; int next[100005]; int LCPS[100005]; int n,m; void GetLCPS() { int j=0; int k=-1; int len=strlen(a); next[0]=-1; while(j<len) { if(k==-1||a[k]==a[j]) { LCPS[j++]=++k

hdu 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i

模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. 例: abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 analyse: 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len]. 证明: 举个例子:abcab

(KMP 1.4)hdu 3746 Cyclic Nacklace(使用next数组来求循环节的长度——求一个字符串需要添加多少个字符才能使该字符串的循环节的个数&gt;=2)

题目: Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3387    Accepted Submission(s): 1549 Problem Description CC always becomes very depressed at the end of this month, he has che