POJ 2752 Seek the Name, Seek the Fame

题意:找整个串的前缀与后缀相等,从小到大输出

解题思路:Next[]数组的运用,只要回溯Next[]数组就行了,你自己写个例子一下子就懂了(本人懒,就不帮你写了)

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #define MAXSIZE 1000100
 7 using namespace std;
 8 
 9 char a[MAXSIZE];
10 int Next[MAXSIZE];
11 int Res[MAXSIZE];
12 int Len, Len_Res;
13 
14 int GetNext()
15 {
16     int i = 0, j = Next[0] = -1;
17     while (i < Len)
18     {
19         if (j == -1 || a[i] == a[j])
20             Next[++i] = ++j;
21         else
22             j = Next[j];
23     }
24 }
25 
26 void GetRes()
27 {
28     Len_Res = 0;
29     int p = Len;
30     while (p)
31     {
32         Res[Len_Res++] = p;
33         p = Next[p];
34     }
35 }
36 
37 int main(void)
38 {
39     ios::sync_with_stdio(false);
40     while (cin >> a)
41     {
42         Len = strlen(a);
43         GetNext();
44         GetRes();
45         for (int i = Len_Res - 1; i >= 0; --i)
46             cout << Res[i] << ' ';
47         cout << endl;
48     }
49 
50     return 0;
51 }

猜你喜欢

转载自www.cnblogs.com/ducklu/p/8962074.html