LA_3026_Period_(kmp)
描述
给出一个长度为n的字符串,求它的每个前缀(前缀长度>=2)是否是循环的(循环次数>1),如果是,求出循环周期.
分析
我们先来看对于一个满足条件的循环的串.设其长度为l,一个循环节的长度为s,那么很明显,[1,l-s]与[s+1,l]这一对前后缀是相同的,并且l是s的整数倍.
那么反过来呢?如果一个串的前后缀[1,l-s]与[s+1,l]相同,那么该串从第一位开始,每一位都和其右移s位的相同,如果l是s的整数倍,那么(l-s)就是s的整数倍,那么前缀可以分成整数个长度为s的字串,每一个字串都是右移s后重合的,那么这就是一个循环节为s的循环串.
这样的话这两种定义就是等价的,所以我们直接用kmp求前后缀重叠就行了.
#include <bits/stdc++.h>
using namespace std; const int maxn=+;
int n,kase;
char p[maxn];
int f[maxn];
void get_fail(){
f[]=; f[]=;
for(int i=;i<n;i++){
int j=f[i];
while(j&&p[i]!=p[j]) j=f[j];
f[i+]=p[i]==p[j]?j+:;
}
}
int main(){
while(scanf("%d",&n)&&n){
scanf("%s",p);
get_fail();
printf("Test case #%d\n",++kase);
for(int i=;i<=n;i++)
if(f[i]>&&i%(i-f[i])==) printf("%d %d\n",i,i/(i-f[i]));
printf("\n");
}
return ;
}
3026
Period
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 ≤ 1000000) 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
LA_3026_Period_(kmp)的更多相关文章
- KMP算法求解
// KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...
- 简单有效的kmp算法
以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...
- KMP算法
KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [KMP]【学习笔记】
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36916 Accepted: 14904 Descript ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
- KMP专题
1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...
- KMP学习之旅
说起kmp就要从字符串的匹配说起,下面我们谈谈字符串的匹配 给定一个原字符串:bababababababababb,再给定一个模式串:bababb,求模式串是否在源字符串中出现 最简单的方法就是遍历源 ...
- KMP模板
参考:http://www.cnblogs.com/c-cloud/p/3224788.html #include<stdio.h> #include<string.h> vo ...
随机推荐
- 性能更好的js动画实现方式——requestAnimationFrame
本文转载,原文地址:http://www.cnblogs.com/2050/p/3871517.html 用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css ...
- [技术翻译]构建现代化的 Objective-C (上)
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3561514.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- OpenJudge/Poj 1321 棋盘问题
1.链接地址: http://bailian.openjudge.cn/practice/1321 http://poj.org/problem?id=1321 2.题目: 棋盘问题 Time Lim ...
- Struts2文件上传功能浅析
本文将以图片上传为例,解析Struts2文件上传的主要过程实例的功能:1.在jsp页面选择要上传的图片, 2.为待上传的图片取名,以便于查找 ...
- 【nodemailer】之 work with mustache
之前对nodemailer做了简要的研究,基本上是搞定了发邮件的问题.但很多情况下邮件的内容不是固定的,然后又需要有一个合适的样式,这就需要使用模板了.nodemailer有nodemailer-ma ...
- 银行卡号Luhm校验Java实现代码
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CheckBankNumber { //Des ...
- 升级iOS10后SearchController焦点无法获取的问题
原来在没升级之前,是这样获取的,好使 - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self.sea ...
- python制作安装包(setup.py)
1.制作setup.py from distutils.core import setup setup(name='Myblog', version='1.0', description='My Bl ...
- Shell面试题
1.用Shell编程,判断一文件是不是块或字符设备文件,如果是将其拷贝到 /dev 目录下. #!/bin/bash#1.sh#判断一文件是不是字符或块设备文件,如果是将其拷贝到 /dev 目录下#f ...
- JLink软件升级到4.92之后,Jlink不能用了
JLink软件升级到4.92之后,Jlink不能用了 情景描述: Jlink软件升级到4.9 ...