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学习Episode 13——常用模块
模块 一.time模块 时间戳(timestamp) :时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. 元组(struct_time) :struct_time元组共有9 ...
- Web安全入门书籍推荐
<Web应用安全权威指南> 链接:https://pan.baidu.com/s/1wZgv3c9jhWm1bAUHZw3gEg 提取码:teqj <黑客攻防技术宝典__Web实战篇 ...
- USACO 2.3.3 Zero Sum 和为零(深搜枚举)
Description 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白,来将 ...
- postman的巨坑 之 cookie
问题描述:一个后端接口,该接口需要校验登录态,登录态通过cookie中的一个传参k判断.在保证登录的前提下,调用机器A上的接口一直报“用户未登录”,调机器B上就没问题,于是开始排查问题. 解决过程: ...
- 关于“问吧APP”问卷调查报告分析与体会
上周根据我们走廊奔跑队的“问吧APP”项目对本校范围内的学生发放了上百份调查问卷,并对此作出了统计和整理.针对我们项目所提出的问题涉及到的用户信息有性别.年龄.学历.职业.平时上网途径以及对 ...
- 结对编程——paperOne基于java web的简易四则运算出题网站
项目成员:张金生 张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...
- 7. 由一道ctf学习变量覆盖漏洞
0×00 背景 近期在研究学习变量覆盖漏洞的问题,于是就把之前学习的和近期看到的CTF题目中有关变量覆盖的题目结合下进一步研究. 通常将可以用自定义的参数值替换原有变量值的情况称为变量覆盖漏洞.经常导 ...
- 爬虫学习之-Python list 和 str 互转
一.list转字符串 命令:''.join(list)其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等如:list = [1, 2, 3, 4, 5]''.join(list) 结果 ...
- 【Nginx】优化配置
nginx优化 突破十万并发 一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它 ...
- codepen & js demos
codepen & js demos Mutation Observer & customize resize event listener & demo https://co ...