ZeptoLab Code Rush 2015
D. Om Nom and necklace
Test instructions
Give a string s, to determine whether its various prefixes are Ababa ... ABA forms (both A and B can be empty, and A has q+1, B has q, q is given).
"The Official Puzzle"
For the prefix p, we can split it into p=ssss ... Sssst, where T is the prefix of S. Obviously, the KMP algorithm can be used, and the time complexity is O (n).
When t=s:p=sss ... S Suppose that S has appeared r times. If you convert to Ababab ... In the form of Ababa, A and b are composed of several s, and the final a must be a suffix of p. By the greedy algorithm, A's length is as small as possible, so a in s appears r mod q times. B in S appears r/q-r mod Q times. Because you just have to Judge R/q-r MoD q>=0.
When T!=s: Because the previous method is similar, a can be SSS ... ST, because its length is as small as possible, so we just need to determine whether R/q-r mod q>0.
There are two suspects,
- Why the length of a should be as small as possible
- Why is there a R mod q in a s,b with r/q-r mod q s
Because you want to p=ssss the string ... SS divided into Abababa ... ABA form (q+1 a a,q b), equivalent to Q-AB plus a, we make AB as long as possible, then the last a must be as small as possible.
So each AB has r/q s, and the extra r%q is attributed to a, so you can push and export B with a r/q-r%q s.
When T=s, B can be empty, so meet r/q-r%q>=0.
When T!=s, B can not be null (A is a prefix of AB, and T is a suffix of a, is a prefix of s), due to t!=s, at least some characters need to fill t into full S. So B can not be empty), this is to determine whether to meet r/q-r%q>0 can.
#include <bits/stdc++.h>#defineEPS 1e-9#definefor (i,j,k) for (int i=j;i<=k;i++)#defineMAXN 1000005#defineMAXM 40005#defineINF 0X3FFFFFFFusing namespaceStd;typedefLong LongLL;intI,j,k,n,m,x,y,t,big,cas,num,len;BOOLFlag;intNEXT[MAXN],ANS[MAXN];CharS[MAXN];intMain () {scanf ("%d%d",&n,&k); scanf ("%s", s); Len=strlen (s); next[0]=0; J=0; if(k==1) ans[0]=1; for(i=1; i<len;i++) {J=Next[i]; while(J && S[i]!=s[j]) j=Next[j]; if(S[i]==s[j]) j + +;Elsej=0; Next[i+1]=J; intsize=i+1-next[i+1]; if((i+1)%size==0) { intR= (i+1)/size; if(r/k>=r%k) ans[i]=1; }Else { intR= (i+1)/size; if(r/k>r%k) ans[i]=1; } } for(i=0; i<len;i++) printf ("%d", Ans[i]); printf ("\ n"); return 0;}
Codeforces 526d-om Nom and Necklace "KMP"