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. Delphi过程函数传递参数的八种方式

    今天一同事问我为什么有些过程函数里面有Var而有些没有,不解,遂到网上百度,得解.快哉,快哉. 在Delphi过程.函数中传递参数几个修饰符为Const.Var.Out.另一种不加修饰符的为默认按值传 ...

  2. 一种单片机支持WiFi的应用——SimpleWiFi在单片机中的应用

    一种单片机支持WiFi的应用——SimpleWiFi在单片机中的应用 先上图: 现在的智能控制都是基于微控制器,随着智能的手持终端的普及,基于智能终端的控制就会越来越普遍. WIFI便是其中的一种.W ...

  3. ACM起步要点总结(转哈工大)

    首先,我想说的就是,我是一个很普通的ACMer,高中没有参加过任何计算机和数学竞赛的经历,也没有ben那样过人的天资,努力至今也未能取得什么成绩,我之所以写下这篇文章,只是希望给刚进大学或者刚进ACM ...

  4. Error : APP-FND-01926: The custom event WHEN-LOGON-CHANGED raised unhandled exception: ORA-06502: PL

    In this Document   _afrLoop=440418974213449&id=1508865.1&_afrWindowMode=0&_adf.ctrl-stat ...

  5. iBeacon怎样工作

    原文地址 iBeacons iBeacons近期是一个趋势的话题,它们同意室内定位,让你的电话知道你在基站的范围.这个能有很多应用:在停车场帮你找到你的车,零售商通过优惠券和基于位置的特别优惠,以至很 ...

  6. mongodb - 前端form表单数据传输,在保存和清除的数据格式的处理程序的 - 非递归

    //处理时间段,将ISODate("2014-10-09T18: 37: 50.0Z") 兑换 2014-10-09 18:37:50这样的格式 //截至处理6层树形结构数据,当多 ...

  7. Redis集群明细文档(转)

    相信很多用过Redis的同学都知道,Redis目前版本是没有提供集群功能的,只能单打独斗.如果要实现多台Redis同时提供服务只能通过客户端自身去实现.目前根据文档已经看到Redis正在开发集群功能, ...

  8. 水晶易表 Xcelsius 2008 安装指南 完美支持office2010(亲手体验)

    Xcelsius2008水晶易表是一款很好用的软件.网上已经有破解方法,大家能够尝试一下这款经典软件了. 可是网上对于安装破解过程介绍的不详细或者纷乱,今天我汇总了全部的方法最终成功的安装上了,而且支 ...

  9. java的提取与替换操作

    public class Demo02 { public static void main(String args[]){ String str = "java 技术学习班  2007032 ...

  10. Thread Dump 和Java应用诊断(转)

    Thread Dump 和Java应用诊断 Thread Dump是非常有用的诊断Java应用问题的工具,每一个Java虚拟机都有及时生成显示所有线程在某一点状态的thread-dump的能力.虽然各 ...