HDU-3746 Cyclic nacklace (minimum follow link kpm)

Source: Internet
Author: User

CC always becomes very depressed at the end of this month, he had checked his credits card yesterday, without any SURPRI SE, there is only 99.9 yuan left. He is too distressed and thinking on how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU Cakeman", he wants to sell some little things Of course, this is not a easy task.

As Christmas is around the corner, Boys be busy in choosing Christmas presents to send to their girlfriends. It is believed this chain bracelet is a good choice. However, things is isn't always so simple, as was 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 are that it must be C Onnected by a cyclic chain which means the color of pearls is cyclic connected from the left to right. And the cyclic count must is 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 was 9 and its cyclic count is 2:

Now CC have brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make charmbracelets so th At 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's to say, a Dding to the middle is forbidden.
CC is satisfied with the ideas and ask you.

Input
The first line of the input was a single integer T (0 < T <=) which means the number of the 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 is kinds by ' a ' ~ ' Z ' Pearls of being described. The length of the string Len: (3 <= Len <= 100000).
Output
For each case, you is required to output the minimum count of pearls added to make a charmbracelet.
Sample Input
3
Aaa
Abca
Abcde
Sample Output
0
2
5

To give you a string of strings to make it a periodic loop the string needs to be added a few more
(for example, AAA cycle period of 1 does not need to add ABA minimum cycle period is 2 so to add a B)

Analysis: So it is to seek the minimum cycle.
minimum Follow-up link =len-next[len];
if the len% (Len-next[len]) ==0 instructions do not need to be added, they will meet the requirements of the topic
Then the function that only needs next to jump the table can be AC
AC Code

 #include <stdio.h> #include <string.h> struct KMP {char a[1000000];
    int nextval[1000000];
    int m;
    void Init () {memset (nextval,0,sizeof (nextval));
            } void Get_next () {for (int i=1;i<m;i++) {int j=nextval[i];
            while (J&&a[j]!=a[i]) j=nextval[j];
        nextval[i+1]=a[i]==a[j]?j+1:0;
}}} T;
    int main () {int n;
    scanf ("%d", &n);
        while (n--) {scanf ("%s", T.A);
        T.m=strlen (T.A);
        T.get_next ();
        if (T.NEXTVAL[T.M]) printf ("%d\n", t.m% (T.M-T.NEXTVAL[T.M]) ==0?0:t.m-t.nextval[t.m]-t.m% (T.M-T.NEXTVAL[T.M]));
    else printf ("%d\n", T.M); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.