一个带有 n 个字符的字符串 s ,要求找出 s 的前缀中具有循环结构的字符子串,也就是要输出具有循环结构的前缀的最后一个数下标与其对应最大循环次数。(次数要求至少为2)
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.

InputThe 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.
OutputFor 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

思路: 因为next记录的是原字符串当前前缀的后缀的最大匹配长度,反过来就是当前前缀的前缀匹配长度(即可能存在的循环节长度)。

          故利用好next数组能很快的解决这道题

第一步:用kmp算法,求出所有前缀(满足是两个或两个以上的循环节组成的字符串)

    并用next数组保存的是当前字符串中能够匹配的最长前后缀的长度。

i-next[i]就是循环节长度.

第二步:当next数组满足i%(i-next[i])==0。且next[i]!=0。说明当前字符串中前缀为周期串,且循环次数为i/(i-next[i]);

字符串长度      1   2   3
字符串               a   a   a
next数组         0   1   2

l=i-(next[i])     1   1   1       循环节长度

i%l            0        0         0      判断是否能构成循环

i/l 循环次数      不存在    2   3

我们以 aabaabaabaab 为例。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int Next[1100000] ;
char str[1100000] ;
void getnext(int len)
{
int i=0,j=-1;
Next[0]=-1;
while(i<len)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
Next[i]=j;
if(Next[i]&&i%(i-Next[i])==0)
printf("%d %d\n",i,i/(i-Next[i]));
}
else
j=Next[j];
}
} int main()
{
int n,Case=1;
while(scanf("%d",&n)&&n)
{
scanf("%s",str);
next[0]=next[1]=0;
printf("Test case #%d\n",Case++);
getnext(n);
printf("\n");
}
return 0;
}

E - Period(KMP中next数组的运用)的更多相关文章

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

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

  2. KMP中next数组的理解

    next数组是KMP的核心,但对于next数组我们总是有时候感觉明白了,但有时候又感觉没明白,现在我就说下我自己对KMP中next数组的理解,首先next[i]上的数字的意义,next[i]表示的是当 ...

  3. POJ 2752 KMP中next数组的理解

    感觉这里讲的挺好的.http://cavenkaka.iteye.com/blog/1569062 就是不断递归next数组.长度不断减小. 题意:给你一个串,如果这个串存在一个长度为n的前缀串,和长 ...

  4. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  5. KMP中next数组的理解与应用

    理解 1.next数组一直往前走 next数组一直往前走,得到的所有前缀也是当前主串的后缀,当然了,也是当前主串的前缀. 2.周期性字符串 1.周期性字符串$\Leftrightarrow n \,\ ...

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

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

  7. HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. POJ1961(kmp中Next数组的性质)

    对于某个位置i,i - Next[i]是循环节长度,i整除(i - Next[i])时是完整的几个循环元. ; int n, kase, Next[maxn]; char ch[maxn]; inli ...

  9. BZOJ 1355 KMP中next数组的应用

    思路: 我们知道 next[i]是失配的i下一步要去哪儿 next[n]就是失配的n要去哪儿 n-next[n]就是答案(即最短周期)啦 //By SiriusRen #include <cst ...

随机推荐

  1. Go语言快速安装手册

    Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Griesemer, Rob Pike, Ken Thompson主持开发,后来还加入了Ia ...

  2. Python使用urllib,urllib3,requests库+beautifulsoup爬取网页

    Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...

  3. 【高精度】计算2的N次方

    题目相关 [题目描述] 任意给定一个正整数N(N≤100),计算2的n次方的值. [输入] 输入一个正整数N. [输出] 输出2的N次方的值. [输入样例] 5 [输出样例] 32 分析 本题考察的是 ...

  4. dbms_job和dbms_job基础学习

    一.dbms_job学习 a.创建job: dbms_job.submit(jobno,what,next_date,interval);b.删除job: dbms_job.remove(jobno) ...

  5. RedHat6.1通过配置yum server安装软件包

    1.获取镜像RHEL_6.1\ x86_64\ Disc\ 1 2.配置yum server #cd /etc/yum.repos.d #tar -cvf 20141114bak.tar *.repo ...

  6. 【JavaWeb】EL 表达式

    EL 表达式 简介 EL(Expression Language),即表达式语言. EL 表达式主要是代替 jsp 页面中 表达式脚本 在 jsp 页面中进行数据的输出,因为 EL 表达式在输出数据的 ...

  7. 【Flutter】容器类组件之Container容器

    前言 Container是一个组合类容器,它本身不对应具体的RenderObject,它是DecoratedBox.ConstrainedBox.Transform.Padding.Align等组件组 ...

  8. LR参数

    一.LR函数 : lr_start_transaction:   为性能分析标记事务的开始 lr_end_transaction: 为性能分析标记事务的结束:事务名称与事务开始时保持一致 lr_ren ...

  9. Kafka 探险 - 生产者源码分析: 核心组件

    这个 Kafka 的专题,我会从系统整体架构,设计到代码落地.和大家一起杠源码,学技巧,涨知识.希望大家持续关注一起见证成长! 我相信:技术的道路,十年如一日!十年磨一剑! 往期文章 Kafka 探险 ...

  10. ctfhub技能树—sql注入—过滤空格

    手注 查询数据库 -1/**/union/**/select/**/database(),2 查询表名 -1/**/union/**/select/**/group_concat(table_name ...