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. 使用docxtemplater来处理word模板

    工作中遇到需要根据不同数据来处理模板word的情况,网上搜索了一番之后,找到了一个叫做docxtemplater的库,使用起来非常便携,也十分满足此次的需求. 这次就来记录一下docxtemplate ...

  2. 全面优化MySQL(一)

    mysql执行一条查询语句的内部执行过程 权限验证 客户端通过连接器连接到 MYSQL服务器. 查询缓存 查询是否有查询缓存, 如果有缓存(之前执行过此语句),则直接返回缓存数据. 语法检查 分析器会 ...

  3. Simple Live System Using Nginx

    1. Install nginx #Preinstalled directory install=/usr/local/nginx #Delete installed directory rm -rf ...

  4. Lamabda Where Select Find First等区别

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  5. Maven生成可以直接运行的jar包的多种方式(转)

    转自:https://blog.csdn.net/xiao__gui/article/details/47341385 Maven可以使用mvn package指令对项目进行打包,如果使用java - ...

  6. tac反向显示文件内容

    1.命令功能 tac是cat的反向拼写功能是反向显示文件内容.cat是从文件第一行开始读取文件输出,tac是从最后一行开始读取文件并进行反向输出. 2.语法格式 tac  [option]  [fil ...

  7. APKMirror - 直接下载google play里的应用

    APKMirror - Free APK Downloads - Download Free Android APKs #APKPLZ https://www.apkmirror.com/

  8. Linux wc -l 统计行数问题

    统计文件行数   通过wc -l 来实现. [root@xguang_02 xguang]# wc -l tt.dat     798 tt.dat    而通过nl\ cat -n 查看时发现为79 ...

  9. sonar-runner命令模式运行sonar

    适用环境:该种配置的模式适用于本地调试模式 前提条件:在工程路径下创建sonar-project.properties文件 该客户端的路径在系统配置文件中进行了定义 alias sonar-runne ...

  10. CodeChef Max-digit Tree(动态规划)

    传送门. 题解: 最主要的问题是如何判断一个数是否合法,这就需要发现性质了. 这个状态划分还是不太容易想到, 每次加的数\(∈[0,k)\),也就是个位一直在变变变,更高的位每次都是加一,这启发我们状 ...