Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 11356   Accepted: 5279

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 A K ,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

题意:

给你一个字符串。叫你依次输出输出该串长度为i的前缀(如果该前缀可以由重复子串得到)的长度。产生该前缀所需的最大重复次数。

思路:

见训练指南。

详细见代码:

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1000100;
int f[maxn];
char txt[maxn];
void getf(char *p)
{
int i,j,m=strlen(p);
f[0]=f[1]=0;
for(i=1;i<m;i++)
{
j=f[i];
while(j&&p[i]!=p[j])
j=f[j];
f[i+1]=p[i]==p[j]?j+1:0;
}
}
int main()
{
int len,i,t,cas=1; while(scanf("%d",&len),len)
{
scanf("%s",txt);
getf(txt);
printf("Test case #%d\n",cas++);
for(i=2;i<=len;i++)
{
t=i-f[i];
if(f[i]&&i%t==0)
printf("%d %d\n",i,i/t);
}
printf("\n");
}
return 0;
}

poj 1961 Period(KMP训练指南例题)的更多相关文章

  1. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  2. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  3. POJ 1961 Period KMP算法之next数组的应用

    题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...

  4. KMP POJ 1961 Period

    题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...

  5. POJ 1961 Period(KMP)

    http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...

  6. (简单) POJ 1961 Period,扩展KMP。

    Description For each prefix of a given string S with N characters (each character has an ASCII code ...

  7. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  8. LA 3026 && POJ 1961 Period (KMP算法)

    题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...

  9. poj 1961 Period

    Period http://poj.org/problem?id=1961 Time Limit: 3000MS   Memory Limit: 30000K       Description Fo ...

随机推荐

  1. werkzeug源码阅读笔记(二) 下

    wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...

  2. verilog中=和<=的区别

    一般情况下使用<=,组合逻辑使用=赋值,时序逻辑使用<=赋值: 举个例子:初始化m=1,n=2,p=3:分别执行以下语句 1.begin m=n:n=p:p=m: end 2.begin ...

  3. 移动跨平台开发框架Ionic开发一个新闻阅读APP

    移动跨平台开发框架Ionic开发一个新闻阅读APP 前言 这是一个系列文章,从环境搭建开始讲解,包括网络数据请求,将持续更新到项目完结.实战开发中遇到的各种问题的解决方案,也都将毫无保留的分享给大家. ...

  4. web.config中<customErrors>节点

    错误提示: “/”应用程序中的服务器错误.------------------------------------------------------------------------------- ...

  5. TLC是什么

    TLC = Triple-Level Cell,即3bit/cell,它的寿命短,速度慢,约500-5000次擦写寿命. 现在U盘多为MLC,TLC也有一部分,将来TLC会占大部分市场. 一种名为TL ...

  6. openssl编译(VC6.0)

    官网:http://www.openssl.org/ 得到源码: git clone  https://github.com/openssl/openssl 一.用vc编译器编译: 1.下载nasm: ...

  7. c# 多显示器设置主屏幕(Set primary screen for multiple monitors)

    原文 http://www.cnblogs.com/coolkiss/archive/2013/09/18/3328854.html 经过google加各种百度,终于找到了一个有效的解决方案,下面是两 ...

  8. mysql查询语句理解

    看一个查询语句 ,)) as passcount FROM (SELECT b.user,b.full_name,b.user_group From login_log a LEFT JOIN vic ...

  9. openstack 的 policy 问题。

    想写nova的policy的实现, 但是发现网上,有人写的很不错了. ref: http://blog.csdn.net/hackerain/article/details/8241691 但是,po ...

  10. c#搭建服务端 简单中最高效的数据操作Linq (4)

    .NET F 3.5之后提出的linq to sql 概念,大大的简化了对于数据对象的操作,可以通过简单的语法直接操作数据对象,如List,Linq to sql类 等等. 1.使用Linq to s ...