(KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746

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

Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4498    Accepted Submission(s): 2051

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. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl‘s fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls‘ lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet‘s cycle is 9 and its cyclic count is 2:

Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden.
CC is satisfied with his ideas and ask you for help.

Input

The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by ‘a‘ ~‘z‘ characters. The length of the string Len: ( 3 <= Len <= 100000 ).

Output

For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.

Sample Input

3

aaa

abca

abcde

Sample Output

0
2
5

#include<stdio.h>
#include<string.h>
#define N 1000007

char S[N];
int Next[N];

void FindNext(int Slen)
{
    int i=0, j=-1;
    Next[0] = -1;

    while(i<Slen)
    {
        if(j==-1 || S[i]==S[j])
            Next[++i] = ++j;
        else
            j = Next[j];
    }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%s", S);

        int i, Slen = strlen(S), w;

        FindNext(Slen);

        w = Slen - Next[Slen];///w为循环节的长度

        if(w==Slen) ///如果Next[Slen]为0,也就是前Slen位没有循环节,则输出字符串的长度
            printf("%d\n", Slen);
        else if(Slen%w)
            printf("%d\n", w-Slen%w);
        else
            printf("0\n");

    }
    return 0;
}

时间: 2024-10-19 18:27:20

(KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746的相关文章

Cyclic Nacklace HDU 3746 KMP 循环节

Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len]-len的差即是循环部分的长度. 这个是重点.这个题目自己开始没有想明白,看的博客,推荐这个. 代码实现 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const i

Cyclic Nacklace HDU - 3746

给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. 没有优化的next数组的应用. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<string> #define maxn 100010 using namespace std; char p[maxn]; int nex[maxn],n; void get_ne

模板题 + 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 + 求最小循环节 --- POJ 2406 Power Strings

Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Accepted* Submission Date: 20

KMP + 求最小循环节 --- HDU 1358 Period

Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期. analyse: 还是next数组的运用,详见上一篇博客. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Acce

poj 1961 KMP运用之循环节的问题

#include<stdio.h> #include<string.h> #include<iostream> using namespace std; char str[1000010]; int next[1000010]; int get_next(int len) { next[0]=-1; int i=0,k=-1; while(i<len) { if(k==-1||str[i]==str[k]) { i++; k++; next[i]=k; } els

8.2 kmp 扩展kmp

假设一母串S,子串P KMP:用于求解子串P在母串S中第一次出现的位置,或是在母串S中出现的次数.(最长公共前缀后缀) next数组的含义:next[i]表示前面长度为i的子串中,前缀和后缀相等的最大长度. 拓展kmp是对KMP算法的扩展,它解决如下问题:(最长公共前缀) 定义母串S,和子串T,设S的长度为n,T的长度为m,求T与S的每一个后缀的最长公共前缀,也就是说,设extend数组,extend[i]表示T与S[i,n-1]的最长公共前缀,要求出所有extend[i](0<=i<n).

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

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