题目描述:

有一天,欧姆诺姆发现了一串长度为n的宝石串,上面有五颜六色的宝石。他决定摘取前面若干个宝石来做成一个漂亮的项链。

他对漂亮的项链是这样定义的,现在有一条项链S,当S=A+B+A+B+A+...+A+B+A的时候是漂亮的,这儿A,B是一些宝石串,“+”表示连接操作。S中有k+1个A和k个B组成。A和B可能是空串。

现在给出宝石串,问怎么切前几个才能得到一个漂亮的宝石项链。他切下来之后不会改变宝石的顺序。

样例解释:

在这个样例中前6个可以组成漂亮的串( A="", B="bca")。前7个也可以(A="b", B="ca")。

题解:

首先用kmp算法可以将字符串的一个前缀划分成SSSS....ST的形式。

下面考虑如果T=S,R为有多少个S,那么每个AB的要求就是要有R/k个S,最后会余下来R%k个S,那么把余下来的这部分当作A,其余部分当作B即可,这里B可以为空串

所以条件就是R/k-R%k>0

如果T!=S,每个AB的要求仍然是有R/k个S,最后会余下来R%k个S加上T,这时就需要把余下的部分当成A,剩下的部分当成B,但是这里B不能为空串,不然就不能凑出来AB(因为A不是S..SS,是S..ST)了

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 2e6;
int n, k;
char s[maxn];
int ans[maxn], fail[maxn];
int main()
{
cin>>n>>k;
cin>>s;
int j = ;
fail[] = ;
ans[] = ;
if(k == ) ans[] = ;
for(int i = ; i < n; i++){
j = fail[i];
while(j && s[i] != s[j]) j = fail[j];
j = s[i] == s[j] ? j+ : ;
fail[i+] = j; int Q = i-j+, R = (i+)/Q;
if((i+)%Q == ) ans[i] = R/k >= R%k;
else ans[i] = R/k > R%k;
}
for(int i = ; i < n; i++) putchar(ans[i] ? '' : '');
return ;
}

Codeforces ZeptoLab Code Rush 2015 D.Om Nom and Necklace(kmp)的更多相关文章

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

  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. 北京Uber优步司机奖励政策(12月18日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. Python:PyCharm如何导入模块

  3. orm4sqlite

    //-------------------------------------------------------------------------- // // Copyright (c) BUS ...

  4. 第一篇 HTML基础

    浏览网页,就是上网,上网的本质就是下载内容. 浏览器是个解释器,用来执行HTML.css.JS代码的. HTML,CSS, JavaScript 号称网络三剑客. 1. 浏览器发送一个域名给服务端 2 ...

  5. [CodeForce455A]Boredom

    题面描述 Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long ...

  6. Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法

    Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法 工作电脑装了一个虚拟机玩玩,胡乱下载了一些软件,apt-get了不少操作,后来重启 ...

  7. [leetcode-744-Find Smallest Letter Greater Than Target]

    Given a list of sorted characters letters containing only lowercase letters, and given a target lett ...

  8. Sublime Text 插件推荐——for web developers

    楼主向高大上的: web front-end development engineer (好吧,google就是这样翻译的 ^_^)们推荐 ST 插件,在此抛砖引玉: NO.1 :Emmet (原名: ...

  9. 自测之Lesson9:时钟与信号

    题目一:编写一个获取当前时间的程序,并将其以“year-mon-day time”的形式输出. 程序代码: #include <stdio.h> #include <time.h&g ...

  10. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...