I - Period

Problem Description

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 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

题意:输入n,输入n个字符,判断这个字符串中,任意前缀中是否有循环节,输出这个前缀长度和循环次数。(循环次数大于等于二的)

题解:利用KMP算法里的next【】数组,再适合不过了。

样例分析:3 aaa   aa:前两个字符,循环次数2,输出2 2

         aaa:前三个字符,循环次数3,输出3 3

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;
const int maxn = 1e6 + 10;
int nex[maxn]; void getnext(string pat, int lenpat) {
int i = 0;
int j = nex[0] = -1; //j相当于记录nex[i]的值
while(i<lenpat){ //求next[1]~next[len-1]
if (j == -1 || pat[i] == pat[j]) {
i++;
j++;
nex[i] = j;
}
else j = nex[j]; //j回退,直到j回退到-1或pat[i]==pat[j+1]
}
} int main() {
ios::sync_with_stdio(0);
int n, ans = 1;
string s;
while (cin >>n &&n) {
memset(nex, 0, sizeof(nex));
cin >> s;
cout << "Test case #" << ans << endl, ans++;
getnext(s, n);
for (int i =1; i <=n; i++) {
//cout << nex[i] << " ";
int t = i - nex[i];
if (i% t == 0 && i / t >1)
cout << i << " " << i / t << endl;
}
cout << endl;
}
return 0;
}

Period :KMP的更多相关文章

  1. 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列

    头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...

  2. 算法:KMP算法

    算法:KMP排序 算法分析 KMP算法是一种快速的模式匹配算法.KMP是三位大师:D.E.Knuth.J.H.Morris和V.R.Pratt同时发现的,所以取首字母组成KMP. 少部分图片来自孤~影 ...

  3. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  4. SDUT 2772 数据结构实验之串一:KMP简单应用

    数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...

  5. 字符串匹配:KMP算法, Boyer-Moore算法理解与总结

    1. KMP算法是前缀匹配算法,一次从前往后匹配的过程中,根据已经部分匹配的信息,在文本中,移动尽可能远的距离.而不是按照朴素模式匹配方法,每次都只移动一个位置. 比如这个示例,在文本串中从4(从0开 ...

  6. SDUT OJ 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...

  7. SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解

    数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...

  8. 数据结构20:KMP算法(快速模式匹配算法)详解

    通过上一节的介绍,学习了串的普通模式匹配算法,大体思路是:模式串从主串的第一个字符开始匹配,每匹配失败,主串中记录匹配进度的指针 i 都要进行 i-j+1 的回退操作(这个过程称为“指针回溯”),同时 ...

  9. hdu 1358 Period(kmp求一个串的重复子串)

    题意:统计单串中从某个位置以前有多少重复的串 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string. ...

随机推荐

  1. Flex 布局教程:语法和实例

    语法篇 网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便, ...

  2. window7及以上 创建软链接 mklink

    软链接是一种文件共享方式. 命令:mklink /d "C:\d" "C:\e" 有哪些坑: 1.此命名必须以管理员方式在cmd运行 2.文件必须不存在..通过 ...

  3. oracle 导入数据报600错误

    之前导入一个大容量dmp数据文件,报一个600错误,咨询网上的解决方法,按上面的处理一圈也没有整好,最后咨询组里一个大神,出现此错误 思路是,单个数据文件大小最大为32G,分析数据库后解决如下: 错误 ...

  4. iOS之NSDictionary初始化的坑

    最近在做项目的时候遇到一个挺坑的崩溃问题,是由于NSDictionary初始化时nil指针引起的崩溃.假设我们现在要初始化一个{key1 : value1, key2 : value2, key3 : ...

  5. 内网最小化安装CentOS后,想安装ISO文件中的包怎么办呢?

    昨日公司测试人员需要升级公司服务器Python,发现公司服务器上缺失了各种各样的包.比如open-ssl,python-deve等 1.查看你的Centos版本 lsb_release -a 2.上传 ...

  6. Cobbler实现自动化安装(下)--实现过程

    实验环境 [root@cobbler ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@cobbler ~] ...

  7. layer父界面调用子弹窗的方法和获取子弹窗的元素值总结

    layer.open({ type: 2 ,title: false //不显示标题栏 ,closeBtn: false ,area: ['460px', '45%'] ,shade: 0.5 ,id ...

  8. colspan和rowspan合并单元格

    最近在回顾html的时候,经常碰到一些table标签的问题,其中大多数都是合并单元格,所以在这里记录下自己的探究过程: <table cellpadding="0" cell ...

  9. 使用js实现单向绑定

    详细解释单向绑定 参考资料 MDN addEventListener()定义与用法 <!DOCTYPE html> <html lang="en"> < ...

  10. laravel-admin 创建数据库并生成控制器

    以user表为例 1. 生成迁移:php artisan make:migration create_users_table 在 database/migration 中生成迁移文件,可对迁移文件进行 ...