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 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) 可以整除则是循环字串。 代码:
 #include<cstdio>
#define FOR(a,b,c) for(int a=(b);a<(c);a++)
using namespace std; const int maxn = + ;
char s[maxn];
int f[maxn]; //大数组注意要开在函数外包括main int main(){
int kase=,n;
while(scanf("%d",&n)== && n){
scanf("%s",s); f[]=f[]=;
FOR(i,,n){
int j=f[i];
while(j && s[i] != s[j]) j=f[j];
f[i+]= s[i]==s[j]? j+:;
} printf("Test case #%d\n",++kase);
FOR(i,,n+){ //L+1
int k=i-f[i];
if(f[i]> && (i%k)==)
printf("%d %d\n",i,i/k);
}
printf("\n");
}
return ;
}



需要注意大数组的声明位置。

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

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

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

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

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  3. 【暑假】[实用数据结构]UVAlive 4329 Ping pong

    UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: % ...

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

    UVAlive 3027 Corporative Network 题目:   Corporative Network Time Limit: 3000MS   Memory Limit: 30000K ...

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

    UVAlive X-Plosives 思路:    “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物”  如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...

  6. 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns

    UVAlive 4670 Dominating Patterns 题目:   Dominating Patterns   Time Limit: 3000MS   Memory Limit: Unkn ...

  7. UVALIVE 3026 Period

    题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...

  8. UVALive - 3026 Period kmp next数组的应用

    input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...

  9. UVALive 3026 Period (KMP算法简介)

    kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...

随机推荐

  1. centos7 更新yum安装源

    系统自带的yum安装源有些软件找不到  这里我们使用阿里云的源 1.加源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/re ...

  2. 闭包小demo

    var a = (function(){ var c= 0; return function(){ return ++c; } }()); var g = a(); console.log(g); v ...

  3. uva 10056

    概率 Q += p*pow(1-p, i*n+k-1) i = 0,1,2,3...... #include <cstdio> #include <cmath> int mai ...

  4. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  5. struts2 标签的使用之一 s:if

    struts2 的web 项目中为了方便的编写jsp,标签是最好的选择 1:struts2 标签库的定义在**-core-版本号.jar META-INF 路径下找到struts-tags.tld文件 ...

  6. Android GridView、ListView、ScrollView上下拉刷新

    实现方法是将显示的内容最外层的ViewGroup做成一个LinearLayout,并扩展它,使其可以上下拖动. 重点是实现View的onTouch方法. 下载:http://files.cnblogs ...

  7. http://www.linuxidc.com/Linux/2007-09/7399.htm

    http://www.linuxidc.com/Linux/2007-09/7399.htm

  8. Ubuntu 12.04安装字体

    http://www.2cto.com/os/201210/160645.html 安装方法,终端输入:  $ sudo thunar /usr/share/fonts/truetype  待ture ...

  9. GridBagLayout()的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很复杂的布局,可以说GridBagLayout是必须要学好的的, GridBagLayout 类是一个灵活的布局管理器,它不要求 ...

  10. SQL Server 2012 连接到数据库引擎

    第 1 课:连接到数据库引擎 https://msdn.microsoft.com/zh-cn/library/ms345332(v=sql.110).aspx   本课将介绍主要的工具以及如何连接并 ...