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 ...
随机推荐
- Eclipse------用Tomcat运行项目后出现:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
Eclipse中Tomcat运行项目后出现: 严重: Error configuring application listener of class org.springframework.web.c ...
- ios开发之--数组的一些操作
1,创建数组 NSMutableArray * array =[[NSMutableArray alloc] initWithObjects:@"a",@"b" ...
- Python生成器笔记
Python中三大器有迭代器,生成器,装饰器,本文主要讲述生成器.主要从生成器的概念,本质,以及yield关键字的使用执行过程. 本质:生成器是一类特殊的迭代器,使用了yield关键字的函数不再是函数 ...
- linux 设置分辨率(转)
linux 设置分辨率 如果你需要在linux上设置显示屏的分辨率,分两种情况:分辨率模式存在与分辨率模式不存在,具体如下. 1,分辨率模式已存在 1)如何查询是否存在: 图形界面:在System S ...
- codeforces水题100道 第四题 Codeforces Round #105 (Div. 2) A. Insomnia cure (math)
题目链接:http://www.codeforces.com/problemset/problem/148/A题意:求1到d中有多少个数能被k,l,m,n中的至少一个数整出.C++代码: #inclu ...
- C# 压缩 SharpZipLib
zip压缩与解压缩: 官方网站:http://icsharpcode.github.io/SharpZipLib/ 官网下载的资源并不是能够直接运行的,感觉是这个dll的编译,开源的 参考文档:htt ...
- OpenCV——识别各省份地图轮廓
好久没有发OpenCV的博客了,最近想到了一个识别地图轮廓的方案,就写来试试.(识别中国的28个省份地图轮廓,不考虑直辖市) 首先,我的基本思路是 用最小的矩形将地图的轮廓圈出来,可以根据长方形的长 ...
- 【大数据系列】在hadoop2.8.0下配置SecondaryNameNode
修改namenode上的hdfs-site.xml configuration> <property> <name>dfs.replication</name> ...
- 【Eclipse】Ubuntu 下菜单栏失效了,怎么办?(已解决)
如果你的 Ubuntu 的版本是 13.10 , 且你又安装了 Eclipse , 你就会发现 Eclipse 的菜单不起作用了. 就是点击 File , Edit ... 这些菜单,不会显示子菜单了 ...
- Android Security Internals