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. ...
随机推荐
- Oracle的分析函数
Oracle的分析函数row_number(),rank(),dense_rank()的用法与区别 比如查询工资排名第7的员工信息,可以用分析函数来做. --查询工资排名第7的员工信息select * ...
- Window系统Oracle 安装
一:安装Oracle 数据库软件 1.先去官网下载所需文件:http://www.oracle.com/technetwork/database/enterprise-edition/download ...
- oracle系列(三)表操作基础
支持的数据类型: 字符型char 定长 最大2000varchar2() 变长 最大4000clob 字符型大对象 最大4G 数字型number范围 -10的38次方到10的+38次方;number( ...
- kafka topic制定规则
kafka topic的制定,我们要考虑的问题有很多,比如生产环境中用几备份.partition数目多少合适.用几台机器支撑数据量,这些方面如何去考量?笔者根据实际的维护经验,写一些思考,希望大家指正 ...
- python中的extend和append
list.append(object) 向列表中添加一个对象object list.extend(sequence) 把一个序列seq的内容添加到列表中 old = ['a', 'b'] new = ...
- Angular4 JSONP + JAVA代码
一.在app.module.ts模块中,注入JsonpModule模块 import {JsonpModule} from "@angular/http"; @NgModule({ ...
- CASE WHEN 批量更新
单个值: UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHE ...
- SVN错误记录
1.SVN错误:Attempted to lock an already-locked dir 发生这个错误多是中断提交导致了,执行clear后可修复 右键项目--->team--->清理 ...
- STM32(10)——窗口看门狗
简介: 窗口看门狗(WWDG)通常被用来监测由外部干扰或不可预见的逻辑条件造成的应用程序背离正常的运行序列而产生的软件故障.除非递减计数器的值在 T6 位 (WWDG->CR 的第六位)变成 0 ...
- python学习——函数进阶
首先来看下面这个函数. def func(x,y): bigger = x if x > y else y return bigger ret = func(10,20) print(ret) ...