【暑假】[实用数据结构]UVAlive 3026 Period
UVAlive 3026 Period
题目:
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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的更多相关文章
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- 【暑假】[实用数据结构]UVAlive 4329 Ping pong
UVAlive 4329 Ping pong 题目: Ping pong Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: % ...
- 【暑假】[实用数据结构]UVAlive 3027 Corporative Network
UVAlive 3027 Corporative Network 题目: Corporative Network Time Limit: 3000MS Memory Limit: 30000K ...
- 【暑假】[实用数据结构]UVAlive 3644 X-Plosives
UVAlive X-Plosives 思路: “如果车上存在k个简单化合物,正好包含k种元素,那么他们将组成一个易爆的混合物” 如果将(a,b)看作一条边那么题意就是不能出现环,很容易联想到K ...
- 【暑假】[实用数据结构]UVAlive 4670 Dominating Patterns
UVAlive 4670 Dominating Patterns 题目: Dominating Patterns Time Limit: 3000MS Memory Limit: Unkn ...
- UVALIVE 3026 Period
题意:给你一个字符串,问第i位前是否有循环节,若存在,则循环节是多少? 思路:考察失配函数f[i]的意义.只要i%(i-f[i])==0,则循环节长度为i/(i-f[i]).字符在[0,f[i]],[ ...
- UVALive - 3026 Period kmp next数组的应用
input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...
- UVALive 3026 Period (KMP算法简介)
kmp的代码很短,但是不太容易理解,还是先说明一下这个算法过程吧. 朴素的字符串匹配大家都懂,但是效率不高,原因在哪里? 匹配过程没有充分利用已经匹配好的模版的信息,比如说, i是文本串当前字符的下标 ...
随机推荐
- redis 在windows 上的安装与使用
1.redis-windows 最近在做一个抢拍模块,由于过于平凡的insert与update I/O受不了,故只好把东西放内存里,等拍卖结束了,在写入磁盘. 至于为什么要用window呢? 因为服务 ...
- 解决服务器断电导致mysql数据库无法启动
1.找到mysql数据库目录下的*.ini这个配置文件2.复制配置文件到桌面,当作备份3.打开配置文件,在配置文件里加上innodb_force_recovery=6这句,保存,然后重启服务器4重启完 ...
- window live writer的曲折安装过程
之前一直使用windows live writer2012写日志,由于之前重装了系统,所以需要重新安装writer,本以为是一个很简单的过程,你就是安装个软件吗.... 然而事实是... ...
- spring4+hibernate3
环境说明:spring4.0+hibernate3 数据库:oracle 连接池:c3p0 项目结构: lib中的jar: 一.配置spring.xml 说明:这里采用的配置模式将hibernateT ...
- 【技术贴】webservice cxf2 客户端动态调用报错No operation was found with the name
No operation was found with the name xxx 出错原因是因为发布服务的接口所在包路径和此接口实现类包路径不一致,比如你的服务接口可能放在了包com.x.interF ...
- 汇编Ring 3下实现 HOOK API
[文章标题]汇编ring3下实现HOOK API [文章作者]nohacks(非安全,hacker0058) [作者主页]hacker0058.ys168.com [文章出处]看雪论坛(bbs.ped ...
- SQLite数据库简介(转)
大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...
- linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法
环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...
- Objective-C 协议(protocol)
协议(protocol)是Objective-c中一个非常重要的语言特性,从概念上讲,非常类似于JAVA中接口. 一个协议其实就是一系列有关联的方法的集合(为方便后面叙述,我们把这个协议命名为myPr ...
- ***总结:在linux下连接redis并进行命令行操作(设置redis密码)
[root@iZ254lfyd6nZ ~]# cd / [root@iZ254lfyd6nZ /]# ls bin boot dev etc home lib lib64 lost+found med ...