http://codeforces.com/problemset/problem/526/D

题意

给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A 的形式(k +1个 A,k 个 B,均可为空串)

分析

官方题解

对于前缀P,我们可以把它拆成P=SSSS…SSSST,其中T是S的前缀。显然可以用KMP算法,时间复杂度是O(n)。

当T=S:P=SSS…S。假设S出现了R次。如果转换为ABABAB…ABABA的形式,A和B是由几个S组成,而且最后的A一定是P的一个后缀。由贪心算法,A的长度尽量小,所以A中S出现R mod Q次。B中S出现R/Q-R mod Q次。因为只需判断R/Q-R mod Q>=0即可

当T!=S:由于上一种方法类似,A可以是SSS…ST,因为它的长度尽量小,所以我们只需判断是否R/Q-R mod Q>0。

那为什么A中有R mod Q个S,B中有R/Q-R mod Q 个S?因为要将字符串P=SSSSSSS变成ABABABABA的形式,可以看成是Q份AB和一份A结合,那么每份AB就有R/Q个S,所以剩下的R%Q就是A的S,于是B就是R/Q-R mod Q了。

当T=S时,B可以为空串,也就是B的长度可以为0,于是取等号。当T!=S时,反之。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case #%d: ",++T);
}
const int MAXN = 1e6+;
const int MAXM = ;
int n,k;
char s[MAXN];
int Next[MAXN]; void getNext(){
int i,j;
j=Next[]=-;
i=;
while(i<n){
while(-!=j&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
}
int check(int i){
int d=i-Next[i]; //最小循环节
int cnt = i/d; //循环节次数
if(i%d==){ //被整除
return cnt/k-cnt%k>=; //
}else return cnt/k-cnt%k>; }
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
scdd(n,k);
scanf("%s",s);
mset(Next,);
getNext();
for(int i=;i<=n;i++){
printf("%d",check(i));
}
// for(int i=0;i<=1+n;i++) printf("%d ",Next[i]);
return ;
}

Codeforces 526D Om Nom and Necklace (KMP)的更多相关文章

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

  2. CodeForces 526D Om Nom and Necklace

    洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...

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

  4. Codeforces 526.D Om Nom and Necklace

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. poj2406 Power Strings(kmp)

    poj2406 Power Strings(kmp) 给出一个字符串,问这个字符串是一个字符串重复几次.要求最大化重复次数. 若当前字符串为S,用kmp匹配'\0'+S和S即可. #include & ...

  6. Codeforces C - Om Nom and Candies

    C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否 ...

  7. POJ 2406 Power Strings(KMP)

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  8. LightOJ 1258 Making Huge Palindromes(KMP)

    题意 给定一个字符串 \(S\) ,一次操作可以在这个字符串的右边增加任意一个字符.求操作之后的最短字符串,满足操作结束后的字符串是回文. \(1 \leq |S| \leq 10^6\) 思路 \( ...

  9. codeM编程大赛E题 (暴力+字符串匹配(kmp))

    题目大意:S(n,k)用k(2-16)进制表示1-n的数字所组成的字符串,例如S(16,16)=123456789ABCDEF10: 解题思路: n最大50000,k最大100000,以为暴力会超时. ...

随机推荐

  1. SpringMVC一例 是否需要重定向

    在ASP.NET MVC下: return view("List") 和 return RedirectToAction("List") 百度知道的最佳答案: ...

  2. node的经典事件监听

    let fs = require('fs'); let Event = require('events'); let myEvent = new Event(); //注册一个订阅者 A myEven ...

  3. [转帖]2016年时的新闻:ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0

    ASP.NET Core 1.0.ASP.NET MVC Core 1.0和Entity Framework Core 1.0 http://www.cnblogs.com/webapi/p/5673 ...

  4. [转帖] YAML 快速入门

    https://www.jianshu.com/p/97222440cd08 原始文档更加易读. YAML快速入门 叩丁狼教育 关注 2018.02.18 19:19* 字数 1776 阅读 876评 ...

  5. 普通javabean 获得项目的绝对路径

    方式一:String path = RequestContext.class.getResource("/").getFile();

  6. jenkins--svn+Email自动触发2(jenkins系统配置)

    jenkins系统配置-SonarQube servers配置: 邮件通知设置: 邮件调试问题: 在 系统设置 --> Extended E-mail Notification: 找到 Enab ...

  7. BZOJ5178[Jsoi2011]棒棒糖——主席树

    题目描述 Coffee的世界里也是有棒棒糖卖的,Coffee买了N(1≤N≤50000)只连着的.这N只棒棒糖包裹在小塑料袋中,排成 一列,相邻的两只棒棒糖的塑料袋是接起来的.为了方便,我们把棒棒糖从 ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)A,B,C

    A:链接:http://codeforces.com/contest/831/problem/A 解题思路: 从前往后分别统计递增,相等,递减序列的长度,如果最后长度和原序列长度相等那么就输出yes: ...

  9. linux上搭建nginx

    安装包:(按顺序执行过去即可,重复无所谓)c++编译环境 yum install gcc-c++ 安装pcre yum -y install pcre-devel 安装openssl yum -y i ...

  10. 洛谷P1776 宝物筛选_NOI导刊2010提高(02)(多重背包,单调队列)

    为了学习单调队列优化DP奔向了此题... 基础的多重背包就不展开了.设\(f_{i,j}\)为选前\(i\)个物品,重量不超过\(j\)的最大价值,\(w\)为重量,\(v\)为价值(蒟蒻有强迫症,特 ...