Period
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 20436   Accepted: 9961

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

题意:

求一个字符串的所有前缀的最短循环节。

思路:

首先,一个字符串要是可以由他的子串循环而成的话,那么这个字符串的长度一定是子串长度len的倍数。并且一定有S[len+1 ~ i] = S[1 ~ i- len]

而KMP求出的nxt数组,表示的就是对于每一个i,S[i - nxt[i] + 1 ~ i] = S[1 ~ nxt[i]]

因此,当i - nxt[i]能整除i时,S[1 ~ i - nxt[i]]就是S[1 ~ i]的最小循环元。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f int n;
const int maxn = 1e6 + ;
int nxt[maxn];
char s[maxn]; void getnxt()
{
nxt[] = ;
for(int i = , j = ; i <= n; i++){
while(j > && s[i] != s[j + ]){
j = nxt[j];
}
if(s[i] == s[j + ])j++;
nxt[i] = j;
}
} int main()
{
int cas = ;
while(scanf("%d", &n) != EOF && n){
scanf("%s", s + );
getnxt();
printf("Test case #%d\n", cas++);
for(int i = ; i <= n; i++){
if(i % (i - nxt[i]) == && i / (i - nxt[i]) > ){
printf("%d %d\n", i, i / (i - nxt[i]));
}
}
printf("\n");
}
return ;
}

poj1961 & hdu1358 Period【KMP】的更多相关文章

  1. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  2. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  3. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  5. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  6. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  7. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  8. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

  9. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

随机推荐

  1. HTTP/1.1 学习

    发现对于HTTP协议不能脱口而出,故而怒翻资料,RFC2616 . 在其abstract中是这么说HTTP的,应用层协议,generic.无状态.其特点之一是 the typing and negot ...

  2. BinarySearchTree二叉搜索树的实现

    /* 二叉搜索树(Binary Search Tree),(又:二叉查找树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; ...

  3. unity如何停止不用字符串方式开启协程的方法

    通常我们知道开启协程用StartCoroutine("Method"); 停止协程用StopCoroutine("Method"); 如果我们想要终止所有的协程 ...

  4. 第四章 Spring.Net 如何管理您的类___对象的手动装配

    前面我们知道了什么是对象,什么是对象工厂,什么是应用程序上下文.这一次我们来看一下对象的装配. Spring.Net 中有多种装配对象的方式,装配这个词可能比较学术化,我们可以理解为对象的创建. Sp ...

  5. oracle_存储过程_没有参数_根据配置自动创建申请单以及写日志事务回滚

    CREATE OR REPLACE PROCEDURE A_MEAS_MIINSP_PLAN_CREATEASvs_msg VARCHAR2(4000);p_PERIODTYPE number; -- ...

  6. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  7. day06<面向对象>

    面向对象(面向对象思想概述) 面向对象(类与对象概述) 面向对象(学生类的定义) 面向对象(手机类的定义) 面向对象(学生类的使用) 面向对象(手机类的使用) 面向对象(一个对象的内存图) 面向对象( ...

  8. Windows版Nginx启动失败之1113: No mapping for the Unicode character exists in the target multi-byte code page

    Windows版Nginx启动一闪,进程中未发现nginx进程,查看nginx日志,提示错误为1113: No mapping for the Unicode character exists in ...

  9. Oracle函数的使用

    日期转换to_date insert into test (birthday,name) values (to_date('2016-03-12','yyyy-mm-dd'),'zy'); to_ch ...

  10. U3D关于message的使用

    Message相关有3条指令: SendMessage ("函数名",参数,SendMessageOptions) //GameObject自身的Script BroadcastM ...