题意:就是要找出所有循环节的位置
思路:利用next失配数组实现跳转。长度为len-next(len)的前缀显然是符合题意的。
#include<stdio.h> #include<string.h> #include<algorithm> #define ll long long using namespace std; char str[1000010]; int nextt[1000010]; int ans[1000010]; void getnext(int len) { int i=0,j=-1; nextt[i]=j; while(i<len) { if(j==-1||str[i]==str[j]) { i++;j++; nextt[i]=j; } else j=nextt[j]; } } void solve() { scanf("%s",str); int n=strlen(str); for(int i=0;i<=n+1;i++) { nextt[i]=0; } getnext(n); int k=0; int nw=n; while(nw>0) { nw=nextt[nw]; ans[k++]=nw; } printf("%d ",k); for(int i=0;i<k-1;i++) { printf("%d ",n-ans[i]); } printf("%d ",n-ans[k-1]); } int main() { int t=1; scanf("%d",&t); int u=0; while(t--) { u++; printf("Case #%d: ",u); solve(); } }