D. Om Nom and Necklace
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nk (1 ≤ n, k ≤ 1 000 000) — the number of beads on the thread that Om Nom found and number kfrom 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".

大致题意:给一个字符串,问该字符串的[1,i]位上的字符串能不能由A+B+A+B+......+A构成?其中k+1个A,k个B,A,B可以为空串.

分析:分别枚举A,B不大好做,但是AB可以拼起来,原题就变成了能不能用k个AB和1个A拼成,AB作为一个循环节,要先求循环节的长度,利用kmp的next数组得到.最小循环节的t倍还是循环节,记作cir,那么问题就是判断能否存在t使得i / (t * cir) = k或i = (k+1) * t*cir

(A是空串).这个问题就比较简单了,将t用i,cir,k表示,检验t是否>0并且满足条件式子.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int n, k, nextt[];
char s[]; void init()
{
int j = ;
for (int i = ; i <= n; i++)
{
while (j > && s[j + ] != s[i])
j = nextt[j];
if (s[j + ] == s[i])
j++;
nextt[i] = j;
}
} bool check(int x)
{
int cir = x - nextt[x];
if (x % (k + ) == && (x / (k + )) % cir == )
return true;
int t = x / (k * cir);
if (t > && x / (cir * t) == k)
return true;
return false;
} int main()
{
scanf("%d%d", &n, &k);
scanf("%s", s + );
init();
for (int i = ; i <= n; i++)
{
if (check(i))
printf("");
else
printf("");
} return ;
}

Codeforces 526.D Om Nom and Necklace的更多相关文章

  1. 【Codeforces 526D】Om Nom and Necklace

    Codeforces 526 D 题意:给一个字符串,求每个前缀是否能表示成\(A+B+A+B+\dots+A\)(\(k\)个\(A+B\))的形式. 思路1:求出所有前缀的哈希值,以便求每个子串的 ...

  2. Codeforces 526D - Om Nom and Necklace 【KMP】

    ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...

  3. Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Codeforces 526D Om Nom and Necklace (KMP)

    http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A ...

  5. CodeForces 526D Om Nom and Necklace

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...

  6. Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)

    题目描述: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石.他决定摘取前面若干个宝石来做成一个漂亮的项链. 他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+. ...

  7. CF526D Om Nom and Necklace

    嘟嘟嘟 我们可以把AB看成S,则要找的串可以写成SSSSA或者SSSSS.假设S出现了Q次,那么A出现了Q % k次,则B出现了 Q / k - Q % k次. 当ABABA是SSS的形式时,B可以为 ...

  8. 【Henu ACM Round#16 F】Om Nom and Necklace

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] KMP算法可以把"i前缀"pre[i] 分成ssssst的形式 这里t是s的前缀. 然后s其实就是pre[i]中 ...

  9. Codeforces C - Om Nom and Candies

    C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...

随机推荐

  1. Winrar去广告图文教程

    一.前言 1.1 Winrar 解压缩工具 市场上有很多优秀的压缩工具,常用的有Winrar 和360 压缩工具.Winrar是免费压缩工具,特色是每次使用都会弹出广告.影响用户体验和工作效率,当然最 ...

  2. spring-boot断点调试(IDEA)

  3. Gradle初使用

    我以前一直使用Maven来构建工程,这两天突然发现gradle也非常好用,记录一下自己使用gradle的过程. Gradle的下载与配置 本次选择下载的是gradle3.5版本,没选最新的gradle ...

  4. Python科学计算库灬numpy

    Numpy NumPy是一个功能强大的Python库,主要用于对多维数组执行计算.Numpy许多底层函数实际上是用C编写的,因此它的矩阵向量计算速度是原生Python中无法比拟的. numpy属性 维 ...

  5. windows下对python的pip更新到最新版本

    1->打开windows的命令窗口. 2->进入到pip.exe所在的文件夹下,我安装的python在G:\python3.6文件夹下,pip.exe则在G:\python3.6\Scri ...

  6. access数据库频繁读取操作会出现 System.Data.OleDb.OleDbException 的异常解决

    asp.net access数据库 本来想着打开一个access数据库连接后,不关闭,下次操作数据了,直接拿来用,谁知道连着测试64次后(大概这么多次),就会出现System.Data.OleDb.O ...

  7. 算法笔记(c++)--c++中碰到的一些用法

    算法笔记(c++)--c++中碰到的一些用法 toupper(xxx)可以变成大写; tolower(xx)小写 isalpha(xxx)判断是不是字母 isalnum(xx)判断是不是数字 abs( ...

  8. android开发问题 Failed to pull selection 菜鸟记录

    在eclipse中开发创建了一个sqlite数据库文件,为了查看数据库文件的内容,决定复制到PC上一看究竟,位置在data……里 当我点击ddms文件浏览里的pull a file from the ...

  9. Scrum立会报告+燃尽图(十一月二十四日总第三十二次):视频剪辑

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2284 项目地址:https://git.coding.net/zhang ...

  10. 总结在Visual Studio Code运行node.js项目遇到的问题

    一.cannot find module “lodash” 项目运行时出现以下错误: Error: Cannot find module 'lodash' at Function.Module._re ...