Period

Time Limit: 3000MS
Memory Limit: 30000K

Total Submissions: 12089
Accepted: 5656

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
                                        [Submit]   [Go Back]   [Status]   [Discuss]
 
::这道题跟poj2406差不多。题意:对于选定长度i(前i个字符,2<=i<=N),求出对应循环节k>1的情况
比如: aabaabaabaab
当i=2时, a ,a循环节有2个
当i=6时, aab,aab循环节有2个
当i=9时, aab,aab,aab循环节有3个
当i=12时,aab,aab,aab,aab循环节有4个
假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]
证明见POJ 2406 Power Strings (KMP)
 
代码:
   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <algorithm>

   5: using namespace std;

   6: const int maxn=1e6;

   7: char s[maxn+10];

   8: int next[maxn+10];

   9:  

  10: void get_next(char s[],int len)

  11: {

  12:     int i=0,j=-1;

  13:     next[0]=-1;

  14:     while(i<len)

  15:     {

  16:         if(j==-1||s[i]==s[j]) {i++; j++; next[i]=j;}

  17:         else j=next[j];

  18:     }

  19: }

  20:  

  21: int main()

  22: {

  23:     int n,cas=1;

  24:     while(scanf("%d",&n)>0&&n)

  25:     {

  26:         scanf("%s",s);

  27:         printf("Test case #%d\n",cas++);

  28:         get_next(s,n);

  29:         for(int i=2; i<=n; i++)//枚举长度i

  30:         {

  31:             if(i%(i-next[i])==0&&i!=(i-next[i]))

  32:                 printf("%d %d\n",i,i/(i-next[i]));

  33:         }

  34:         printf("\n");

  35:     }

  36:     return 0;

  37: }

POJ 1961 Period( KMP )*的更多相关文章

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

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

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

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

  3. KMP POJ 1961 Period

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

  4. POJ 1961 Period(KMP)

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

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

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

  6. 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中的出现位置或者是次数 这个出现的次数是可 ...

  7. poj 1961 Period(KMP训练指南例题)

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

  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. 重构if...else...或者switch程序块

    我们在开发asp.net时,经常有使用if...else...或者是使用switch来进行多个条件判断.如下面这篇<用户控件(UserControl) 使用事件 Ver2>http://w ...

  2. LODOP打印插件

    HTML代码(请先下载对应LODOP插件安装)  -    打印onclike事件CreatePrintPage()打印函数,LODOP.PREVIEW()打印预览. <div class=&q ...

  3. LeetCode131:Palindrome Partitioning

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  4. The type java.util.Map$Entry cannot be resolved. It is indirectly referenced。。.相似的错误

    这个问题是出现一般都是因为JDK版本的问题.今天公司安装NC的时候就出现了这个问题.经过对错误的分析和猜测,将JDK从1.8i换成了1.7,之后就行了.根据我个人的猜测,可能是1.8以后就不支持Map ...

  5. 从苹果apns的feedback服务器获取推送失败的token

    在开发自己的苹果推送服务时候,要合理的控制ios设备的Token,而这个Token是由苹果服务器Apns产生的,就是每次app问Apns要Token,由苹果服务器产生的Token会记录到Apns里面, ...

  6. KMP的原理详细讲解

    1.kmp算法的原理: 本部分内容转自:http://www.cnblogs.com/c-cloud/p/3224788.html及                           http:// ...

  7. 第三章--Win32程序的执行单元(部分概念及代码讲解)(上 -- 多线程)

    学习<Windows程序设计>记录 概念贴士: 1. 线程描述了进程内代码的执行路径. 2. _stdcall是新标准C/C++函数的调用方法.从底层来说,使用这种调用方法参数的进栈顺序和 ...

  8. hibernate初步4

    JPA 1.JPA概述 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关系映射工具来管理Java应用中的关系数据.,而Hi ...

  9. 一个web页面的访问的过程

    Browers是如何在浩瀚的互联网上找到我们需要的资源呢? 以下将记录这个过程,这个过程是web编程需要需要熟知的. 用户打开浏览器输入目标地址(比如http://www.sina.com),那么接下 ...

  10. SharpGL学习笔记(十九) 摄像机漫游

    所谓的摄像机漫游,就是可以在场景中来回走动. 现实中,我们通过眼睛观察东西,身体移动带动眼睛移动观察身边的事物,这也是在漫游. 在OpenGL中我们使用函数LookAt()来操作摄像机在三维场景中进行 ...