首页 技术 正文
技术 2022年11月10日
0 收藏 951 点赞 4,492 浏览 1677 个字

描述


https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1027

给出一个长度为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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774