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. submit与onsubmit(转)

    发生顺序:onsubmit -> submit 1.阻止表单提单: <script>function submitFun(){    //逻辑判断    return true; / ...

  2. 如何将js与HTML完全脱离

    先举出一个例子: var sound='Roar!'; function myOrneryBeast(){ alert(this); this.style.color='green';//this指代 ...

  3. codeforces 10 D. LCIS LCIS O(n^2)算法

    题目链接 给出两个序列, 求出他们的最长公共上升子序列. 两层循环, 内层循环j, 外层i. 如果a[i] == b[j], 那么dp[j] = max(dp[j], dp[best]+1), bes ...

  4. 走进C标准库(5)——"stdio.h"中的其他部分函数

    函数介绍来自:http://ganquan.info/standard-c/ 函数名: freopen 功  能: 替换一个流 用  法: FILE *freopen(char *filename, ...

  5. Sencha Touch对DOM的访问及控制

    HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  6. SQL Server 日志截断

    截断事务日志是逻辑操作,只是把日志的一部分标记为‘不再需要’所以可以重用这个空间,截断不是物理操作,不会减少磁盘上文件的大小, 要减小物理大小必定要进行收缩. ----------- 有时就算是备份都 ...

  7. poj1552---枚举

    #include <stdio.h> #include <stdlib.h> int main() { ],th=,i,j; while(scanf("%d" ...

  8. nfs nobody,nobody 需要在nfs客户端修改从nfs服务器端共享过来的目录怎么办?

    1,加入我们使用nfs共享安装oracle, 安装oracle需要修改base,data,orainventory等等目录及自目录的属主及权限,一般会继承nfs客户端目录的权限及属主 groupadd ...

  9. HDU 4661 Message Passing 【Tree】

    题意: 给一棵树,每一个结点都有一个信息,每一个时刻,某一对相邻的结点之间可以传递信息,那么存在一个最少的时间,使得所有的节点都可以拥有所有的信息.但是,题目不是求最短时间,而是求最短时间的情况下,有 ...

  10. XmlSerializer

    XmlSerializer作用是将对象序列化到 XML 文档中和从 XML 文档中反序列化对象.XmlSerializer 使您得以控制如何将对象编码到 XML 中. 所在的命名空间:System.X ...