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. Robotium API -- 除click/clickLong外的其他操作

    拖动操作 void drag (float fromX, float toX, float fromY, float toY, int stepCount) 选定两个位置,进行拖动操作(这里的拖动操作 ...

  2. [置顶] 我的Android进阶之旅------>介绍一款集录制与剪辑为一体的屏幕GIF 动画制作工具 GifCam

    由于上一篇文章:我的Android进阶之旅------>Android之动画之Frame Animation实例 中展示的是Frame动画效果,但是之前我是将图片截取下来,不好说明确切的动画过程 ...

  3. 【移动开发】Android中将我们平时积累的工具类打包

    Android开发的组件打包成JAR安装包,通过封闭成JAR包,可以重复利用,非常有利于扩展和减少工作重复性.这里为了讲解方便,我用了之前的一个代码框架中核心部分,不了解的可以回头看一下:http:/ ...

  4. python Sina微博自动转发带抽奖字样的微博,添加关注,取消关注

    项目地址:https://github.com/chengshuyi/SinaWeibo 具有的功能 转发带抽奖字样的微博并可以@相应数量的好友 提取关注并添加关注 取消关注 获取粉丝列表

  5. HDU -1864最大报销额(01背包)

    这道题属于简单的01背包,但是背包问题还算简单,就是前面的细节处理的时候要注意,题意大致说了三条限制吧 1. 只有a, b, c 三种类型的发票可以报销,其它的一律不报销 2. 物品单项的报销额不超过 ...

  6. 数据库连接超时和go away、如何检测数据库的最大连接数

    搜索连接bi库超时 数据库连接超时 go away go away和连接超时之间的关系是什么? 写一个例子测试一下. 如何检测数据库的最大连接数

  7. 匹配不含有某个信息的sql语句写法

    SELECT id,order_id,flight_info FROM order_flights WHERE mark=0 AND flight_info REGEXP '[^() DAY)]' O ...

  8. 使用VS2012的C++生成dll

    1,首先,通过File->New Project的方式新建一个工程,打开"New Project"对话框. 2,选择Visual C++语言下的 Win32->Win3 ...

  9. PHP 错误处理

    PHP 错误处理 在 PHP 中,默认的错误处理很简单.一条错误消息会被发送到浏览器,这条消息带有文件名.行号以及描述错误的消息. PHP 错误处理 在创建脚本和 Web 应用程序时,错误处理是一个重 ...

  10. MySQL FROM 子查询

    FROM 子句中的子查询 MySQL FROM 子查询是指 FROM 的子句作为子查询语句,主查询再到子查询结果中获取需要的数据.FROM 子查询语法如下: SELECT ... FROM (subq ...