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)的更多相关文章

  1. [LA3026]Period

    [LA3026]Period 试题描述 For each prefix of a given string S with N characters (each character has an ASC ...

  2. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  3. hdu oj Period (kmp的应用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  4. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  5. poj1961 Period kmp解决找字符串的最小循环节

    /** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...

  6. HDU1358 Period —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  7. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  9. UVA 1328 - Period KMP

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1 ...

随机推荐

  1. POJ1505&amp;&amp;UVa714 Copying Books(DP)

    Copying Books Time Limit: 3000MS Memory Limit: 10000K Total Submissions: 7109 Accepted: 2221 Descrip ...

  2. Android 开源项目源码解析(第二期)

    Android 开源项目源码解析(第二期) 阅读目录 android-Ultra-Pull-To-Refresh 源码解析 DynamicLoadApk 源码解析 NineOldAnimations ...

  3. Samba &amp; Nginx - Resource temporarily unavailable

    先说说本人的开发环境:Win7 + Editplus + VMware(Centos+Samba+Nginx).用Samba在Centos上把web文件夹(如www)共享,然后在Win7上訪问这个文件 ...

  4. FZU2181+poj2942(点双连通+判奇圈)

    分析:我们对于那些相互不憎恨的人连边,将每次参加会议的所有人(不一定是全部人,只需人数>=3且为奇数)看做一个点双联通分量,那么每个点都至少有两个点与他相邻.即需要保证双联通分量中存在奇圈.至于 ...

  5. SpringMVC @ResponseBody 415错误处理

    在查看下面部分内容之前,请先检查你的请求蚕食是否正确,如果全部正确,请继续往下看 刚开始用SpringMVC, 页面要使用jQuery的ajax请求Controller. 但总是失败,主要表现为以下两 ...

  6. SWT中Display和Shell是个什么东东

    Display:与操作系统沟通的桥梁 我们在前面说过,每个swt程序在最开始都必须创建一个Display对象.Display对象起什么作用呢?它是swt与操作系统沟通的一座桥梁.它负责swt和操作系统 ...

  7. 关于cocos2dx3.0 UITextField不能使用退格键删除字符的解决方式

    近日開始将项目移植到cocos2dx 3.0版本号,出现了一些问题,UI方面眼下就发现UITextField控件不能响应退格键或者删除键,在Windows以下调试如此,我開始以为是平台支持不好,后来公 ...

  8. Java--调试--单步调试,断言,单元测试

    单步调试:主要查看变量内容的变化    1.设置断点位置,设置在可能出现问题的代码 2.以Debug as方式运行 程序 3.F5 --> step into 进入方法内部进行调试    F6 ...

  9. SQL声明发育异常导致项目错误

    1.错误叙述性说明 严重:Exception occurred during processing request:Statement Callback;SQL[   ];OALL8处于不一致状态; ...

  10. 【译】使用微软企业库5.0进行WCF服务边界上的异常保护

    在Windows Communication Foundation (WCF)中,为了阻止服务的实现细节从服务的安全边界泄露,未知的异常不应该被发送至客服端.在WCF配置中将<serviceDe ...