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. Linux命令ln的使用

    ln是linux中一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个链接,这个命令最常用的参数是-s,具体用法是:ln –s 源文件 目标文件. 当我们需要在不同的目录,用到相同的文件时, ...

  2. 单链表反转的递归实现(Reversing a Linked List in Java, recursively)

    转自Reversing a Linked List in Java, recursively There's code in one reply that spells it out, but you ...

  3. [笨木头FireFly01]入门篇1·最简单的服务端和客户端连接

    原地址:http://www.9miao.com/question-15-53938.html 最近一直在写游戏,几乎没有来写教程了,打算放慢一下脚步,学学新东西.那为嘛我要学FireFly呢? 之前 ...

  4. Windows平台上C++开发内存泄漏检查方法

    充分的利用调试工具可以非常方便地避免内存泄漏问题. 这里介绍两种方法,互为补充,第一种是VC编译器提供的方法,第二种是专用的内存泄漏检查工具Memmory Validator.这两种方法的基本原理是一 ...

  5. 缺少编译器要求的成员“System.Runtime.CompilerServices.ExtensionAttribute..ctor” 解决方案

    静态类中添加如下.此方法本人测试有效. //缺少编译器要求的成员“ystem.Runtime.CompilerServices.ExtensionAttribute..ctor” namespace  ...

  6. java中存在的内存泄漏

    大家都知道JAVA有着自己的优点---垃圾回收器的机制,这个开发人员带来了很大的方便,不用我们编程人员去 控制内存回收等问题,有效的解决了内存泄露的问题,不至于导致系统因内存问题崩溃.为了精确的回收内 ...

  7. Netty实现高性能RPC服务器

    在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...

  8. javascript 小日历

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  9. 不只是技术!成为IT经理必备的十大软技能

    摘要:可能你是一名普通的IT从业员,一个小小的程序员,可随着社会的发展和科技的进步,对人才的要求越来越高,你可能通过技术获得了职位,但你若想升职加薪却少不了软技能:谈判技巧.积极倾听.演讲技巧以及领导 ...

  10. bzoj3211,bzoj3038

    线段树的裸题: 但是操作很奇怪,开方是不能lazy tag的 看来只能暴力修改了 但注意,开放开到1的时候就不用开,立一个flag就可以了 这可以大大的优化: 其实我是来复习线段树的 ..] of i ...