Description

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

题意:和spoj687题意相同,只是最后要输出满足重复次数最多的字典序最小的字符串。我们可以把重复次数最多的循环长度保存下来,然后根据sa数组依次找开头的字母,判断下以这个位置字母为首字母,循环长度为保存下的数组中的其中一个值是否符合要求,如果符合就break。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lson th<<1
#define rson th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100010
int sa[maxn],a[maxn];
int wa[maxn],wb[maxn],wv[maxn],we[maxn];
int rk[maxn],height[maxn];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void build_sa(int *r,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[x[i]=r[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p){
for(p=0,i=n-j;i<n;i++)y[p++]=i;
for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=0;i<n;i++)wv[i]=x[y[i]];
for(i=0;i<m;i++)we[i]=0;
for(i=0;i<n;i++)we[wv[i]]++;
for(i=1;i<m;i++)we[i]+=we[i-1];
for(i=n-1;i>=0;i--)sa[--we[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
} void calheight(int *r,int n)
{
int i,j,k=0;
for(i=1;i<=n;i++)rk[sa[i]]=i;
for(i=0;i<n;height[rk[i++] ]=k){
for(k?k--:0,j=sa[rk[i]-1];r[i+k]==r[j+k];k++);
}
} int minx[maxn][30];
void init_rmq(int n)
{
int i,j;
height[1]=inf;
for(i=1;i<=n;i++)minx[i][0]=height[i];
for(j=1;j<=16;j++){
for(i=1;i<=n;i++){
if(i+(1<<j)-1<=n){
minx[i][j]=min(minx[i][j-1],minx[i+(1<<(j-1))][j-1]);
}
}
}
} int lcp(int l,int r)
{
int k,i;
l=rk[l];r=rk[r];
if(l>r)swap(l,r);
l++;
k=(log((r-l+1)*1.0)/log(2.0));
return min(minx[l][k],minx[r-(1<<k)+1][k]);
} char ss[maxn];
int b[maxn]; int main()
{
int n,m,i,j,T,l,beishu,yushu,len,k,h,cas=0;
while(scanf("%s",ss)!=EOF)
{
if(strcmp(ss,"#")==0)break;
n=strlen(ss);
for(i=0;i<n;i++){
a[i]=ss[i]-'a'+97+1;
}
a[n]=0;
build_sa(a,n+1,130);
calheight(a,n);
init_rmq(n);
int tot=0;
int ans=-1;
for(l=1;l<=n;l++){
for(i=0;i+l<n;i+=l){
len=lcp(i,i+l );
beishu=len/l+1;
yushu=len%l;
if(i-(l-yushu)>=0 && lcp(i-(l-yushu),i+l-(l-yushu) )>=len ){
beishu++;
}
if(ans==-1){
ans=beishu;
b[++tot]=l;
}
else if(beishu>ans){
tot=0;
b[++tot]=l;
ans=beishu;
}
else if(beishu==ans){
b[++tot]=l;
}
}
}
int flag=0;
for(i=1;i<=n;i++){
for(j=1;j<=tot;j++){
if(lcp(sa[i],sa[i]+b[j] )>=(ans-1)*b[j] ){ //这里不能取等号,因为并不是所有的串都是恰好匹配
flag=1;
break;
}
}
if(flag)break;
} cas++;
printf("Case %d: ",cas);
for(k=1;k<=ans;k++){
for(h=1;h<=b[j];h++){
printf("%c",ss[sa[i]+h-1]);
}
}
printf("\n");
}
return 0;
} /*
babbabaabaabaabab */

poj3693 Maximum repetition substring (后缀数组+rmq)的更多相关文章

  1. POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串

    题目链接:https://vjudge.net/problem/POJ-3693 Maximum repetition substring Time Limit: 1000MS   Memory Li ...

  2. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  3. POJ3693 Maximum repetition substring 后缀数组

    POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...

  4. POJ 3693 Maximum repetition substring (后缀数组+RMQ)

    题意:给定一个字符串,求其中一个由循环子串构成且循环次数最多的一个子串,有多个就输出最小字典序的. 析:枚举循环串的长度ll,然后如果它出现了两次,那么它一定会覆盖s[0],s[ll],s[ll*2] ...

  5. Maximum repetition substring 后缀数组

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7578   Acc ...

  6. POJ 3693 Maximum repetition substring ——后缀数组

    重复次数最多的字串,我们可以枚举循环节的长度. 然后正反两次LCP,然后发现如果长度%L有剩余的情况时,答案是在一个区间内的. 所以需要找到区间内最小的rk值. 两个后缀数组,四个ST表,$\Thet ...

  7. 【Poj-3693】Maximum repetition substring 后缀数组 连续重复子串

    POJ - 3693 题意 SPOJ - REPEATS的进阶版,在这题的基础上输出字典序最小的重复字串. 思路 跟上题一样,先求出最长的重复次数,在求的过程中顺便纪录最多次数可能的长度. 因为sa数 ...

  8. poj 3693 Maximum repetition substring (后缀数组)

    其实是论文题.. 题意:求一个字符串中,能由单位串repeat得到的子串中,单位串重复次数最多的子串.若有多个重复次数相同的,输出字典序最小的那个. 解题思路:其实跟论文差不多,我看了很久没看懂,后来 ...

  9. poj3693 Maximum repetition substring

    题意 给出一个长度为\(n(n\leqslant 100000)\)的串,求一个字典序最小的子串使得它是某个字符串重复\(k\)次得到的,且\(k\)最大 题解 后缀数组论文上的题,跟上一篇uva那个 ...

随机推荐

  1. 前端面试:Http协议与浏览器

    Http与Https的区别 Http是明文传输的,Https协议是在Http协议上添加了SSL的加密协议,可以进行加密传输和身份验证. 其实就是说Http对网络传输完全是裸奔状态,也就没办法防范中间人 ...

  2. Linux Bash Shell常用快捷键

    Linux Bash Shell常用快捷键 table { margin: auto } 快捷键 功能 tab 补全 ctrl + a 光标回到命令行首 ctrl + e 光标回到命令行尾 ctrl ...

  3. 【Python】在CentOS6.8中安装pip9.0.1和setuptools33.1

    wget https://bootstrap.pypa.io/ez_setup.py python ez_setup.py install --如果这个文件安装需要下载的文件无法下载的话,手动下载,放 ...

  4. leetcode 1593. 拆分字符串使唯一子字符串的数目最大(DFS,剪枝)

    题目链接 leetcode 1593. 拆分字符串使唯一子字符串的数目最大 题意: 给你一个字符串 s ,请你拆分该字符串,并返回拆分后唯一子字符串的最大数目. 字符串 s 拆分后可以得到若干 非空子 ...

  5. 【译】Async/Await(三)——Aysnc/Await模式

    原文标题:Async/Await 原文链接:https://os.phil-opp.com/async-await/#multitasking 公众号: Rust 碎碎念 翻译 by: Praying ...

  6. Centos7.4 小白式安装(初学)

    虚拟机安装Centos7.4系统 适用人群(初学者) 下载Centos7.4镜像 https://pan.baidu.com/s/1NtjfdHV3OWAvfDj5vrR7HQ  提取码:hzzw 虚 ...

  7. SpringMVC下关于静态资源访问

    SpringMVC静态资源访问 聊一聊关于静态资源的访问问题 首先,我们要对web.xml里面的DispatcherServlet进行设置 <!-- 中央调度器--> <servle ...

  8. uni-app开发经验分享九: 组件传值

    一.父组件向子组件传值 通过props来实现,子组件通过props来接收父组件传过来的值! 1.逻辑梳理 父组件中: 第一步:引入子组件: import sonShow from '../../com ...

  9. kettle 连接oracle12c问题解决办法:

    在oracle的安装文件目录......\NETWORK\ADMIN\sqlnet.ora 文件中添加该语句:SQLNET.ALLOWED_LOGON_VERSION_SERVER = 8window ...

  10. Webpack4.0各个击破(5)module篇

    一. 模块化乱炖 脚本合并是基于模块化规范的,javascript模块化是一个非常混乱的话题,各种[*MD]规范乱飞还要外加一堆[*.js]的规范实现.现代化前端项目多基于框架进行开发,较为流行的框架 ...