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. scrapy从安装到爬取煎蛋网图片

    下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/pip install wheelpip install lxmlpip install pyopens ...

  2. Reading Notes : 180213 计算机的硬件构成与处理流程

    读书<计算机组成原理>,<鸟哥的Linux私房菜基础篇> 基本上接触过计算机的人,都多少知道计算机的具体构成,但是真正能讲明白的却说了很多,本节将讲解一下计算机的基本硬件构成和 ...

  3. 删除文件的第一列 -Linux

    删除文件 text中第一列 方式一 awk '{$1="";print $0}' text 方式二 sed -e 's/[^ ]* //' text

  4. 常用的JavaScript设计模式(一)Constructor(构造器)模式

    在es6中,新增了一个语法糖--class,可以说是为JavaScript引入了类的概念.而在传统的JavaScript中,则是通过构造器生成实例对象的. JavaScript支持特殊的constru ...

  5. 企业IT架构转型之道 读后感

    放假三天,用部分时间阅读了企业IT架构转型之道这本书.第一遍潦草读完,就感觉收益颇多.这本书值得多读几遍,适合精度. 作为银行IT开发人员,在央企IT成本部门的大背景下,开发过程中遇到的诸多疑惑.困惑 ...

  6. mysql 导出行数据到txt文件,指定字符分割

    select id,name, concat('tel:',phone) from user order by time INTO outfile 'user.txt' FIELDS terminat ...

  7. 用matplotlib库画图

    1.用例一 import matplotlib.pyplot as plt import numpy as np x=np.linspace(0,10,100) y=np.cos(2*np.pi*x) ...

  8. Scala学习笔记(四)—— 数组

    定长数组Array 定义定长数组用Array,有如下几种方法: 初始化一个长度为8的定长数组,其所有元素默认值均为0 scala> new Array[Int](8) res0: Array[I ...

  9. Dijkstra算法堆优化(vector建图)

    #include<iostream> #include<algorithm> #include<string.h> #include<stdio.h> ...

  10. react路由按需加载方法

    使用router4之后以前的按需加载方法require.ensure 是不好使了. 所以我们改用react-loadable插件做按需加载. 第一步: yarn add react-loadable ...