POJ 1961 Period( KMP )*
Period
Time Limit: 3000MS
Memory Limit: 30000K
Total Submissions: 12089
Accepted: 5656
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
[Submit] [Go Back] [Status] [Discuss]
::这道题跟poj2406差不多。题意:对于选定长度i(前i个字符,2<=i<=N),求出对应循环节k>1的情况
比如: aabaabaabaab
当i=2时, a ,a循环节有2个
当i=6时, aab,aab循环节有2个
当i=9时, aab,aab,aab循环节有3个
当i=12时,aab,aab,aab,aab循环节有4个
假设S的长度为len,则S存在循环子串,当且仅当,len可以被len - next[len]整除,最短循环子串为S[len - next[len]]
证明见POJ 2406 Power Strings (KMP)
代码:
1: #include <iostream>
2: #include <cstdio>
3: #include <cstring>
4: #include <algorithm>
5: using namespace std;
6: const int maxn=1e6;
7: char s[maxn+10];
8: int next[maxn+10];
9:
10: void get_next(char s[],int len)
11: {
12: int i=0,j=-1;
13: next[0]=-1;
14: while(i<len)
15: {
16: if(j==-1||s[i]==s[j]) {i++; j++; next[i]=j;}
17: else j=next[j];
18: }
19: }
20:
21: int main()
22: {
23: int n,cas=1;
24: while(scanf("%d",&n)>0&&n)
25: {
26: scanf("%s",s);
27: printf("Test case #%d\n",cas++);
28: get_next(s,n);
29: for(int i=2; i<=n; i++)//枚举长度i
30: {
31: if(i%(i-next[i])==0&&i!=(i-next[i]))
32: printf("%d %d\n",i,i/(i-next[i]));
33: }
34: printf("\n");
35: }
36: return 0;
37: }
POJ 1961 Period( KMP )*的更多相关文章
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
- POJ 1961 Period KMP算法之next数组的应用
题意:给一个长度为n的字符串,如果它长度为l(2 <= l <= n)的前缀部分是由一些相同的字符串相接而成,输出前缀的长度l和长度为l时字符串重复的最大次数. 例如字符串为: aaaba ...
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- (简单) POJ 1961 Period,扩展KMP。
Description For each prefix of a given string S with N characters (each character has an ASCII code ...
- KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period
首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...
- poj 1961 Period(KMP训练指南例题)
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 11356 Accepted: 5279 Descripti ...
- LA 3026 && POJ 1961 Period (KMP算法)
题意:给定一个长度为n字符串s,求它每个前缀的最短循环节.也就是对于每个i(2<=i<=n),求一个最大整数k>1(如果存在),使得s的前i个字符组成的前缀是某个字符串重复得k次得到 ...
- poj 1961 Period
Period http://poj.org/problem?id=1961 Time Limit: 3000MS Memory Limit: 30000K Description Fo ...
随机推荐
- UnityShader快速上手指南(一)
简介 引言 其实网上有很多shader教程,但是大概看了下,也不知是网上各位大神已经脱离了代码层面的高度还是啥原因.貌似没有找到从代码方面作为入门讲解的,导致了shader对于苦逼程序员入门有一定要求 ...
- jquery 全选 全不选 反选
1.概述 在项目中经常遇到列表中对复选框进行勾选操作,全选...反选.. 2. example <html> <body> <form id="test-for ...
- 那些教程没有的php4-composer依赖管理工具
phpcomposer PHP 5.3.2+ Composer 不是一个包管理器,但它在每个项目的基础上进行管理,在你项目的某个目录中(例如 vendor)进行安装.默认情况下它不会在全局安装任何东西 ...
- javascript 之正则匹配HTML
正则表达式 <(\S*?) [^>]*>.*?</\1>|<.*? /> 匹配 <html>hello</html>|<a> ...
- SystemClock.sleep和Thread.sleep的区别(转)
在Java中我们处理线程同步问题时,处理延迟可能会使用Thread类的sleep方法,这里抛开concurrent类的一些方法,其实 Android平台还提供了一个SystemClock.sleep方 ...
- js的基本数据类型有哪些?
js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined.Null.Boolean.Number和String.还有1中复杂的数据类型----O ...
- 同源策略和JSONP(概念性)
同源策略 浏览器有一个很重要的概念——同源策略(Same-Origin Policy). 所谓同源是指,域名,协议,端口相同.不同源的客户端脚本(javascript.ActionScript)在没明 ...
- Access sql语句创建表及字段类型
创建一张空表: Sql="Create TABLE [表名]" 创建一张有字段的表: Sql="Create TABLE [表名]([字段名1] MEMO NOT NUL ...
- 聊聊GISer的职业发展
一.前言 去年写了一篇名为<GISer们还有机会屌丝逆袭吗?>的博文,希望能和广大GISer一起探讨地理信息产业留给小团队和个人的机会.文章发布后,很多GISer通过网络和我进行了交流,其 ...
- Oracle EBS在编码方式为AL32UTF8时的注意事项
现如今的EBS系统中,为了推进国际化的进程,以及系统向全球化的扩展,在Oracle数据库的编码方式上渐渐从支持中国本土简体中文的ZHS16GBK转向了更趋于国际化的AL32UTF8编码方式.但随之而来 ...