Match:Seek the Name, Seek the Fame(POJ 2752)

               

                追名逐利

  题目大意:给定一个字符串S,要你找到S的所有前缀后缀数组

  还是Kmp的Next数组的简单应用,但是这一题有一个BUG,那就是必须输出字符串的长度(不输出就WA),然而事实上对于abcbab,这样输出会是2,6,很明显是错,但是答案还是会判对,吃惊

  

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <functional>
 4 #include <string.h>
 5 
 6 using namespace std;
 7 
 8 static char str[400010];
 9 static int _Next[400010], store[400010];
10 
11 void Get_Next(const int);
12 
13 int main(void)
14 {
15     int Length, store_len;
16     while (~scanf("%s", str))
17     {
18         Length = strlen(str);
19         Get_Next(Length);
20         store_len = 0;
21 
22         //if (_Next[Length] >= Length / 2)
23         //    store[store_len++] = Length;
24         store[store_len++] = Length;
25         for (int k = _Next[Length]; k > 0; k = _Next[k])
26             store[store_len++] = k;
27         for (int i = store_len - 1; i >= 0; i--)
28             printf("%d ", store[i]);
29         printf("\n");
30     }
31     return EXIT_SUCCESS;
32 }
33 
34 void Get_Next(const int Length)
35 {
36     int i = 0, k = -1;
37     _Next[0] = -1;
38 
39     while (i < Length)
40     {
41         if (k == -1 || str[i] == str[k])
42         {
43             i++;
44             k++;
45             _Next[i] = k;
46         }
47         else k = _Next[k];
48     }
49 }

  

posted @ 2016-02-04 17:36  PhiliAI  阅读(350)  评论(0编辑  收藏  举报