【暑假】[实用数据结构]UVAlive 3026 Period

UVAlive 3026 Period

题目:

Period

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit Status

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 file 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

思路:  利用KMP算法中的失配函数,如果是一个循环字串那么(i-f[i])必为一个循环节,因此只要判断(i%(i-f[i])==0) 可以整除则是循环字串。

代码:
 1 #include<cstdio>
 2 #define FOR(a,b,c) for(int a=(b);a<(c);a++)
 3 using namespace std;
 4
 5 const int maxn = 1000000 + 10;
 6 char s[maxn];
 7 int f[maxn];    //大数组注意要开在函数外包括main
 8
 9 int main(){
10 int kase=0,n;
11   while(scanf("%d",&n)==1 && n){
12       scanf("%s",s);
13
14       f[0]=f[1]=0;
15       FOR(i,1,n){
16           int j=f[i];
17           while(j &&  s[i] != s[j]) j=f[j];
18           f[i+1]= s[i]==s[j]? j+1:0;
19       }
20
21       printf("Test case #%d\n",++kase);
22       FOR(i,2,n+1){  //L+1
23           int k=i-f[i];
24           if(f[i]>0 && (i%k)==0)
25             printf("%d %d\n",i,i/k);
26       }
27       printf("\n");
28   }
29   return 0;
30 } 



需要注意大数组的声明位置。
时间: 2024-10-26 22:27:11

【暑假】[实用数据结构]UVAlive 3026 Period的相关文章

UVALive - 3026 - Period (KMP)

UVALive - 3026 Period Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status 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

【暑假】[实用数据结构]UVAlive 3135 Argus

UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, fin

【暑假】[实用数据结构]UVAlive 3942 Remember the Word

UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Know

【暑假】[实用数据结构]UVAlive 3027 Corporative Network

UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 3450   Accepted: 1259 Description A very big corporation is developing its corporative network. In the beginning each of the N ent

【暑假】[实用数据结构]UVAlive 3644 X-Plosives

UVAlive X-Plosives 思路:    “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到Kruskal算法中并查集的判环功能(新加入的边必须属于不同的两个集合否则出现环),因此本题可以用并查集实现.模拟装车过程即可. 代码: 1 #include<cstdio> 2 #include<cstring> 3 #define FOR(a,b,c) for(int a=(b);a

【暑假】[实用数据结构]UVAlive

题目:   Dominating Patterns Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description The archaeologists are going to decipher a very mysterious ``language". Now, they know many language patterns; each patt

UVALIVE 3026 Period

题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[i-f[i],i]范围内的相等,所以如果存在循环节则每i-f[i]可以分为一段.理解起来比较抽象,模拟一遍. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib

Uvalive - 3026 Period (kmp求字符串的最小循环节+最大重复次数)

参考:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html 总结一下,如果对于next数组中的 i, 符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且 循环节长度为:   i - next[i] 循环次数为:       i / ( i - next[i] ) 1 #include <iostream> 2 #include <cstdi

UVALive 3026 Period (KMP算法简介)

kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标,j是要匹配的模版串当前正在匹配的字符的下标.(下标都从零开始) 当匹配到i = 4, j = 4的时候失配了,朴素的匹配做法是往右边移一位然后从j开始扫,这样做效率很低. 不难发现前面已经匹配好的串ab是相同的最大前缀后缀.把串移动到后缀的第一个位置正好是 朴素的匹配过程中第一次匹配能把这个前缀匹