标签:
求一个字符串所有的相同前后缀
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <stack> 5 using namespace std; 6 7 const int N = 400010; 8 int next[N]; 9 char T[N]; 10 int tlen; 11 stack<int> st ; 12 13 void getNext() 14 { 15 int j, k; 16 j = 0; k = -1; next[0] = -1; 17 while(j < tlen) 18 if(k == -1 || T[j] == T[k]) 19 next[++j] = ++k; 20 else 21 k = next[k]; 22 23 } 24 25 26 int main() 27 { 28 while (cin>>T) 29 { 30 tlen = strlen(T) ; 31 getNext() ; 32 int t = next[tlen] ; 33 while (t) 34 { 35 st.push(t) ; 36 t = next[t] ; 37 } 38 while(!st.empty()) 39 { 40 int u = st.top() ; 41 printf("%d " , u) ; 42 st.pop() ; 43 } 44 printf("%d\n" , tlen) ; 45 } 46 47 48 49 return 0; 50 }
标签:
原文地址:http://www.cnblogs.com/-Buff-/p/4502150.html