KMP求最短循环节的应用
//2100 KB 218 ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define M 100000+1000 char str[M]; int next2[M]; int len; void getnext() { len=strlen(str); int i=0,j; j=next2[0]=-1; while(i<len) { while(j!=-1&&str[i]!=str[j]) j=next2[j]; j++; i++; next2[i]=j; } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",str); getnext(); int ans=(len-next2[len])-len%(len-next2[len]); if(len%(len-next2[len])==0&&len/(len-next2[len])>1) printf("0\n"); else printf("%d\n",ans); } return 0; }
时间: 2024-10-11 07:01:04