POJ 2725
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #define MAXN 400005 5 using namespace std; 6 7 char _m[MAXN]; 8 int next[MAXN]; 9 void give_next(int len); 10 int ans_len[MAXN]; 11 int ans_len_index; 12 int ans[MAXN]; 13 bool take_judge(int b,int len); 14 int main() 15 { 16 //freopen("acm.acm","r",stdin); 17 string s; 18 int i; 19 int j; 20 int len; 21 int index; 22 int cur; 23 //int ans; 24 while(getline(cin,s)) 25 { 26 if(s.length() == 0) 27 { 28 break; 29 } 30 index = 0; 31 //cout<<s<<endl; 32 //ans = 0; 33 //cout<<s<<endl; 34 for(i = 0; i < s.length(); ++ i) 35 { 36 _m[i] = s[i]; 37 } 38 memset(next,-1,sizeof(next)); 39 give_next(s.length()); 40 41 i = s.length(); 42 //cout<<i<<" "; 43 ans[index ++] = i; 44 for(; next[i] != 0; i = next[i]) 45 { 46 //cout<<next[i]<<" "; 47 ans[index++] = next[i]; 48 } 49 for(i = index-1; i >= 1; -- i) 50 { 51 cout<<ans[i]<<" "; 52 } 53 cout<<ans[i]<<endl; 54 //cout<<endl; 55 // cout<<endl; 56 } 57 return 0; 58 } 59 60 bool take_judge(int b,int len) 61 { 62 int j = 0; 63 int i; 64 for(i = b; i < len; ++ i,++ j) 65 { 66 if(_m[i] != _m[j]) 67 { 68 break; 69 } 70 } 71 if(i == len) 72 { 73 return true; 74 } 75 return false; 76 } 77 78 void give_next(int len) //此为求next 数组的方法。 79 { 80 int i; 81 int j; 82 i = 0; 83 j = -1; 84 next[0] = -1; 85 while(i < len) 86 { 87 if(j == -1 || _m[i] == _m[j]) 88 { 89 i ++; 90 j ++; 91 next[i] = j; 92 } 93 else 94 j = next[j]; 95 } 96 }