LA3026 - Period(KMP)
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模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
using namespace std; const int maxn = 1000000+10;
int next[maxn];
int n;
string str; vector<int> find(string T,string P){
int n = T.size(),m = P.size();
vector<int> vn;
getNext(P,next);
for(int i = 0,j = 0; i < n; i++){
while(j&&P[j] != T[i]) j = next[j];
if(P[i]==T[j]) j++;
if(j==m) vn.push_back(i-m+1);
}
return vn; } void getFail(string st,int *f){
int m = st.size();
next[0] = 0;
next[1] = 0;
int len = 1;
for(int i = 1; i < m; i++){
int j = next[i];
while(j && st[i] != st[j]) j = next[j];
if(st[i]==st[j]){
next[i+1] = j+1;
}else{
next[i+1] = 0;
}
}
}
int main(){
int T = 1;
while(~scanf("%d",&n) && n){
cin >> str;
printf("Test case #%d\n",T++);
getFail(str,next);
for(int i = 2; i <= n; i++){
if(next[i]> 0&&i%(i-next[i])==0){
cout<<i<<" "<<i/(i-next[i])<<endl;
}
}
cout<<endl;
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
LA3026 - Period(KMP)的更多相关文章
- [LA3026]Period
[LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- hdu oj Period (kmp的应用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- poj1961 Period kmp解决找字符串的最小循环节
/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- UVA 1328 - Period KMP
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1 ...
随机推荐
- GNU libmicrohttpd 0.9.29 发布 - 开源中国社区
GNU libmicrohttpd 0.9.29 发布 - 开源中国社区 GNU libmicrohttpd 0.9.29 发布
- HTML5系列之——applicationCache对象
ApplicationCache主要简单介绍: applicationCache对象实现HTML5相应WEB离线功能.以下我们来主要解说applicationCache对象的一些主要功能和方法 app ...
- win8 远程桌面 你得凭证不工作
今天在在登录azure远程桌面时,一直提示你的凭证不工作, 按照网上各种教程都不行, 后来发现在win8中用户名结构中,需要加上本机电脑名比如:“win8\administrator”.
- httpclient超时总结(转)
Httpclient超时 背景: 网站这边多次因为httpclient调用超时时间没设置好导致关掉,影响非常不好,而且问题重复出现,查看网络,没有比较明确介绍httpclient所有超时相关的设置(大 ...
- Spring集成XFire开发WebService
Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...
- 分享3一个博客HTML5模板
1.材类别:半透明 博客html模板 个人博客 半透明html5博客主题,半透明,博客,博客html模板,个人博客,html5,灰色,半透明html5博客主题是一款适合用于个人博客主题,风格非常不错. ...
- Object.wait()的使用方法示例(转)
本文节选自 Effective Java by Joshua Bloch 和 Concurrent Programming in Java by Doug Lea. 1.3 原子数据的同步 java语 ...
- pan
百度云盘地址第一季http://pan.baidu.com/share/link?shareid=198342&uk=1191518428第二季http://pan.baidu.com/sha ...
- php获取分类以下的全部子类方法
获取分类以下的全部子类方法: static function getMenuTree($arrCat, $parent_id = 0, $level = 0,$all=True) { static $ ...
- 《Node.js In Action》笔记之流程控制
转向Javascript后,开始学习了node.js,选择的第一本书是<Node.js in Action> 将近一个月时间,断断续续看完,选几个点做下笔记 1.实现串行化流程控制 var ...