poj1961--Period(KMP求最小循环节)
| Time Limit: 3000MS | Memory Limit: 30000K | |
| Total Submissions: 13511 | Accepted: 6368 |
Description
to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
a line, having the
number zero on it.
Output
space; the prefix sizes must be in increasing order. Print a blank line after each test case.
Sample Input
3
aaa
12
aabaabaabaab
0
Sample Output
Test case #1
2 2
3 3 Test case #2
2 2
6 2
9 3
12 4
Source
和poj1961同样,http://blog.csdn.net/winddreams/article/details/40268107
只是是多加了一个对从头到每一个字符的推断。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int next[1100000] ;
char s[1100000] ;
void getnext(int l)
{
int j = 0 , k = -1 ;
next[0] = -1 ;
while( j < l )
{
if( k == -1 || s[j] == s[k] )
next[++j] = ++k ;
else
k = next[k] ;
}
}
int main()
{
int n , i , num = 1 , l ;
while(scanf("%d", &n) && n)
{
scanf("%s", s);
getnext(n);
printf("Test case #%d\n", num++);
for(i = 1 ; i <= n ; i++)
{
l = i - next[i] ;
if( i%l==0 && i/l != 1 )
printf("%d %d\n", i, i/l);
}
printf("\n");
}
return 0;
}
poj1961--Period(KMP求最小循环节)的更多相关文章
- [KMP求最小循环节][HDU1358][Period]
题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...
- [KMP求最小循环节][HDU3746][Cyclic Nacklace]
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...
- KMP + 求最小循环节 --- POJ 2406 Power Strings
Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- KMP + 求最小循环节 --- HUST 1010 - The Minimum Length
The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
- KMP + 求最小循环节 --- HDU 1358 Period
Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...
- KMP解决最小循环节问题
# 10035. 「一本通 2.1 练习 1」Power Strings [题目描述] 给定若干个长度 $\le 10^6$ 的字符串,询问每个字符串最多是由多少个相同的子字符串重复连接而成的.如 ...
- nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】
循环小数 时间限制:3000 ms | 内存限制:65535 KB 难度:1 描述 我们可爱的 c小加 近段儿正在潜心研究数学,当他学习到循环小数这一部分时不是太明白循环体是什么意思(比如说3 ...
- poj2406 Power Strings (kmp 求最小循环字串)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 47748 Accepted: 19902 ...
随机推荐
- js保留两位小数的解决的方法
var a = 123.456; a = a..toFixed(2); alert(a);//结果:123.46
- WCF:目录
ylbtech-WCF:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.c ...
- C# Keywords - as
记录一下在日常开发过程中遇到的一些C# 基础编程的知识! 希望以后能用的着.知识是在平常的开发过程中去学到的.只有用到了,你才能深入的理解它,并用好它. 本资料来源于:MSND 下面是一些相关的cod ...
- [Tomcat]Tomcat安全设置
1.关闭服务器端口:server.xml默认有下面一行: <Server port="8005" shutdown="SHUTDOWN"> 这样允许 ...
- 5. Longest Palindromic Substring[M]最长回文子串
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- tabBar的图标不被系统渲染
navi.tabBarItem.selectedImage = [[UIImage imageNamed:imageStr]imageWithRenderingMode:UIImageRenderin ...
- Rsync 服务器搭建
Rsync简介 rsync 是一个 Unix 系统下的文件同步和传输工具. 它具备以下特性: 1. 能更新整个目录和树和文件系统 2. 有选择性的保持符号链链.硬链接.文件属于.权限.设备以及时间 等 ...
- Web Api和Asp.Net mvc post请求区别
这是mvc的,mvc的post请求可以这样写参数,但是web api的不行.而且content_type:"application/json" 必须要写 下面这是web api的: ...
- px 与 pt
px:pixel,像素,屏幕上显示的最小单位,用于网页设计,直观方便: pt:point,是一个标准的长度单位,1pt=1/72英寸,用于印刷业,非常简单易用: em:即%,在CSS中,1em=100 ...
- 使用 chrome 扩展 Vimium 实现快捷键关闭其他标签页
Vimium 是一款很强大的键盘操作扩展,几乎所有的操作都可以使用键盘命令完成,还支持自定义按键. 其实 Vimium 很早就支持了关闭其他标签页这个操作,甚至还能关闭左侧标签页和关闭右侧标签页,只是 ...