Codeforces 526.D Om Nom and Necklace
1 second
256 megabytes
standard input
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.
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 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.
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.
7 2
bcabcab
0000011
21 2
ababaababaababaababaa
000110000111111000011
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的更多相关文章
- 【Codeforces 526D】Om Nom and Necklace
Codeforces 526 D 题意:给一个字符串,求每个前缀是否能表示成\(A+B+A+B+\dots+A\)(\(k\)个\(A+B\))的形式. 思路1:求出所有前缀的哈希值,以便求每个子串的 ...
- Codeforces 526D - Om Nom and Necklace 【KMP】
ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...
- 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 ...
- 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 ...
- CodeForces 526D Om Nom and Necklace
洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...
- Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)
题目描述: 有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石.他决定摘取前面若干个宝石来做成一个漂亮的项链. 他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+. ...
- CF526D Om Nom and Necklace
嘟嘟嘟 我们可以把AB看成S,则要找的串可以写成SSSSA或者SSSSS.假设S出现了Q次,那么A出现了Q % k次,则B出现了 Q / k - Q % k次. 当ABABA是SSS的形式时,B可以为 ...
- 【Henu ACM Round#16 F】Om Nom and Necklace
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] KMP算法可以把"i前缀"pre[i] 分成ssssst的形式 这里t是s的前缀. 然后s其实就是pre[i]中 ...
- Codeforces C - Om Nom and Candies
C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...
随机推荐
- Python基础入门(迭代器和生成器)
1 Python迭代器 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退. 迭代器有两个基本的方法:iter() 和 ...
- HDU 6438
Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1 ...
- OpenLDAP备份和恢复
OpenLDAP中数据备份一般分为二种: 1)通过slapcat 指令进行备份 2)通过phpLDAPadmin控制台进行备份 备份方式1: 1)slapcat -v -l openldap-back ...
- [T-ARA][Lovey-Dovey]
歌词来源:http://music.163.com/#/song?id=22704426 作曲 : 新沙洞老虎/崔圭成 [作曲 : 新沙洞老虎/崔圭成] [作曲 : 新沙洞老虎/崔圭成] 作词 : 新 ...
- 常用DOS指令备忘
1.删除整个目录,包括空目录 rd D:\管理\2012新同学练习\.svn /s/q /s 删除当前目录及子目录 /q 不询问直接删除 2.拷贝目录树 xcopy D:\管理\2012新同学练习 E ...
- ES6的新特性(18)——async 函数
async 函数 含义 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 Generato ...
- 为phpStorm 配置PHP_CodeSniffer自动检查代码
通过composer 安装PHP_CodeSniffer : squizlabs/PHP_CodeSniffer gihub地址 composer global require "squiz ...
- Scrum立会报告+燃尽图(十月二十七日总第十八次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- CentOS7安装Consul集群
1.准备4台服务器 linux1 192.168.56.101 linux2 192.168.56.102 linux3 192.168.56.103 linux4 192.168.56.104 2. ...
- Dsyy的第一篇博文~
2017-08-07 周一 晴热热热热热 咳咳,很多人看到dsyy第一反应是什么意思?当然是大神媛媛!显然不是些(diao)(si)yy.(da)(si)yy...的别义,咋有点此地无银三百两的感 ...