(KMP 1.4) hdu 3746 Cyclic nacklace (use next array to find the length of the loop section--How many characters a string needs to be added to make the number of loop sections of the string >=2)

Source: Internet
Author: User

Topic:

Cyclic NacklaceTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3387 Accepted Submission (s): 1549


Problem DESCRIPTIONCC becomes very depressed at the end of this month, he had checked his credit card yesterday, WI Thout any surprise, 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.
Inputthe first line of the input was a single integer T (0 < T <=) 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 is kinds by ' a ' ~ ' Z ' Pearls of being described. The length of the string Len: (3 <= Len <= 100000).
Outputfor each case, you is required to output the minimum count of pearls added to make a charmbracelet.
Sample Input
3aaaabcaabcde

Sample Output
025

Authorpossessor WC
Sourcehdu 3rd "Vegetable-birds Cup" programming Open Contest
Recommendlcy | We have carefully selected several similar problems for you:3336 1358 2222 3068 2896


Main topic:

For a given string, how many characters need to be added to >=2 the number of loop sections of the string.


Topic Analysis:

Kmp. Simple question.


This question needs to be kept in mind in the following points:

1, Len-next[len]: the length of the minimum loop section of the string

2, if len% (Len-next[len]) = = 0, indicating that the string consists of a circular section, then the number of circular section is len/(Len-next[len]).



The code is as follows:

/* * hdu3746.cpp * * Created on:2015 April 18 * author:administrator * * #include <iostream> #include <algorithm& GT, #include <cstdio> #include <cstring>using namespace std;const int maxn = 100001;int m;//The length of the target string char pattern [maxn];//mode string int nnext[maxn];//next array. Direct from next may be the same as the predetermined name in the system./*o (m) time for next array */void Get_next () {m = strlen (pattern); Nnext[0] = nnext[1] = 0;for (int i = 1; i < m; i++) {int J = nnext[i];while (J && pattern[i]! = pattern[j]) j = Nnext[j];nnext[i + 1] = pattern[i] = = Pattern[j]? j + 1:0;}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%s", pattern), get_next (); int len = strlen (pattern);//Calculate the length of the pattern string/ * * If the string is already made up of multiple loop sections. * len% (Len-nnext[len]) = = 0: The string consists of a looping section * (len! = Len-nnext[len]): The length of the string is not equal to the length of the looping section * *-----> The string itself has been composed of multiple loop sections */if (len % (Len-nnext[len]) = = 0 && (len! = Len-nnext[len])) {printf ("0\n");//The number of characters that need to be added at this time is 0}else{/** * Len-nnext[len]: Length of the link * len% (Len-nnext[len]: The length of the part of the string that is not the loop section */int ans = (Len-nnext[len])-(Len% (Len-nnext[len]));//calculates the number of characters that need to be added printf ("%d\n", ans);}} return 0;}






(KMP 1.4) hdu 3746 Cyclic nacklace (use next array to find the length of the loop section--How many characters a string needs to be added to make the number of loop sections of the string >=2)

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.