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. C++ mfc 简易文本编辑器 遇到的一些问题

    [题目40]简易文本编辑器. 设计一个简易的文本编辑器. 设计要求: (1) 具有图形菜单界面: (2) 查找,替换(等长,不等长),插入(插串,文本块的插入).文本块移动(行块,列块移动),删除; ...

  2. ArcoLinux美化教程

    ArcoLinux美化教程 1. 前言 ArcoLinux已经足够美观,这里主要是讲解如何配置桌面特效 2. 安装compiz $ yay -S compiz 3. 用compiz替换xfwm4 编辑 ...

  3. http请求的过程及潜在的性能优化点

    web前端的看富于部署过程 开发者将开发的代码发布到远程的服务器(webserver/cdn),用户通过访问浏览器输入相应的网址,浏览器向远程服务器发送请求,动态的增量式的加载资源 web前端就是一个 ...

  4. Windows平台整合SpringBoot+KAFKA_第1部分_环境配置部分

    项目需要,需要整合 SpringBoot+KAFKA 我调查了一下,发现Linux中,要先装zoomkeeper,再装KAFKA,如  https://blog.csdn.net/zhangcongy ...

  5. 032-session函数

    <?php $username="guest1"; if(isset($username)) { session_name($username); } echo " ...

  6. 095-PHP遍历关联数组,并修改数组元素值

    <?php $arr=array('I'=>1,'II'=>2,'III'=>3,'IV'=>4,'V'=>5); //定义一个数组 echo '修改之前数组信息: ...

  7. 068-PHP定义并输出数组

    <?php $arr=array(98,'hello',67,'A',85,NULL); //定义一个数组 echo "输出第一个元素:{$arr[0]}"; //输出数组的 ...

  8. Spring配置数据源的三种方法

    前言:今天接触新项目发现用的是JNDI配置数据源,用度娘倒腾了一会也没弄好,只好用平常用的方法,结果发现BasicDataSource和DriverManagerDataSource也是不同的,所以记 ...

  9. 第二阶段scrum-4

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 前端制作完成,数据库在制作中.

  10. 新手学Java,有哪些入门知识点?

    很多小伙伴们在刚接触Java的时候,会有些迷茫,不知道该从哪里入手,不管是做前端还是后端,程序员都会用到JAVA,那该掌握哪些必要的基础知识呢.今天就跟大家分享新手学Java,有哪些入门知识点? 下面 ...