Cyclic NacklaceTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4124 Accepted Submission (s): 1866
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
Circular section related issues of proof of circulation section related issues portal
Test instructions: To make a bracelet into a loop greater than or equal to two, ask for at least a few beads to add
Idea: ask for the minimum cycle section, and then see if it is the whole number of loops, not the words of a few to make up a few of the minimum follow-up link t = Len-next[len]
#include <stdio.h> #include <string> #include <cstring> #include <cmath> #include <algorithm > #include <iostream>using namespace Std;char s[100009];int nt[100009];void getnext (int len) { int k=-1;int j=0; Nt[0]=-1; while (J<len) { if (k<0 | | s[k]==s[j]) nt[++j]=++k; else k=nt[k];} } int main () { int T; scanf ("%d", &t); while (t--) { scanf ("%s", s); int Len=strlen (s); GetNext (len); if (nt[len]==0) { printf ("%d\n", Len); Continue; } for (int i=0;i<=len;i++)// cout<<next[i]<< ""; int T=len-nt[len]; if (len%t==0) { puts ("0"); Continue; } t=t-len%t; printf ("%d\n", t); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 3746 Cyclic nacklace KMP Cyclic section problem