#include <iostream>
#include <cstring>
考查了对next数组的了解
using namespace std;
#define max 1000005
int next[max],l,ans[max];
char s[max];
void getNext(){
int j,k;
next[0]=-1;
j=0;k=-1;
while(j<l){if(k==-1||s[j]==s[k]) {
j++;
k++;
next[j]=k;
}
else
k=next[k];
}
}
int main(){
int t;
cin>>t;
int game=0;
while(t--){
game++;
cin>>s;
l=strlen(s);
getNext();
int t=next[l];
int cnt=0;
while(t){
ans[cnt++] = l-t;
t=next[t];
}
ans[cnt++] = l;
cout<<"Case #"<<game<<": "<<cnt<<endl;
for(int i = 0;i < cnt-1;i++)cout<<ans[i]<<" ";
cout<<ans[cnt-1]<<endl;
}
return 0;
}
时间: 2024-11-16 11:33:41