51NOD 1554 欧姆诺姆和项链 巧妙利用KMP
1 #include<cstdio> 2 #define N 1000100 3 char s[N]; 4 int n,k,nxt[N],ans[N]; 5 int main() 6 { 7 scanf("%d%d%s",&n,&k,s+1); 8 for (int i=2,j=0;i<=n;i++) 9 { 10 while (j>0 && s[j+1]!=s[i]) j=nxt[j]; 11 if (s[j+1]==s[i]) j++; 12 nxt[i]=j; 13 } 14 for (int i=1;i<=n;i++) 15 { 16 int cnt=i/(i-nxt[i]); 17 if (i%(i-nxt[i])==0) 18 if (cnt/k>=cnt%k) 19 ans[i]=1; 20 else; 21 else 22 if (cnt/k>cnt%k) 23 ans[i]=1; 24 } 25 for (int i=1;i<=n;i++) 26 putchar(ans[i]+'0'); 27 return 0; 28 }