题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358

题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成。

也就是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”)

的两次组成,也就是aa有两个a组成;

第三行自然就是aabaab可有两个aab组成;

第四行aabaabaab可由三个aab组成;

第五行aabaabaabaab可由四个aab组成。

解题思路:同HDU 3746类似,通过计算字符串前缀的循环节获得相应周期即可。

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+; int m;
int nxt[N];
char p[N]; void getnext(){
int i,j;
i=,j=nxt[]=-;
while(i<m){
while(j!=-&&p[i]!=p[j])
j=nxt[j];
nxt[++i]=++j;
}
} int main(){
int cas=;
while(scanf("%d",&m)&&m){
scanf("%s",p);
getnext();
printf("Test case #%d\n",++cas);
//枚举长度为2~m的字符串前缀
for(int i=;i<=m;i++){
int mmin=i-nxt[i]; //len-nxt[len]为最小循环节
if(i!=mmin&&i%mmin==)
printf("%d %d\n",i,i/mmin);
}
printf("\n");
}
return ;
}

HDU 1358 Period(KMP计算周期)的更多相关文章

  1. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  2. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  3. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  4. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  5. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  6. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  7. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  8. HDU - 1358 - Period (KMP)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

随机推荐

  1. Unity3D手游开发日记(5) - 适合移动平台的植被随风摆动

    一直在思考怎么让场景更有生机,我觉得植被的随风摆动是必不可少的.CE3引擎的植被bending就做得特别棒.我也准备在手机上做一套. 先分析一下植被摆动常见的几种做法.其实不管哪种做法,核心就是让植被 ...

  2. UESTC--1655

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1655 分析:注意可能会反向. #include<iostream> #include<c ...

  3. php curl使用ss代理

    1.安装 ss,过程略 2.ss 配置文件 { "server":"x.x.x.x", #你的 ss 服务器 ip "server_port" ...

  4. redis 配置和使用(C++)

    一.Redis简介: Redis为非关系型数据库,Redis是一个Key-Value存储系统.它支持存储的value类型有:string(字符串),list(链表), set(无序集合),zset(s ...

  5. TCP与UDP区别详解

    TCP协议与UDP协议的区别    首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UDP协议的区别,我觉得这是没有从本质上弄清楚网络通信! ...

  6. 主席树 或者 离散化+分块 BZOJ 4636

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 381  Solved: 177[Submit][Status][Discuss ...

  7. ZOJ 3774 Fibonacci的K次方和

    In mathematics, Fibonacci numbers or Fibonacci series or Fibonacci sequence are the numbers of the f ...

  8. LightOJ 1065 - Number Sequence 矩阵快速幂水题

    http://www.lightoj.com/volume_showproblem.php?problem=1065 题意:给出递推式f(0) = a, f(1) = b, f(n) = f(n - ...

  9. errno错误号含义

    errno0 : Success errno1 : Operation not permitted errno2 : No such file or directory errno3 : No suc ...

  10. ② 设计模式的艺术-02.简单工厂(Simple Factory)模式

    工厂模式 实现了创建者和调用者的分离. 详细分类:简单工厂模式.工厂方法模式.抽象工厂模式 面向对象设计的基本原则 OCP(开闭原则,Open-Closed Principle):一个软件的实体应当对 ...