Period :KMP
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的更多相关文章
- 【未完】训练赛20190304:KMP+树状数组+线段树+优先队列
头炸了啊,只做出L题,前两天刚看的Shawn zhou的博客学习的,幸亏看了啊,否则就爆零了,发现题目都是经典题,线段树,KMP,我都没看过,最近又在复习考研,真后悔大一大二没好好学习啊,得抽时间好好 ...
- 算法:KMP算法
算法:KMP排序 算法分析 KMP算法是一种快速的模式匹配算法.KMP是三位大师:D.E.Knuth.J.H.Morris和V.R.Pratt同时发现的,所以取首字母组成KMP. 少部分图片来自孤~影 ...
- SDUT 3311 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...
- SDUT 2772 数据结构实验之串一:KMP简单应用
数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...
- 字符串匹配:KMP算法, Boyer-Moore算法理解与总结
1. KMP算法是前缀匹配算法,一次从前往后匹配的过程中,根据已经部分匹配的信息,在文本中,移动尽可能远的距离.而不是按照朴素模式匹配方法,每次都只移动一个位置. 比如这个示例,在文本串中从4(从0开 ...
- SDUT OJ 数据结构实验之串三:KMP应用
数据结构实验之串三:KMP应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之串一:KMP简单应用 && 浅谈对看毛片算法的理解
数据结构实验之串一:KMP简单应用 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- 数据结构20:KMP算法(快速模式匹配算法)详解
通过上一节的介绍,学习了串的普通模式匹配算法,大体思路是:模式串从主串的第一个字符开始匹配,每匹配失败,主串中记录匹配进度的指针 i 都要进行 i-j+1 的回退操作(这个过程称为“指针回溯”),同时 ...
- hdu 1358 Period(kmp求一个串的重复子串)
题意:统计单串中从某个位置以前有多少重复的串 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string. ...
随机推荐
- 小程序内嵌H5——判断小程序环境的坑
现在各种小程序风靡,这边H5的需求还没有搞定,产品又要求做小程序版本,做可以,关键是618前上线,我-- whatever,618要做推广,日期订了,剩下的就只能是排期,定方案,尽可能完成. 最后和产 ...
- 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
转 关于ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法 2016年03月31日 10:14:59 阅读数:1 ...
- python中 的继承
1.Python的类可以继承多个类,Java和C#中则只能继承一个类. 2.Python的类如果继承了多个类,那么其寻找方法的方式有两种,分别是:深度优先和广度优先. 当类是经典类时,多继承情况下,会 ...
- LAMP+Varnish的实现
基于Keepalived+Varnish+Nginx实现的高可用LAMP架构 注意:各节点的时间需要同步(ntpdate ntp1.aliyun.com),关闭firewalld(systemctl ...
- IO,File对象-构造函数和常用方法
import java.io.File; import java.text.DateFormat; import java.util.Date; public class FileDemo { pub ...
- php_Trait
* Trait Answer* Trait : 关键字 Trait* 使用 use* 我的理解 是为了解决php不能多继承的一个处理方式* 在使用的时候 可以让两个不相关的类 产生联系* Trait ...
- ES5拓展
一.JSON拓展 1.JSON.parse(str,fun):将JSON字符串转为js对象 两个参数:str表示要处理的字符串:fun处理函数,函数有两个参数,属性名.属性值 // 定义json字符串 ...
- laravel-admin 创建数据库并生成控制器
以user表为例 1. 生成迁移:php artisan make:migration create_users_table 在 database/migration 中生成迁移文件,可对迁移文件进行 ...
- TImage保存图片到Stream及从Stream中取图片
因为一个项目,不得不将图片保存到数据库中,需要的时候再从数据库中读取.初时,以为很简单,不就是一个Stream.事实上,也很简单.度娘一下,代码也很多,但,都是坑! 看一下TImage的源,Pictu ...
- Windows 聚焦(锁屏背景)不更新的解决方法
在 Windows Store 搜索 Dynamic theme 安装后可对桌面背景.锁屏界面等进行设置,非常好用!