POJ1961 Period

Period

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 18405   Accepted: 8920

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

Southeastern Europe 2004

【题解】

如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元。使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数。
现在给定一个长度为N的字符串S,对S的每一个前缀S[1~i],如果它的最大循环次数大于1,则输出该前缀的最小循环元长度和最大循环次数。

当i-next[i]能整除i时,S[1~i-next[i]]就是S[1~i]的最小循环元。它的最大循环次数就是i/(i-next[i])。
进一步地,如果i-next[next[i]]能整除i,那么S[1~i-next[next[i]]]就是S[1~i]的次小循环元。
以此类推我们还可以找出S[1~i]所有可能的循环元。

——by李煜东

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6
 7 inline void read(int &x)
 8 {
 9     x = 0;char ch = getchar(), c = ch;
10     while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
11     while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
12     if(c == ‘-‘)x = -x;
13 }
14
15 const int MAXN = 2000000 + 10;
16
17 char s[MAXN];
18 int n,lens,next[MAXN];
19
20 void make_next()
21 {
22     memset(next, 0, sizeof(next));
23     next[0] = -1;
24     for(int i = 1, j = -1; i < lens; ++ i)
25     {
26         while(j >= 0 && s[j+1] != s[i]) j = next[j];
27         if(s[j+1] == s[i]) ++ j;
28         next[i] = j;
29     }
30     for(register int i = 0;i < lens;++ i) ++next[i];
31 }
32
33 int cnt;
34
35 int main()
36 {
37     while(scanf("%d", &lens) != EOF && lens)
38     {
39         ++cnt;
40         scanf("%s", s);
41         printf("Test case #%d\n", cnt);
42         make_next();register int lenp;
43         for(register int i = 2;i <= lens;++ i)
44         {
45             lenp = next[i - 1];
46             if((lenp << 1) >= i && i % (i - lenp) == 0)
47                 printf("%d %d\n", i, i/(i - lenp));
48         }
49         putchar(‘\n‘);
50     }
51     return 0;
52 ;}
时间: 2024-07-30 11:31:18

POJ1961 Period的相关文章

poj1961 Period kmp解决找字符串的最小循环节

/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个),输出前缀字符串长度以及最大的循环周期: 思路: 参考自:http://www.cnblogs.com/chenxiwenruo/p/3546457.html 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0-len-next[len]-1]. (1)

POJ1961 Period (kmp) 题解

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 16462   Accepted: 7903 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

poj1961 Period(KMP)

C - Period Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1961 Appoint description: System Crawler (2016-05-10) Description For each prefix of a given str

poj1961 Period 2012-01-11

http://162.105.81.212/JudgeOnline/problem?id=1961 _____________________________________ 也是求最长重复字串._____________________________________ 1 Program Stone; 2 var i,j,n,m:longint; 3 s:ansistring; 4 b:array[1..1000000]of longint; 5 Begin 6 assign(input,'i

kmp入门小结

void get_next(char *s) { int len = strlen(s); int j = 0; int k = -1; while (j < len){ if (k == -1 || s[j] == s[k]){ j++; k++; next[j] = k; } else k = next[k]; } } 设t = next[i]; next[i] 表示的是 i之前最大的t满足 s[0...t-1]  =  s[i-t...i-1] 比如 0123 4 0123 5 ,next

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 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

今天的工作状态,规划未来一段时间内必须完成的事情(Record the working status of today,planning for the next period of time must be completed)

中文: 今天的工作状态,规划未来一段时间内必须完成的事情 待完成功能:(本周完成,不包括modbus传感器,完成之后就不管了) 1.传感器识别功能框架: 根据四个上拉电阻自动识别工作模式:数字型传感器.模拟形传感器.modebus式传感器 2.类似于红外的FD把STM32远程升级功能实现(思考实现方法,如此大的程序,分段存储吗?待处理)3.基于Zigbee的485的透传实现 业余生活: 1.把以上功能实现,ESP8266的AT指令掌握使用,然后基于Linux开发简单的功能(基础) 2.把TI的C

POJ Period 1961【KMP】

Language: Default Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14658   Accepted: 6968 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 t

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 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