POJ_2752_KMP
http://poj.org/problem?id=2752
求字符串的字串,使前缀后缀都为这个字串,按字母数量排序输出数量。
用了KMP的未优化的next数组。
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; char a[400005]; int ans[400005],next[400005]; void get_next(int len) { int i = 0,j = -1; next[0] = -1; while(i < len) { if(j == -1 || a[i] == a[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } } int main() { while(~scanf("%s",a)) { int cnt = 0,len = strlen(a); int now = len; get_next(len); while(now != 0) { now = next[now]; ans[cnt++] = now; } for(int i = cnt-2;i >= 0;i--) printf("%d ",ans[i]); printf("%d\n",len); } return 0; }