Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 13511   Accepted: 6368

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want
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

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with
a line, having the 

number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single
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

Southeastern Europe 2004

和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求最小循环节)的更多相关文章

  1. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  2. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  3. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  4. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  5. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  6. 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

    Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...

  7. KMP + 求最小循环节 --- HDU 1358 Period

    Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...

  8. KMP解决最小循环节问题

    # 10035. 「一本通 2.1 练习 1」Power Strings [题目描述] 给定若干个长度 $\le 10^6$​​ 的字符串,询问每个字符串最多是由多少个相同的子字符串重复连接而成的.如 ...

  9. nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】

    循环小数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 我们可爱的 c小加 近段儿正在潜心研究数学,当他学习到循环小数这一部分时不是太明白循环体是什么意思(比如说3 ...

  10. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

随机推荐

  1. JAVAEE之--------过滤器设置是否缓存(Filter)

    在网页中.每次的client訪问server.有部分不用反复请求.如有些图片,视频等就没有必要每次都请求,这样会让server增大工作量.为了防止这样.我们採用过滤器来设置client是都缓存. 參考 ...

  2. ubuntu13.10升级到ubuntu14.04

    Upgrading from Ubuntu 13.10 To upgrade from Ubuntu 13.10 on a desktop system: Open Software Sources. ...

  3. struts2在action中获取request、session、application,并传递数据

    假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...

  4. UVA 11987 Almost Union-Find 并查集单点修改

                                     Almost Union-Find I hope you know the beautiful Union-Find structur ...

  5. TS2

    //还要注意的是,在构造函数的参数上使用public等同于创建了同名的成员变量. class Student { fullName: string; constructor(public firstN ...

  6. oracle 实时查询最耗CPU资源的SQL语句

    1. 先通过top命令查看产用资源较多的spid号 2.查询当前耗时的会话ID,用户名,sqlID等: select sid,serial#,machine,username,program,sql_ ...

  7. SOAPUI 安装及破解

    转自:https://blog.csdn.net/henni_719/article/details/79000130 先下载SOAPUI,我这里用的是5.1.2 PRO  版 下载路径:http:/ ...

  8. ubuntu 使用阿里云 apt 源

    以下内容来自 https://opsx.alibaba.com/mirror Ubuntu对应的“帮助”信息 修改方式:打开 /et/apt/sources.list 将http://archive. ...

  9. Ubuntu16.04系统安装搜狗输入法

    前言:正常双击.deb软件包安装搜狗输入法会有bug,需要按照下面操作进行消除错误. 一.下载搜狗输入法Linux版软件包 下载地址为:http://pinyin.sogou.com/linux/ , ...

  10. ListView的Item动画

    1.效果图 2.需求就是点击item歌曲时,实现一种飞入到预约按钮处的效果 3.思路:在布局文件中加入了一个条目布局,和listView的item一样,点击listView的item时,使用给条目布局 ...