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 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.

Sample test(s)
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".


思路:可以将AB看成一个串(设为C),然后问题就等价为:判断字符串是否由k个连续的C串以及C串的一个前缀组成(前缀可以为空)。

然后用Z算法求出z数组。

然后从1到n枚举C串的长度(设为len),判断z[0],z[len],z[len*2],....,z[len*(k-1)]的值是否都不小于len,如果都符合就标记最后一个匹配位置为true。

输出的时候维护一个last指针,表示C串的前缀最多能扩展到哪个位置。

复杂度o(n).


 #include <iostream>
#include <stdio.h>
using namespace std;
#define MAXN 1000010 char s[MAXN];
bool ans[MAXN] = {};
int z[MAXN] = {}; int n, k;
bool check(int len)
{
for(int i = , cnt = ; i < n && cnt < k; i += len, cnt++)
{
if(z[i] < len)
return false;
}
return true;
} int main()
{
freopen("in.txt", "r", stdin);
cin >> n >> k; scanf("%s", s); // Z[i] is the length of the longest substring starting from S[i] which is also a prefix of S
// s[0,n-1]
int L = , R = ;
for(int i = ; i < n; i++)
{
if(i > R)
{
L = R = i;
while(R < n && s[R - L] == s[R]) R++;
z[i] = R - L;
R--;
}
else
{
int k = i - L;
if(z[k] < R - i + ) z[i] = z[k];
else
{
L = i;
while(R < n && s[R - L] == s[R]) R++;
z[i] = R - L;
R--;
}
}
}
z[] = n; // 枚举AB串的长度
for(int len = ; len <= n; len++)
{
if(len * k > n)
break;
// 判断长度为len的AB串是否符合
if(check(len))
{
int last = len * k;
ans[last - ] = true;
}
} int last = -;
for(int i = ; i < n; i++)
{
if(ans[i] || i<=last)
printf("");
else
printf("");
if(ans[i] && i < n - )
{
int len = (i+)/k;
last = max(last, min(i+len, i+z[i+]));
}
} return ;
}

Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串的更多相关文章

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

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

  2. ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力

    C. Om Nom and Candies Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526 ...

  3. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS

    B. Om Nom and Dark Park Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  4. ZeptoLab Code Rush 2015 C. Om Nom and Candies [ 数学 ]

    传送门 C. Om Nom and Candies time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park

    Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who l ...

  6. CodeForces ZeptoLab Code Rush 2015

    拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...

  7. Zepto Code Rush 2014 B - Om Nom and Spiders

    注意题目给的是一个nxm的park,设元素为aij,元素aij 有4种可能U(上移),D(下移),L(左移),R(右移) 假设第i行第j列元素aij(注意元素的索引是从0开始的) 当aij为D时,此时 ...

  8. CF Zepto Code Rush 2014 B. Om Nom and Spiders

    Om Nom and Spiders time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  9. ZeptoLab Code Rush 2015 A. King of Thieves 暴力

    A. King of Thieves Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/526/pr ...

随机推荐

  1. 二分图的最大匹配-hdu-3729-I'm Telling the Truth

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3729 题目意思: 有n个学生,老师询问每个学生的排名,每个学生都告诉了一个排名区间,求可能的最多的学 ...

  2. COM编程入门第一部分——什么是COM,如何使用COM

    本文的目的是为刚刚接触COM的程序员提供编程指南,并帮助他们理解COM的基本概念.内容包括COM规范简介,重要的COM术语以及如何重用现有的COM组件.本文不包括如何编写自己的COM对象和接口. CO ...

  3. ProGuard 代码混淆

    简介 Java代码是非常容易反编译的.为了很好的保护Java源代码,我们往往会对编译好的class文件进行混淆处理. ProGuard是一个混淆代码的开源项目.它的主要作用就是混淆,当然它还能对字节码 ...

  4. excel导入mssql数据库,支持excel2003--2010文件格式

    rt,简单的excel导入mssql.未做性能及海量数据优化,一般单表5000左右条数据导入适用. 源码非原创,来源于之前搜集整理,原作者无从考究,如有版权问题请留言注明. 看代码.前台页面是一个Fi ...

  5. bootstrap字体图标

    bootstrap字体图标 http://v3.bootcss.com/components/ <!DOCTYPE HTML> <html> <head> < ...

  6. sping注解原理

    持续更新中.. spring注解用的是java注解,用到的是java反射机制. 参考文档如下: http://zxf-noimp.iteye.com/blog/1071765 对应spring源码如下 ...

  7. 《CSS网站布局实录》学习笔记(二)

    第二章 XHTML与CSS基础 2.1 XHTML基础 XHTML是网页代码的核心内容,标准XHTML代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

  8. JQuery 代码

    http://baike.baidu.com/view/136475.htmhttp://www.cnblogs.com/gleamy_ming/archive/2009/04/29/1446492. ...

  9. TFS统计编码行数语句

    ****** Script for SelectTopNRows command from SSMS ******/ SELECT TeamProjectProjectNodeName 项目名称 ,c ...

  10. 有关phpmailer的详细介绍及使用方法

    第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket ,通过phpinfo();查 ...