POJ 2406 - Power Strings - [KMP求最小循环节]
题目链接:http://poj.org/problem?id=2406
Time Limit: 3000MS Memory Limit: 65536K
Description
Input
Output
Sample Input
abcd
aaaa
ababab
.
Sample Output
1
4
3
题意:
求给出字符串,最多是多少个循环节组成的。
题解:
利用len % (len-Next[len]) == 0的话,len-Next[len]是最小循环节长度的性质。
AC代码:
#include<cstdio>
#include<cstring>
using namespace std; const int MAXpat = +; char pat[MAXpat];
int Next[MAXpat],len; void getNext()
{
int i=, j=-;
len=strlen(pat);
Next[]=-;
while(i<len)
{
if(j == - || pat[i] == pat[j]) Next[++i]=++j;
else j=Next[j];
//printf("now i=%d j=%d next[%d]=%d pat[%d]=%c\n",i,j,i,Next[i],i,pat[i]);
}
}
int main()
{
while(scanf("%s",pat))
{
if(pat[]=='.' && pat[]=='\0') break;
getNext();
if(len%(len-Next[len])==) printf("%d\n",len/(len-Next[len]));
else printf("1\n");
}
}
注意:
①当且仅当len%(len-Next[len])==0时,len-Next[len]才是最小循环节长度。
②关于Next数组,我觉得这个图很不错的展示了Next数组存储了啥:

POJ 2406 - Power Strings - [KMP求最小循环节]的更多相关文章
- UVA - 10298 Power Strings (KMP求字符串循环节)
Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...
- POJ 2406 Power Strings KMP求周期
传送门 http://poj.org/problem?id=2406 题目就是求循环了几次. 记得如果每循环输出为1.... #include<cstdio> #include<cs ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- POJ 2406 Power Strings (KMP)
Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...
- poj2406--Power Strings(KMP求最小循环节)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 33178 Accepted: 13792 D ...
- KMP 求最小循环节
转载自:https://www.cnblogs.com/chenxiwenruo/p/3546457.html KMP模板,最小循环节 下面是有关学习KMP的参考网站 http://blog.cs ...
- 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 ...
- hdu 3746 Cyclic Nacklace (KMP求最小循环节)
//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string. ...
- poj 2406 Power Strings【字符串+最小循环节的个数】
Po ...
随机推荐
- scala akka Future 顺序执行 sequential execution
对于 A => B => C 这种 future 之间的操作,akka 默认会自动的按照顺序执行,但对于数据库操作来说,我们希望几个操作顺序执行,就需要使用语法来声明 有两种声明 futu ...
- iptables相关操作以及简单理解端口和服务之间关系
一般CentOS7默认安装的是firewall不是iptables 1.查看firewall状态 firewall-cmd --state 关闭后显示not running,开启后显示running ...
- 转载用sql语句计算出mysql数据库的qps,tps,iops性能指标
本帖最后由 LUK 于 2014-9-21 22:39 编辑 思路: 1 关注MYSQL三个方面的性能指标,分别为query数,transaction数,io请求数 2 在某个时间范围内(例如20秒) ...
- sutdio中替换全局方法
Ctril Shift R Text to find :是要搜索的内容或者要被替换的内容 Replace with :是要替换的内容 Preview:是可以看到预览 在Scope的选项卡里 Whole ...
- java如何调用另一个包里面的类
我现在有两个包: 我想在Boss里面实现对Employee的调用, Employee.java: package payroll2; public class Employee { public vo ...
- iOS UIImage:获取图片主色调
本文转载至 http://www.wahenzan.com/a/mdev/ios/2015/0325/1677.html -(UIColor*)mostColor{ #if __IPHONE_OS_V ...
- 《转》Python学习(17)-python函数基础部分
http://www.cnblogs.com/BeginMan/p/3171977.html 一.什么是函数.方法.过程 推荐阅读:http://www.cnblogs.com/snandy/arch ...
- django进阶-查询(适合GET4以上人群阅读)
前言: 下篇博客写关于bootstrap... 一.如何在脚本测试django from django.db import models class Blog(models.Model): name ...
- 学了Python可以做什么工作
学了Python可以做什么工作 用 Python 写爬虫 据我所知很多初学 Python 的人都是使用它编写爬虫程序.小到抓取一个小黄图网站,大到一个互联网公司的商业应用.通过 Python 入门爬虫 ...
- 零基础读懂视频播放器控制原理——ffplay播放器源代码分析
版权声明:本文由张坤原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/535574001486630869 来源:腾云阁 ht ...