Codeforces 526D Om Nom and Necklace(kmp+数学)

题面

One day Om Nom found a thread with n beads of different colors. He decided to cut the first several beads from this thread to make a bead necklace and present it to his girlfriend Om Nelly.

Om Nom knows that his girlfriend loves beautiful patterns. That's why he wants the beads on the necklace to form a regular pattern. A sequence of beads S is regular if it can be represented as S = A + B + A + B + A + ... + A + B + A, where A and B are some bead sequences, " + " is the concatenation of sequences, there are exactly 2k + 1 summands in this sum, among which there are k + 1 "A" summands and k "B" summands that follow in alternating order. Om Nelly knows that her friend is an eager mathematician, so she doesn't mind if A or B is an empty sequence.

Help Om Nom determine in which ways he can cut off the first several beads from the found thread (at least one; probably, all) so that they form a regular pattern. When Om Nom cuts off the beads, he doesn't change their order.

Input

The first line contains two integers n, k (1 ≤ n, k ≤ 1 000 000) — the number of beads on the thread that Om Nom found and number k from the definition of the regular sequence above.

The second line contains the sequence of n lowercase Latin letters that represent the colors of the beads. Each color corresponds to a single letter.

Output

Print a string consisting of n zeroes and ones. Position i (1 ≤ i ≤ n) must contain either number one if the first i beads on the thread form a regular sequence, or a zero otherwise.

Examples

Input

7 2
bcabcab

Output

0000011

Input

21 2
ababaababaababaababaa

Output

000110000111111000011

Note

In the first sample test a regular sequence is both a sequence of the first 6 beads (we can take A = "", B = "bca"), and a sequence of the first 7 beads (we can take A = "b", B = "ca").

In the second sample test, for example, a sequence of the first 13 beads is regular, if we take A = "aba", B = "ba".

题目链接

Codeforces_526D

参考链接

题解参考:Codeforces 526D Om Nom and Necklace 循环节 kmp author: kkkkahlua

题解参考:codeforces 526D(kmp,数学)author: 1035719430

kmp学习:kmp求最小循环节

题目简述

见上述题解

第一次的代码:试图遍历循环节的大小,逐个比较,妥妥的TLE

AC代码:

A+B+A+B+A+B+A,令A+B=C,则C+C+C+“C的前缀”,C为一个循环节

先求得kmp中的next数组,得到最小循环节长度i-next[i]。

根据当前能否求得一个合法的t(t*最小循环节长度==循环节长度),来判断answer[i]为‘1’或‘0’

程序

#include<stdio.h>
#include<string.h>
#define maxn 1000005
char s[maxn];
int next[maxn];

void build_next(int n)/**求最小循环子串**/
{
    int j=0;
    for(int i=2;i<=n;i++)
    {
        while(j&&s[j+1]!=s[i])
            j=next[j];
        if(s[i]==s[j+1])
            j++;
        next[i]=j;
    }
}

bool check_i(int where,int mins_length,int k)/**能否找到t,循环节长度为t*mins_length**/
{
    int mins_num=where/mins_length;
    if(mins_num/k>mins_num%k)
        return true;
    if(where%(k+1)==0)
    {
        where/=(k+1);
        if(where%mins_length==0)
            return true;
    }
    return false;
}

int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    scanf("%s",s+1);
    build_next(n);
    for(int i=1;i<=n;i++)
    {
        int mins_length=i-next[i];
        if(check_i(i,mins_length,k))
            printf("1");
        else
            printf("0");
    }
    printf("\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值