题目链接: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. elasticsearch 第四篇(API约定)

    对多个indices进行操作 es中大多resetapi支持请求多个index, 例如”test1,test2,test3”,index也可以使用通配符, 例如”test*“, 还可以使用+,-来包含 ...

  2. 关于kali linux系统的简单工具

    Linux系统中关于几个重要目录的原英文解释: /etc/: Contains configuration files of the installed tools /opt/: Contains M ...

  3. Zabbix Agent for Linux部署(四)

    一.环境介绍 二.代理安装 1.将代理程序拷贝至Node1服务器的/usr/src/zabbix3.4.5/目录下 [root@Node3 zabbix3.-.el7.x86_64.rpm zabbi ...

  4. bzoj2296: 【POJ Challenge】随机种子(思维题/水题)

    有点类似CF某场div2T1... 前面接上1234567890000000,后面加上x+(1234567890000000%x)就可以保证是x的倍数了 #include<iostream> ...

  5. [JOI 2015 Final] 分蛋糕 2

    link 试题分析 容易发现性质,选择的是一段区间,但是贪心无法去维护这件事情,所以考虑$dp$,且我们只要去设计关于$JOI$的选择. 设$dp(i,j)$为现在要在$[l,r]$区间内选择,然后就 ...

  6. 【字符串】manacher算法

    Definition 定义一个回文串为从字符串两侧向中心扫描时,左右指针指向得字符始终相同的字符串. 使用manacher算法可以在线性时间内求解出一个字符串的最长回文子串. Solution 考虑回 ...

  7. 2017 Multi-University Training Contest - 1

    hdu 6033 pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #inc ...

  8. BNU-2017.7.3排位赛1总结

    比赛链接:https://www.bnuoj.com/v3/contest_show.php?cid=9146#info A题 国际象棋棋盘,黑白相间染色. B题 最大值只取决于每个连通块的大小,一个 ...

  9. vue 和react

    React 和 Vue 有许多相似之处,它们都有: 使用 Virtual DOM 提供了响应式 (Reactive) 和组件化 (Composable) 的视图组件. 将注意力集中保持在核心库,而将其 ...

  10. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...