#HDU 3746 Cyclic Nacklace (KMP + fill cycle principle)

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

Subject to the effect: I Enter a string to determine the minimum number of characters to the left or to the right to add, to make him into N string loop consisting of

This question is resolved finished after reading someone else's, found that for this question more crucial step basically in passing, and some people may see the half-comprehended, so I wrote a little more points - 

Ideas: First, look at the sample to the data, just this question summarizes all cases: First, the input string is already a string cycle, such as "aaa", the answer is 0 Second, enter the string loop length of the entire string, such as "abcde", the answer is three whole length of the string, the string length of the input loop is> 1, but the suffix> 1 and <loop length, we mainly discuss this situation.

When it is not one or two either case, the string loop structure already exists but is not the last loop end of the loop, so we need only consider the last loop is like, take a look at the topic to sample: abca. Completion of KMP should know that when the operation to i, i - nxt [i] is far from 1 to i of the loop "should have" in length, and why? First, nxt array, the maximum length of the prefix and suffix is equal, he said, in other words, is that you look back and you from 1 from i look forward, if str [1] == str [i], 1 -> 2, i -> i - 1, until they are not equal, the number of operations at this time is the nxt [i], specifically how to realize we can look at this big brother blog, in a very detailed https: // blog .csdn.net / v_july_v / article / details / 7041827

The reason why the "should have" such a statement, because the last cycle and the third case is not over, or else he is the first case of direct output 0, so we need to do is to make the length of the last cycle of increase due to his length, that is, i - nxt [i], in this question, the length of the string is len - nxt [len], how much you need to add it? That there are two cases, the two figure out the situation, you know it! (Talk about a good understanding of written, concise wording will naturally after you read the QAQ)

For example the string "abababa", the length len = 7, nxt [7] = 5, the loop length = len - nxt [7] = 2, it is clear that the need to add a character on the OK;

Look a "abcdeab", length 7, NXT [7] = 2, the length of the loop 5 is necessary to add 3

For these two chestnuts, because the first loop has cycled many times, while the second loop cycle only once, so that all the cases are also taken into account. In order to prevent that the smaller the length of the loop occurs we add it to a length greater than his, and then after the completion of the addition the length of% total length of the string now, for the first, is (2 + 2 + 2 + 2 )% 7 == 1, for the second is (5 + 5)% 3 == 7, under which understand it, much more than look at him more than he.

Simpler wording: one, 2 - (7%), titanium, 5 - (7% 5).

AC Code:

 

#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5;

char str[maxn];
int nxt[maxn], len_, T;
void getnext() {
    nxt[0] = -1;
    int i = 0, j = -1;
    while (i < len_) {
        if (j == -1 || str[i] == str[j])
            nxt[++i] = ++j;
        else
            j = nxt[j];
    }
}

int main()
{
    scanf("%d", &T);
    while (T--) {
        memset(nxt, 0, sizeof(nxt));
        scanf("%s", str);
        len_ = strlen(str);
        getnext();
        if (nxt[len_] == 0) cout << len_ << endl;
        else if (len_ % (len_ - nxt[len_]) == 0) cout << 0 << endl;
        else cout << (len_ - nxt[len_]) - (len_ % (len_ - nxt[len_])) << endl;
    }
    return 0;
}

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_43851525/article/details/91650232