首页 技术 正文
技术 2022年11月23日
0 收藏 624 点赞 4,281 浏览 2919 个字

The repetition number of a string is defined as the maximum number \(R\) such that the string can be partitioned into \(R\) same consecutive substrings. For example, the repetition number of "ababab" is 3 and "ababa" is 1.

Given a string containing lowercase letters, you are to find a substring of it with maximum repetition number.

Input

The input consists of multiple test cases. Each test case contains exactly one line, which

gives a non-empty string consisting of lowercase letters. The length of the string will not be greater than 100,000.

The last test case is followed by a line containing a ‘#’.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by the substring of maximum repetition number. If there are multiple substrings of maximum repetition number, print the lexicographically smallest one.

Sample Input

ccabababc
daabbccaa
#

Sample Output

Case 1: ababab
Case 2: aa

题意:

找出原字符串中的重复次数最多的连续重复子串。

题解:

记这个连续重复子串为\(L\),我们可以发现,这个字符串一定会覆盖\(s[0],s[L],s[L*2]\)…..这些点中相邻的两个(因为长度至少为\(2L\)嘛)。假设它覆盖的是\(s[L*i]\)和\(s[L*(i+1)]\),那么我们就往前和往后计算能匹配多远(往后匹配用到了后缀数组的height数组,往前匹配可以while到\(s[L*(i-1)]\),越过\(s[L*(i-1)]\)的情况和前面计算的重复了,可以不算)

记往前匹配和往后匹配的最长长度为k,则重复次数为\(k/L+1\)。

再求lcp的时候用st表优化一下即可

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000010;
char s[N];
int n;
int fir[N],sec[N],rnk[N],t[N],sa[N],b[N];
double log2(double x){
return log(x)/log(2.0);
}
void msort(){
memset(t,0,sizeof t);
for(int i=1;i<=n;++i)t[sec[i]]++;
for(int i=1;i<N;++i)t[i]+=t[i-1];
for(int i=n;i;--i)b[t[sec[i]]--]=i;
memset(t,0,sizeof t);
for(int i=1;i<=n;++i)t[fir[b[i]]]++;
for(int i=1;i<N;++i)t[i]+=t[i-1];
for(int i=n;i;--i)sa[t[fir[b[i]]]--]=b[i];
}
int height[N];
void get_height(char *s){
int k=0;
for(int i=1;i<=n;++i){
if(rnk[i]==1){
height[i]=0;
continue;
}
if(k)--k;
int j=sa[rnk[i]-1];
while(i+k<=n&&j+k<=n&&s[i+k]==s[j+k])k++;
height[i]=k;
}
}
int mn[N][20];
void get_height_st(){
for(int i=1;i<=n;++i)mn[i][0]=height[sa[i]];
int t=log2(n);
for(int i=1;i<=t;++i){
for(int j=1;j<=n;++j){
if(j+(1<<(i-1))>n)mn[j][i]=mn[j][i-1];
else mn[j][i]=min(mn[j][i-1],mn[j+(1<<(i-1))][i-1]);
}
}
}
int height_query(int l,int r){
l=rnk[l],r=rnk[r];
if(l>r)swap(l,r);l++;
int t=log2(r-l+1);
return min(mn[l][t],mn[r-(1<<t)+1][t]);
}
void get_sa(char *s){
for(int i=1;i<=n;++i)rnk[i]=s[i];
for(int k=1;k<=n;k*=2){
for(int i=1;i<=n;++i){
fir[i]=rnk[i];
if(i+k>n)sec[i]=0;
else sec[i]=rnk[i+k];
}
msort();
int num=1;rnk[sa[1]]=1;
for(int i=2;i<=n;++i){
if(fir[sa[i]]!=fir[sa[i-1]]||sec[sa[i]]!=sec[sa[i-1]])num++;
rnk[sa[i]]=num;
}
if(num==n)break;
}
}
int maxn,pos,len;
void find(){
maxn=1;
for(int i=1;i<=n/2;++i){
for(int j=1;j+i<=n;j+=i){
if(s[j]!=s[j+i])continue;
int k=height_query(j,j+i),now,r;
now=k/i+1,r=i-k%i;
int cnt=0,p=j;
for(int m=j-1;m>j-i&&s[m]==s[m+i]&&m;--m){
cnt++;
if(cnt==r)now++,p=m;
else p=rnk[p]>rnk[m]?m:p;
}
if(now>maxn)maxn=now,pos=p,len=i;
else if(now==maxn&&rnk[pos]>rnk[p])pos=p,len=i;
}
}
}
int main(){
int js=0;
while(1){
js++;
cin>>s+1;
if(s[1]=='#')break;
n=strlen(s+1);
get_sa(s);
get_height(s);
get_height_st();
find();
printf("Case %d: ",js);
for(int i=pos;i<=pos+len*maxn-1;++i){
putchar(s[i]);
}puts("");
}
}
上一篇: WPF设计时
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848