标签:
Description
Input
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
Source
1 /* 2 登科后 3 唐代 4 孟郊 5 6 昔日龌龊不足夸,今朝放荡思无涯。 7 春风得意马蹄疾,一日看尽长安花。 8 */ 9 #include <iostream> 10 #include <cstdio> 11 #include <algorithm> 12 #include <cstring> 13 #include <vector> 14 #include <utility> 15 #include <iomanip> 16 #include <string> 17 #include <cmath> 18 #include <queue> 19 #include <assert.h> 20 #include <map> 21 #include <ctime> 22 #include <cstdlib> 23 #include <stack> 24 #define LOCAL 25 const int MAXN = 400000 + 10; 26 const int INF = 100000000; 27 const int SIZE = 450; 28 const int MAXM = 1000000 + 10; 29 const int maxnode = 0x7fffffff + 10; 30 using namespace std; 31 int l1, l2; 32 char a[MAXN]; 33 int next[MAXN];//不用开太大了.. 34 int Ans[MAXN]; 35 void getNext(){ 36 //初始化next数组 37 next[1] = 0; 38 int j = 0; 39 for (int i = 2; i <= l1; i++){ 40 while (j > 0 && a[j + 1] != a[i]) j = next[j]; 41 if (a[j + 1] == a[i]) j++; 42 next[i] = j; 43 } 44 return; 45 } 46 /*int kmp(){ 47 int j = 0, cnt = 0; 48 for (int i = 1; i <= l2; i++){ 49 while (j > 0 && a[j + 1] != b[i]) j = next[j]; 50 if (a[j + 1] == b[i]) j++; 51 if (j == l1){ 52 cnt++; 53 j = next[j];//回到上一个匹配点 54 } 55 } 56 return cnt; 57 }*/ 58 59 /*void init(){ 60 scanf("%s", a + 1); 61 scanf("%s", b + 1); 62 l1 = strlen(a + 1); 63 l2 = strlen(b + 1); 64 }*/ 65 66 int main(){ 67 int T; 68 69 while (scanf("%s", a + 1) != EOF){ 70 int tot = 0; 71 l1 = strlen(a + 1); 72 getNext(); 73 int tmp = next[l1]; 74 if (tmp != 0) Ans[tot++] = tmp; 75 while (next[tmp] != 0){ 76 tmp = next[tmp]; 77 Ans[tot++] = tmp; 78 } 79 for (int i = tot - 1; i >= 0; i--) printf("%d ", Ans[i]); 80 printf("%d\n", l1); 81 } 82 /*scanf("%s", a + 1); 83 l1 = strlen(a + 1); 84 getNext(); 85 for (int i = 1; i <= l1; i++) printf("%d" , next[i]);*/ 86 return 0; 87 }
【POJ2752】【KMP】Seek the Name, Seek the Fame
标签:
原文地址:http://www.cnblogs.com/hoskey/p/4333433.html