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 A K,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
      地址:  hdu1358  http://acm.hdu.edu.cn/showproblem.php?pid=1358
    求最小循环节,比如样例2:    aabaabaabaab
                    6 2 表示第6个为止,有两个循环节。不要有重叠。
    再举例:    

              9
              aabaabaab
              Test case #1
           next: -1 0 1 0 1 2 3 4 5 6
            i:  0 1 2 3 4 5 6 7 8 9

          根据next数组,我们可以做:int    k =  i-next[i];  k即为到第i的段的最小循环节。比如在i=7的位置,其next值为4。为什么是4?因为:

        

        此有重叠部分。然后:

        

        如果a与b相同,那么1处与2处必定相同。那么  i - next[i]变为1处长度,那么既然1==2,此就为**最小循环节**。如果像上两图中,俩循环节有重叠部分,明显不符合题意。因为 总长度%k!=0,会把3处余出来(这里是3!=2的情况,如果1==2==3的话,那就不是重叠的情况了。)。

        像这种的:aaa  aaa .在i=6的时候,next=5,  6 - 5==1,   6%1==0,6 /1 =6 ,便为6个循环节。

        又比如:aab aab aab  在i=9的时候,next=6,  9-6==3,  9%3==0,9/3=3,便为3个循环节。

        next==-1||==0的不符合题意,判断一下就好了。主要是对next数组的理解!!

      

#include<iostream>
#include<cstring>
#include<map>
#include<set>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e6+;
char a[maxn];
int next[maxn];
void pr(int n)
{
next[]=-;
int k=-;
int j=;
while(j<n)
{
if(k==-||a[j]==a[k])
{
++k;
++j;
next[j]=k;
}
else
k=next[k];
}
}
int main()
{
int n;
int ac=;
while(cin>>n)
{
if(n==)
break;
cin>>a;
pr(n);
printf("Test case #%d\n",ac++);
// for(int i=0;i<=n;i++)
// cout<<next[i]<<" ";
for(int i=;i<=n;i++)
{
int k=i-next[i];
if(next[i]==-||next[i]==)
continue;
if(i%k==)
{
printf("%d %d\n",i,i/k);
}
}
cout<<endl;
}
}

A - Period(kmp的next数组的应用)的更多相关文章

  1. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

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

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

  3. 求最长公共前缀和后缀—基于KMP的next数组

    KMP算法最主要的就是计算next[]算法,但是我们知道next[]求的是当前字符串之前的子字符串的最大前后缀数,但是有的时候我们需要比较字符串中前后缀最大数,比如 LeetCode的shortest ...

  4. hdu1358 Period KMP

    给出一个字符串,找出所有可以作为它循环节的子串长度 利用kmp的失配数组的性质,可以直接做 #include<stdio.h> #include<string.h> ; cha ...

  5. 【bzoj2384】[Ceoi2011]Match 特殊匹配条件的KMP+树状数组

    题目描述 给出两个长度分别为n.m的序列A.B,求出B的所有长度为n的连续子序列(子串),满足:序列中第i小的数在序列的Ai位置. 输入 第一行包含两个整数n, m (2≤n≤m≤1000000).  ...

  6. SP263 PERIOD - Period KMP技巧

    \(\color{#0066ff}{题目描述}\) 如果一个字符串S是由一个字符串T重复K次形成的,则称T是S的循环元.使K最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现给一个给定长 ...

  7. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  8. POJ 2752 KMP中next数组的应用

    题意: 让你从小到大输出给的字符串中既是前缀又是后缀的子串的长度. 思路: 先要了解这个东西: KMP中next数组表示的含义:记录着字符串匹配过程中失配情况下可以向前多跳几个字符,它描述的也是子串的 ...

  9. KMP(next数组的更新理解)Codeforces Round #578 (Div. 2)--Compress Words

    题目链接:https://codeforc.es/contest/1200/problem/E 题意: 有n串字符串,让你连起来:sample please ease in out   ---> ...

  10. UVA 11475 Extend to Palindrome (kmp || manacher || 后缀数组)

    题目链接:点击打开链接 题意:给你一个串,让你在串后面添加尽可能少的字符使得这个串变成回文串. 思路:这题可以kmp,manacher,后缀数组三种方法都可以做,kmp和manacher效率较高,时间 ...

随机推荐

  1. JAVA Random 详解

    Java中存在着两种Random函数: 一.java.lang.Math.Random; 调用这个Math.Random()函数能够返回带正号的double值,该值大于等于0.0且小于1.0,即取值范 ...

  2. scanf与正则表达式的搭配及应用

    scanf与正则表达式的搭配及应用 正则其实我也学的不咋地,只会一点皮毛,正则最大的作用就是当输入流是一个字符串,我们能在输入的时候就滤掉无用信息,省去后期提取数值的步骤. 正则的语法我怕误人子弟,嘿 ...

  3. P1056 组合数的和

    P1056 组合数的和 转跳点:

  4. ExcelPackage导入导出,命名空间一定要是EPPlus

    1.引入EPPlus.dll,旧版的是OfficeOpenXml.dll,最好使用EPPlus2.调用 string path = UploadExecl(batchUpload.BinaryExce ...

  5. css实现三角箭头(兼容IE6)

    纯css实现三角箭头有几种方式,常规的方式用css3的rotate把元素旋转45度角,无法兼容ie的主要原因是ie不支持边框透明, 第二种方法,使用chroma滤镜透明,经尝试在ie下会出现activ ...

  6. 导出execl

    string filepath = Utils.GetMapPath("/upload/excel/"); filepath = filepath + fileName + &qu ...

  7. Aspen安装过程报错总结

    前几天一直帮朋友安装Aspen v11,因为之前的老版本总是报错,报错内容大概是证书过期了, 一开始朋友电脑上的老版本的Aspen 8卸载了,删除之前的数据库SqlServer 2012 ,然后重新安 ...

  8. junit基础学习之-简介(1)

    JUnit介绍 JUnit是一个开源的Java单元测试框架,由 Erich Gamma 和 Kent Beck 开发完成. 1  JUnit简介 JUnit主要用来帮助开发人员进行Java的单元测试, ...

  9. C# Stream篇(七) -- NetworkStream

    NetworkStream 目录: NetworkStream的作用 简单介绍下TCP/IP 协议和相关层次 简单说明下 TCP和UDP的区别 简单介绍下套接字(Socket)的概念 简单介绍下Tcp ...

  10. Inheritance and the prototype chain 继承和 原型 链

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain Inherita ...