Cyclic Nacklace ---hdu3746(循环节,kmp)

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

给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串。

abca---添加bc,成为abcabc

abcd---添加abcd,成为abcdabcd

aa---无需添加

经典的求最小循环节。

首先给出结论:一个字符串的最小循环节为:len-next[len]

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;

const int N = 1e5+7;

char s[N];
int Next[N], n;

void GetNext()
{
    int i=0, j=-1;
    Next[0] = -1;
    while(i<n)
    {
        if(j==-1 || s[i] == s[j])
            Next[++i] = ++j;
        else
            j=Next[j];
    }
}
int main()
{
    int ans, T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%s", s);
        n = strlen(s);
        GetNext();
        ans = n - Next[n];
        if(Next[n] == 0)
        {
            printf("%d\n", n);
            continue;
        }
        if(n%ans==0)///判断是否已经不需要添加了;
            ans = 0;
        else
            ans = ans - Next[n] % ans;///ans = 2*ans-n;
        printf("%d\n", ans);
    }
    return 0;
}

时间: 2024-08-08 18:50:33

Cyclic Nacklace ---hdu3746(循环节,kmp)的相关文章

HDU 3746 Cyclic Nacklace 环形项链(KMP,循环节)

题意:给一个字符串,问:要补多少个字符才能让其出现循环?比如abc要补3个变成abcabc.若已经循环,输出0. 思路:KMP的next数组解决.如果已经出现循环,那么答案为0.但是不循环呢?要根据next[len]来断定.我们要用最少字符来补上使其循环,而我们所知的就是要令循环节为k=len-next[len]这么长,即串开头的这么长.补到串长为k的倍数为止,此时,k就是循环节了. 其实答案可以直接算的,我用个循环来找而已.直接算应该是len%k后剩下末尾那小串的长度,补k-len%k这么多个

D - Cyclic Nacklace HDU3746 (kmp 计算字符串最小循环节 )

D - Cyclic Nacklace Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are on

D - Cyclic Nacklace HDU-3746

题目大意: 给一个t,接下来每个t,给一个串,求出最小循环节下还要增加多少个珠子才完美.(要是都没啥循环节,就输出长度) 解题思路: 求出最小循环节 cir:cir=len - next[len] (关于为什么是这个式子详见<KMP 专题知识>),然后拿len%cir得到的余数就是已经有的,那么拿循环节再减掉有的,就是需要增加的.即:cir-len%cir.如果len%cir == 0,而且cir!=len,就意味着一颗都不用增加,因为此时已经是完美的循环了,但是要是cir==len,就说明没

UVAlive 3026 KMP 最小循环节

KMP算法: 一:next数组:next[i]就是前面长度为i的字符串前缀和后缀相等的最大长度,也即索引为i的字符失配时的前缀函数. 二:KMP模板 1 /* 2 pku3461(Oulipo), hdu1711(Number Sequence) 3 这个模板 字符串是从0开始的 4 Next数组是从1开始的 5 */ 6 #include <iostream> 7 #include <cstring> 8 using namespace std; 9 10 const int m

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

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

Problem 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. Bein

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