标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14172 | Accepted: 7055 |
Description
Input
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 char a[400040]; int p[400040], lena, num[400040]; 6 void Getp() 7 { 8 int i = 0, j = -1; 9 p[i] = j; 10 while(i < lena) 11 { 12 //printf("1\n"); 13 if(j==-1 || a[i] == a[j]) 14 { 15 i++; j++; 16 p[i] = j; 17 } 18 19 else 20 j = p[j]; 21 } 22 } 23 int main() 24 { 25 while(~scanf("%s", a)) 26 { 27 lena = strlen(a); 28 Getp(); 29 int i, k = 0; 30 for(i = lena; i != 0;) 31 { 32 num[k++] = p[i]; 33 i = p[i]; 34 } 35 /* printf("%d\n", p[lena]);*/ 36 for(i = k-2; i >= 0; i--) 37 printf("%d ", num[i]); 38 printf("%d\n", lena); 39 } 40 return 0; 41 }
Poj2752--Seek the Name, Seek the Fame(Kmp → → Next数组应用)
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4713215.html