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. 2.9 学习总结 之 【Android】体温统计APP

    一.说在前面 昨天 学习了JQ的相关知识 今天 编写体温统计APP 我的工程源码:https://github.com/xiaotian12-call/Take-body-temperature 二. ...

  2. POJ 1195:Mobile phones 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16893   Accepted: 7789 De ...

  3. java核心-JVM-gc面试题

    1.写一个memory leak的例子 public class MemonyLeak { //1.memoryLeak内存泄漏 /* 这类错误报错具体显示:java.lang.OutOfMemory ...

  4. nidlist 问题

    错误问题如下: 解决方案: Dao文件 boolean DeleteList(String nidList); 改为: boolean DeleteList(@Param("nidList& ...

  5. Python安装bs4

    - 需要将pip源设置为国内源,阿里源.豆瓣源.网易源等 - windows (1)打开文件资源管理器(文件夹地址栏中) (2)地址栏上面输入 %appdata% (3)在这里面新建一个文件夹 pip ...

  6. 苏州大学ICPC集训队新生赛第二场

    A - Score UVA - 1585 水 #include<bits/stdc++.h> using namespace std; int main(){ int n; cin> ...

  7. Java的优先队列PriorityQueue详解

    一.优先队列概述 优先队列PriorityQueue是Queue接口的实现,可以对其中元素进行排序, 可以放基本数据类型的包装类(如:Integer,Long等)或自定义的类 对于基本数据类型的包装器 ...

  8. javascript 解决provisional headers are shown的过程

    请求没有被发送,因为是载入缓存资源. 大概是说 完全相同的请求间隔数毫秒(太短),导致加载失败,查看了chrome控制台发现 Provisional headers are shown 出现在 载入缓 ...

  9. windows自带的颜色编辑器居中

    void xxx::SetOSDColor(CLabelUI * pLabel) { COLORREF color = RGB(*, *, *); CColorDialog cdlg(color, C ...

  10. X2安装配置keras环境(包含matplotlib安装)

    https://blog.csdn.net/jonado13/article/details/83933453 1.安装pipapt install python3-pipE: Could not o ...