[关闭]
@Chilling 2017-07-23T20:17:14.000000Z 字数 931 阅读 912

HUST-1010: The Minimum Length

KMP


题目描述

There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.

输入

Multiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.

输出

For each line, output an integer, as described above.

样例输入

bcabcab
efgabcdefgabcde

样例输出

3
7

题意:求一个串的最小循环节。


  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e6 + 5;
  4. char s[maxn];
  5. int Next[maxn];
  6. void getNext(char s[])
  7. {
  8. int len = strlen(s);
  9. Next[0] = -1;
  10. int j = 0, k = -1;
  11. while(j < len)
  12. {
  13. if(k == -1 || s[j] == s[k])
  14. Next[++j] = ++k;
  15. else
  16. k = Next[k];
  17. }
  18. }
  19. int main()
  20. {
  21. while(scanf("%s", s) != EOF)
  22. {
  23. int l = strlen(s);
  24. getNext(s);
  25. printf("%d\n", l - Next[l]);
  26. }
  27. return 0;
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注