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. Toast分析——实现自己的Toast

    android 4.0以后,新增了一个功能:关闭某个应用发出的通知.Toast等.详细操作为:打开应用安装列表.找到要屏蔽的应用(长按通知,点击弹出的"应用信息",就可以跳到应用信 ...

  2. (转)c++ typedef 函数指针详细说明

    转自:http://blog.csdn.net/future200x/article/details/5350134 一个函数在编译时被分配一个入口地址,将这个入口地址称为函数的指针,可以用一个指针变 ...

  3. 广东工业大学2016校赛决赛-网络赛 1174 Problem F 我是好人4 容斥

    Problem F: 我是好人4 Description 众所周知,我是好人!所以不会出太难的题,题意很简单 给你n个数,问你1000000000(含1e9)以内有多少个正整数不是这n个数任意一个的倍 ...

  4. Django迁移到mysql数据库时的错误

    pip install mysqlclient Collecting mysqlclient Using cached https://files.pythonhosted.org/packages/ ...

  5. HttpClient简单操作

    HttpClient 这个框架主要用来请求第三方服务器,然后获取到网页,得到我们需要的数据: HttpClient设置请求头消息User-Agent模拟浏览器 比如我们请求 www.tuicool.c ...

  6. VB.net 捕获项目全局异常

    在项目中添加如下代码:新建窗口来显示异常信息. Namespace My '全局错误处理,新的解决方案直接添加本ApplicationEvents.vb 到工程即可 '添加后还需要一个From用来显示 ...

  7. Excel里的多列求和(相邻或相隔皆适用)

    最近,需要这个知识点,看到网上各种繁多复杂的资料,自己梳理个思路. 不多说,直接上干货! 简述:将L列.M列和N列,相加放到O列.(当然这里是相邻的列).同时,也可以求相隔几列的某些列相加.

  8. 不用copy代码--eclipse使用git提交项目-转

    原文地址:http://blog.csdn.net/u014079773/article/details/51595127 准备工作: 目的:eclipse使用git提交本地项目,提交至远程githu ...

  9. Java基础——过滤器和监听器

    什么是过滤器? Servlet过滤器和Servlet十分相似,但它具有拦截客户端请求的功能,Servlet过滤器可以改变请求中的内容,来满足实际开发中的需要.对于开发人员而言,过滤器实际上就是在Web ...

  10. TCP/IP 三次握手和HTTP过程

    0 引言 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在“无差别”的网络之上. 1 T ...