Cyclic NacklaceTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total submission (s): 4063 Accepted Submission (s): 1830
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 Input3aaaabcaabcde
Sample Output025
Authorpossessor WC Sourcehdu 3rd "Vegetable-birds Cup" programming Open Contest Recommendlcy | We have carefully selected several similar problems for you:1358 2222 3068 2896 3744 |
Test instructions: give you a string to the tail can connect (this sentence no confusion) ask you to add a few characters, will be composed of two loops of the string
For example, ABCA in the last BC is a string of two loops
Problem Solving Ideas:
This topic mainly examines the use of the next array in the KMP ~next array is used to save the longest common prefix, so we only ask for the longest common prefix l with the length of the string l%l to get the rest and then use L minus the rest of the beads to be added at least.
AC Code:
#include <stdio.h> #include <string.h> #include <algorithm> #define MAXN 100000+10using namespace std; Char p[maxn];int pre[maxn];void getnext (int n) {int i=0,j=-1;pre[0]=-1;while (i<n) {if (j==-1| | P[i]==p[j]) {pre[++i]=++j;} else j=pre[j];}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%s", p), int l=strlen (p); GetNext (l); int l=l-pre[l];//Circular section length if (l %l==0&&l!=l) {printf ("0\n"); continue;} int res=l-l%l; printf ("%d\n", res);} return 0;}
Cyclic NacklaceTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total submission (s): 4063 Accepted Submission (s): 1830
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 Input3aaaabcaabcde
Sample Output025
Authorpossessor WC Sourcehdu 3rd "Vegetable-birds Cup" programming Open Contest Recommendlcy | We have carefully selected several similar problems for you:1358 2222 3068 2896 3744 |
Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.
Hdoj Cyclic nacklace 3746 "KMP"