http://acm.hdu.edu.cn/showproblem.php?pid=1358

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 AK , 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
 
Recommend
JGShining
 
题意:一个字符串,问输出所有:长度为多少(小于等于总长),能够由最少2个相同的子字符串组成
 注意:单纯是求最长前缀与后缀的题目,最好不要进行next数组优化(匹配题可以使用)

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
char a[]; void getnext(char *a , int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[j] == a[k])
{
k++;
j++;
next[j] = k ;
}
else
{
k = next[k];
}
}
} int main()
{
int n , ans = ;
while(~scanf("%d" , &n) && n)
{
int next[];
scanf("%s" , a );
printf("Test case #%d\n" , ++ans); memset(next , , sizeof(next));
getnext(a , n, next);//求next数组
for(int i = ; i <= n ; i++)//遍历每个大于2的字符串
{
//i % i (i - next[i])表示该字符串都由若干个(大于1个)相同的字符串组成
if(next[i] >= && i % (i - next[i]) == )
{
int l = i - next[i] ;
printf("%d %d\n" ,i , i / l); // i / l 求由多少个相同的个相同字符串组成
}
}
printf("\n");
} return ;
}

kmp(最长前缀与后缀)的更多相关文章

  1. kmp(所有长度的前缀与后缀)

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  2. POJ 2752 Seek the Name, Seek the Fame (KMP的next函数,求前缀和后缀的匹配长度)

    给一个字符串S,求出所有前缀,使得这个前缀也正好是S的后缀.升序输出所有情况前缀的长度.KMP中的next[i]的意义就是:前面长度为i的子串的前缀和后缀的最大匹配长度.明白了next[i],那么这道 ...

  3. POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  4. Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)

    #include<bits/stdc++.h>using namespace std;const int N=1000007;char s1[N],s2[N];int len1,len2; ...

  5. 【Todo】字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树

    另开一文分析字符串相关的各种算法,以及用到的各种数据结构,包括前缀树后缀树等各种树. 先来一个汇总, 算法: 本文中提到的字符串匹配算法有:KMP, BM, Horspool, Sunday, BF, ...

  6. 139. 回文子串的最大长度(回文树/二分,前缀,后缀和,Hash)

    题目链接 : https://www.acwing.com/problem/content/141/ #include <bits/stdc++.h> using namespace st ...

  7. poj 2774 最长公共子串 后缀数组

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 25752   Accepted: 10 ...

  8. AcWing:139. 回文子串的最大长度(字符串Hash + 前缀和 + 后缀和 + 二分)

    如果一个字符串正着读和倒着读是一样的,则称它是回文的. 给定一个长度为N的字符串S,求他的最长回文子串的长度是多少. 输入格式 输入将包含最多30个测试用例,每个测试用例占一行,以最多1000000个 ...

  9. C# 输出一个字符串的前缀、后缀和它的子串(信息内容安全 实验一)

    一.什么是前后缀 字符串的前缀:符号串左部的任意子串(或者说是字符串的任意首部) 字符串的后缀:符号串右部的任意子串(或者说是字符串的任意尾部) 举例:比如 101110 它的前缀就是空串.1.10. ...

随机推荐

  1. Python之路-Python中文件和异常

    一.文件的操作 open函数 在python中,使用open函数,打开一个已经存在的文件,或者新建一个新文件. 函数语法 open(name[, mode[, buffering[,encoding] ...

  2. File基本操作

    File (1)File是文件和目录路径名的抽象表示.IO流操作中大部分都是对文件的操作,因此Java就提供了File类供我们来操作文件. (2)构造方法 根据一个路径得到一个File对象      ...

  3. 如何查看Codeforces的GYM中比赛的数据

    前置条件:黄名(rating >= 2100) 或者 紫名(rating >= 1900)并且打过30场计分的比赛. 开启:首先打开GYM的界面,如果符合要求会在右边展示出一个Coach ...

  4. [POI2007]POW-The Flood(并查集)

    [POI2007]POW-The Flood Description AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了 ...

  5. File类常用方法和枚举

    新建一个file对象: File f = new File("F:\\01.JAVA基础300集\\05_常用类\\122.File类的使用.mp4"); (文件路径也可以用&qu ...

  6. js中关键字 const , let , var 的用法区别

    1.const定义的变量不可以修改,而且必须初始化. 2.var定义的变量可以修改,如果不初始化会输出undefined,不会报错. 3.let是块级作用域,函数内部使用let定义后,对函数外部无影响 ...

  7. Mac终端的Cocoapods创建自己的私有库和公有库

    一,前言 为什么要用Cocopods 通常在开发的过程中,大多时候,我们会处理一类相同的操作,比如对于字符串String的邮箱验证,是否为空,手机号验证,或者一些UIView的动画操作,我们为了避免写 ...

  8. Android逆向之旅---基于对so中的section加密技术实现so加固

    一.前言 好长时间没有更新文章了,主要还是工作上的事,连续加班一个月,没有时间研究了,只有周末有时间,来看一下,不过我还是延续之前的文章,继续我们的逆向之旅,今天我们要来看一下如何通过对so加密,在介 ...

  9. 银联高校极客挑战赛 初赛 第一场 B

    自学图论的码队弟弟 试图写非递归求解,然后TLE了一下午==,全程找不到bug,换成递归,一发AC 判断环写得很丑== #include<bits/stdc++.h> using name ...

  10. [CSP-S模拟测试]:Simple(数学)

    题目描述 对于给定正整数$n,m$,我们称正整数$c$为好的,当且仅当存在非负整数$x,y$,使得$n\times x+m\times y=c$. 现在给出多组数据,对于每组数据,给定$n,m,q$, ...