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. ...
随机推荐
- HTML中的<meta>标签的使用
HTML中<meta>标签的使用 在我们制作的网页中,要是想让它能够让更多的人去访问,最好的方法就是通过搜索引擎来找到你的网址,于是需要你的网页可以有关键词能够让搜索引擎来识别,于是HTM ...
- 摩尔吧 FPGA培训
摩尔吧 FPGA培训 2017.7.30 第一天与非网摩尔吧创始人苏公雨给我们介绍了FPGA的发展历史,以及目前FPGA厂家的市场定位. 2017.7.30~2017.8.4 这个星期主要是学习画电 ...
- redux-saga框架使用详解及Demo教程
redux-saga框架使用详解及Demo教程 前面我们讲解过redux框架和dva框架的基本使用,因为dva框架中effects模块设计到了redux-saga中的知识点,可能有的同学们会用dva框 ...
- ABAP术语-SAP GUI for HTML
SAP GUI for HTML 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/14/1104996.html An ITS impleme ...
- IOS移动端(H5)alert/confirm提示信息去除url
前几天写移动端项目用alert和confirm进行信息提示,但发现在iOS系统中,每次提示信息上面都会被添加一行URL地址,安卓却没有,经过查找之后,果然不出所料,兼容!!兼容!!!兼容!!! 需要重 ...
- Java性能优化的50个细节
在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时 ...
- Spring : JDBC模板, 事务和测试
JDBCTemplate简单配置:-------------------------------jdbc.properties配置----------------------------------- ...
- 使用docker搭建laravel记叙
第一步,先从dockerhub上pull一个docker镜 docker pull laraedit/laraedit 这个docker镜像已经安装了 nginx.laravel和mysql,所以不需 ...
- DQL数据查询
set hive.fetch.task.conversion=more; -- 避免触发MR job select distinct name from employee_id limit 2; -- ...
- socketpair通信
1.线程间通信(参考安卓源码InputTransport.cpp) #include <pthread.h> #include <sys/types.h> /* See NOT ...