Codeforces 526d-om Nom and Necklace "KMP"

Source: Internet
Author: User

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,

    1. Why the length of a should be as small as possible
    2. 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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.