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

解决OmNom为其女友制作项链的问题,通过寻找序列中的规律模式,使用KMP算法确定哪些序列可以形成规则的项链图案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

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;
}

 

### 关于 Codeforces 平台上的 KMP 算法练习题目 对于希望在 Codeforces 上找到有关 KMP (Knuth-Morris-Pratt) 字符串匹配算法的练习题目的选手来说,可以关注几个特定标签下的问题。通常这些题目会被标记为 "strings" 或者更具体地标记为 "string suffix structures"[^1]。 为了帮助更好地理解如何查找适合的练习题目,在此提供一段 Python 代码用于模拟访问 API 获取带有指定标签的问题列表: ```python import requests def fetch_problems(tag, platform="codeforces"): url = f"https://{platform}.com/api/problemset.problems?tags={tag}" response = requests.get(url) if response.status_code != 200: raise Exception(f"Failed to retrieve data from {platform}") json_data = response.json() problem_list = [] for index, item in enumerate(json_data['result']['problems']): name = item["name"] rating = item.get('rating', 'N/A') contest_id = item["contestId"] index = item["index"] problem_info = { "Name": name, "Rating": rating, "Link": f"{platform}.com/contest/{contest_id}/problem/{index}" } problem_list.append(problem_info) return problem_list[:5] # Example usage with the tag related to string processing or specifically KMP pattern matching. kmp_related_tag = "strings" fetched_problems = fetch_problems(kmp_related_tag) for prob in fetched_problems: print(prob) ``` 这段脚本会调用 Codeforces 的公开 API 来获取前五个与字符串处理相关的编程挑战链接,并打印出来供参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值