Period

Time Limit: 3000MS Memory Limit: 30000K

Total Submissions: 14280 Accepted: 6773

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

POJ2406与这道题一个意思,就是这道题细化了一点。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std; char a[1000005];
int next1[1000005]; void cal()
{
int len = strlen(a);
int i,j=-1;
next1[0]=-1;
for(i=0;i<len;)
{
if(j==-1||a[i]==a[j])
{
i++;
j++; next1[i]=j;
}
else
{
j=next1[j];
}
}
} int main()
{
int len,count=1;
while(cin>>len)
{
if(len==0)
break; cin>>a;
cal(); int i;
cout<<"Test case #"<<count++<<endl; for(i=2;i<=len;i++)
{
if(i%(i-next1[i])==0 && i/(i-next1[i])>=2)
cout<<i<<" "<<i/(i-next1[i])<<endl;
} cout<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1961:Period的更多相关文章

  1. 【poj 1961】Period(字符串--KMP 模版题)

    题意:给你一个字符串,求这个字符串到第 i 个字符为止的重复子串的个数. 解法:判断重复子串的语句很重要!!if (p && i%(i-p)==0) printf("%d % ...

  2. 【POJ 1961】 Period

    [题目链接] 点击打开链接 [算法] KMP 和POJ2406很像 [代码] #include <algorithm> #include <bitset> #include & ...

  3. KMP POJ 1961 Period

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

  4. Period POJ - 1961

    Period POJ - 1961 时限: 3000MS   内存: 30000KB   64位IO格式: %I64d & %I64u 提交 状态 已开启划词翻译 问题描述 For each ...

  5. POJ 1961 2406 (KMP,最小循环节,循环周期)

    关于KMP的最短循环节.循环周期,请戳: http://www.cnblogs.com/chenxiwenruo/p/3546457.html (KMP模板,最小循环节) POJ 2406  Powe ...

  6. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  7. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  8. poj 1961 Period

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

  9. POJ 1961 Period( KMP )*

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

随机推荐

  1. Java Reflection (JAVA反射) --转载

    对于软件开发人员来说,单元测试是一项必不可少的工作.它既可以验证程序的有效性,又可以在程序出现 BUG 的时候,帮助开发人员快速的定位问题所在.但是,在写单元测试的过程中,开发人员经常要访问类的一些非 ...

  2. js 判断时间大小

    //判断结束时间一定要大于开始时间 function comparativeTime(){ var isok=true; //早餐配送时间 var breakfastScanTimeMin = $(& ...

  3. JAVA--文件内容属性替换

    说明:文件中执行内容是变量,随着环境不同会配置不同,在程序启动后,读取配置进行变量替换 1.测试类如下: public class FileUtilsTest { //public static bo ...

  4. cookie、session、localStorage、sessionStorage的区别

    cookie的机制 cookie是存储在用户本地终端上的数据.有时也用cookies,指某些网站为了辨别用户身份,进行session跟踪而存储在本地终端上的数据,通常经过加密. Cookie是服务器发 ...

  5. 从零开始-建站前的准备之django数据库创建的问题

    稍微熟悉了一下django里面对于数据的操作,发现遇见了好多的问题. django对数据的操作是代码式的操作. 一开始在models里面开始为某个表创建参数,像username,password这样的 ...

  6. DoMes平台首页菜单栏

    问题1:左侧菜单栏数据是在哪里获取的? 答案1: 项目根目录的Views/Home/Index文件为平台首页 打开Index.cshtml文件,有一个framework-clientdata.js引入 ...

  7. 使用JNA替代JNI调用本地方法

    JNA全称是Java Native Access,是Sun推出的一种调用本地方法技术,比起它的同门师兄JNI,JNA大大简化了调用本地方法的过程,使用也比较方便, JNA是在JNI的基础上完善的,用青 ...

  8. 1-Docker学习笔记

    docker还是比较容易的,比较蛋疼的就是镜像网络问题,不过也可以配置了镜像加速器(比如阿里云).这里重点记录一下初学docker时遇到的知识点. docker环境变量 docker对环境变量的定义和 ...

  9. java set的线程安全

    CopyOnWriteArraySet和ConcurrentSkipListSet 与线程不安全的集合类的对应关系 HashSet -> CopyOnWriteArraySet TreeSet ...

  10. [网络必学]TCP/IP四层模型讲解【笔记整理通俗易懂版】

    OSI七层模型     表示层:用来解码不同的格式为机器语言,以及其他功能. 会话层:判断是否需要网络传输. 传输层:识别端口来指定服务器,如指定80端口的www服务. 网络层:提供逻辑地址选路,即发 ...