題目連接:http://acm.hdu.edu.cn/showproblem.php?pid=3746php
Cyclic Nacklace
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4372 Accepted Submission(s): 1986
html
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
Sample Output
給你一個字符串,讓你求出使此串至少循環兩次要添加的字符數量。
如:
aaa不須要添加任何字符,因此輸出0
abca,循環節認爲是abc,那麼至少要循環2次,則須要額外添加2個字符。
abcde,無循環節,所以重複本串,就是5個字符。
下面說下思路:
如同POJ2752的那個題同樣,仍是考察對於pre(next)數組的理解,(POJ2752題題解:http://www.cnblogs.com/vincentX/p/4758339.html)
咱們取最後一個pre值pre[n]=k,它表明的含義爲整個字符串前k個字符和後k個字符相等。那麼咱們能夠用整串的長度減去pre[n],也就是減掉最後的那pre[k]個字符,那麼獲得的是該串循環節的長度。
這麼想就很好理解了,咱們接下來看一下樣例:
"aaa" pre[n] = 2,即前2個字符和後2個字符相等(注:pre[n] = n = 3的時候是沒有意義的),咱們用總長減去pre[n],即 3 - 2 = 1,剩下的串(循環節)爲"a"
"abca" pre[n] = 1,即第一個字符和最後一個字符相等,4 - 1 =3,剩下的串爲 "abc"
"abcde" pre[n] = 0,循環節是5,整串看做循環節 "abcde"
先考慮如何求添加的字符的個數:
咱們添加字符是以最後pre[n]個字符爲標準的,由於他們不在循環節內,添加的字符數必定小於等於循環節(loop)的長度(若是pre[n]=0的話,那麼就把整個串再添加一遍),那麼通常狀況下,去掉現有的pre[n],再須要額外添加的字符就是:
loop - (pre[n] % loop)
特別地,若是串自己不須要添加任何字符,即:循環至少兩次。除了保證串長能夠被循環節整除之外,還應保證串長不該該等於循環節長度。
n != loop && n % loop == 0
代碼以下:
1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <algorithm>
5 #include <iostream>
6 #include <cmath>
7 #include <queue>
8 #include <map>
9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 const int maxn = 100010;
16 int na, nb;
17 char a[maxn];
18 char b[maxn];
19 int pre[maxn];
20
21 //b是模式串,a是目標串
22 void getpre(char *b, int *pre) {
23 int j, k;
24 pre[0] = -1;
25 j = 0;
26 k = -1;
27 while(j < nb) {
28 if(k == -1 || b[j] == b[k]) {
29 j++;
30 k++;
31 pre[j] = k;
32 }
33 else {
34 k = pre[k];
35 }
36 }
37 }
38
39 int kmp() {
40 int ans = 0;
41 int i = 0;
42 int j = 0;
43 getpre(b, pre);
44 while(i < na) {
45 if(j == -1 || a[i] == b[j]) {
46 i++;
47 j++;
48 }
49 else {
50 j = pre[j];
51 }
52 if(j == nb) {
53 ans++;
54 }
55 if(i == na) {
56 return ans;
57 }
58 }
59 }
60
61 int main() {
62 freopen("in", "r", stdin);
63 int T;
64 scanf("%d", &T);
65 while(T--) {
66 scanf("%s", b);
67 nb = strlen(b);
68 getpre(b,pre);
69 int loop = nb - pre[nb];
70 if(nb != loop && nb % loop == 0) {
71 printf("0\n");
72 }
73 else {
74 printf("%d\n", loop - (pre[nb] % loop));
75 }
76 }
77 }