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 ...
随机推荐
- MySQL存储过程的创建及调用
阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 1.创建存储过程 2.调用存储过程 3.存储过程体 4.语句块标签 存储过程的参数 1.in:向过程里传参 2.out:过 ...
- Android中显示照片的Exif信息
package com.hyzhou.pngexifdemo; import android.media.ExifInterface; import android.os.Bundle; import ...
- linux关闭喇叭
beep时常响起有时是很烦人的一件事情,在登录linux的时候关闭喇叭可以进行如下操作:在 Linux 控制台下(没有 X11),你可以使用一下命令:在 ~/.bash_profile中写入sette ...
- 关于 Handler 与 opener
我们可以使用 urllib.request.Request() 构造请求对象,但是对于一些更高级的操作,比如 Cookies 处理.代理设置 .身份验证等等,Request() 是处理不了的这时就需要 ...
- PyQt4显示提示信息
我们可以为任何窗口部件设置一个气球提示. #!/usr/bin/python # -*- coding:utf-8 -*- import sys from PyQt4 import QtGui fro ...
- Oralce分析函数
1 列传行 listagg(city,',') within GROUP (order by city) over (partition by nation) rank with temp ...
- Qt编写网络中转服务器(开源)
需求1:手机端或者其他端可以对设备进行回控,并查看设备各种运行状态,接收报警推送等.2:同时支持在局域网.广域网.互联网访问,尤其是互联网访问.3:权限控制,给定账号控制授权的设备,并自动拉取设备信息 ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot初识
项目代码地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/Spring-boot-helloworld 一.Spring B ...
- chrome插件开发,易懂
- document.visibilityState 监听浏览器最小化
document.hidden:表示页面是否隐藏的布尔值.页面隐藏包括 页面在后台标签页中 或者 浏览器最小化 (注意,页面被其他软件遮盖并不算隐藏,比如打开的 sublime 遮住了浏览器). do ...