HDU--3746--Cyclic Nacklace【KMP】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746

题意:在一个字符串后最少加几个字符才能使这个字符串是某个串重复两次而得。

思路:借助了这篇博文的结论:传送门

结论:len-next[i]为此字符串的最小循环节(i为字符串的结尾),另外如果len%(len-next[i])==0,此字符串的最小周期就为len/(len-next[i]);

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 10100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define LLINF 0x7FFFFFFFFFFFFFFF
#define seed 131
#define MOD 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

char str[100100];
int next[100100];
void getnext(){
    int i,j;
    i = 0, j = -1;
    int l = strlen(str);
    next[0] = -1;
    while(i<l){
        if(j==-1||str[i]==str[j]){
            i++;
            j++;
            next[i] = j;
        }
        else
            j = next[j];
    }
}
int main(){
    int i,j,t;
    scanf("%d",&t);
    while(t--){
        scanf("%s",str);
        getnext();
        int l = strlen(str);
        int x = l - next[l];
        if(next[l]==0)  printf("%d\n",l);
        else if(l%x==0) printf("0\n");
        else    printf("%d\n",x-l%x);
    }
    return 0;
}
时间: 2024-10-27 07:32:02

HDU--3746--Cyclic Nacklace【KMP】的相关文章

HDU3746 Cyclic Nacklace 【KMP】

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

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循环节)

题意:循环节 思路: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】

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

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求循环节)

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求最短循环节的应用 //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=n

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 (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