Maximum repetition substring
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7578   Accepted: 2281

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

Source

 
题意:给出一串字符,需要求这串字符中的最长重复子串,要是有多个,输出字典序最小的.........
 
 
 

 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=;
// KMP TRIE DFA SUFFIX
int dp[MS][]; // RMQ
int t1[MS],t2[MS],c[MS],v[MS];
int rank[MS],sa[MS],height[MS];
char str[MS],str1[MS];
int s[MS];
int cmp(int *r,int a,int b,int k)
{
return r[a]==r[b]&&r[a+k]==r[b+k];
} void get_sa(int *r,int *sa,int n,int m)
{
int i,j,p,*x=t1,*y=t2;
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[x[i]=r[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[x[i]]]=i;
p=;j=;
for(;p<n;j*=,m=p)
{
for(p=,i=n-j;i<n;i++)
y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=;i<n;i++)
v[i]=x[y[i]];
for(i=;i<m;i++)
c[i]=;
for(i=;i<n;i++)
c[v[i]]++;
for(i=;i<m;i++)
c[i]+=c[i-];
for(i=n-;i>=;i--)
sa[--c[v[i]]]=y[i];
swap(x,y);
x[sa[]]=;
for(p=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
} void get_height(int *r,int n)
{
int i,j,k=;
for(i=;i<=n;i++)
rank[sa[i]]=i;
//height[i]>=height[i-1]-1;
for(i=;i<n;i++)
{
if(k)
k--;
else
k=;
j=sa[rank[i]-];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
} void rmq_init(int n)
{
for(int i=;i<=n;i++) dp[i][]=height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);
} int rmq(int ll,int rr)
{
int k=;
ll=rank[ll];
rr=rank[rr];
if(ll>rr)
{
int tmp=ll;
ll=rr;
rr=tmp;
}
ll++;
while((<<(k+))<=rr-ll+) k++;
return min(dp[ll][k],dp[rr-(<<k)+][k]);
} int main()
{
int text=;
while(scanf("%s",str)>)
{
if(str[]=='#')
break;
int len=strlen(str);
for(int i=;i<len;i++)
s[i]=str[i]-'a'+;
s[len]=;
get_sa(s,sa,len+,);
get_height(s,len);
rmq_init(len);
int ans=,pos=,lenn;
for(int i=;i<=len/;i++)
{
for(int j=;j<len-i;j+=i)
{
if(str[j]!=str[j+i])
continue;
int k=rmq(j,j+i);
int tol=k/i+;
//printf("%d\n",tol);
int r=i-k%i;
int p=j;
int cnt=;
for(int m=j-;m>j-i&&str[m]==str[m+i]&&m>=;m--)
{
cnt++;
if(cnt==r)
{
tol++;
p=m;
}
else if(rank[p]>rank[m])
{
p=m;
}
}
if(ans<tol)
{
ans=tol;
pos=p;
lenn=tol*i;
}
else if(ans==tol&&rank[pos]>rank[p])
{
pos=p;
lenn=tol*i;
}
}
}
printf("Case %d: ",++text);
// printf("%d %d %d\n",ans,pos,lenn);
if(ans<) //这里,如果字符总长度小于2,那么就在原串中找出一个最小的字符就好
{
char ch='z';
for(int i=;i<len;i++)
if(str[i]<ch)
ch=str[i];
printf("%c\n",ch);
continue;
}
for(int i=pos;i<pos+lenn;i++)
printf("%c",str[i]);
printf("\n");
}
return ;
}

Maximum repetition substring 后缀数组的更多相关文章

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

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

  2. POJ3693 Maximum repetition substring 后缀数组

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

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

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

  4. poj3693 Maximum repetition substring (后缀数组+rmq)

    Description The repetition number of a string is defined as the maximum number R such that the strin ...

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

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

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

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

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

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

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

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

  9. POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)

    题意: 给出一个串,求重复次数最多的连续重复子串 分析: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现几次. 既然长度为L的串重复出现,那么str[0],str[l],str ...

随机推荐

  1. cocos2d-x使用python创建vs模板

    cocos2d-x 2.2推荐使用create_project.py创建工程,所有的平台都可以通过这个python文件创建工程.这个文件位置在源码cocos2d-x-2.2.2\tools\proje ...

  2. Dependency Injection学习笔记

    component把需要依赖者(CoffeeMaker)和供应提供者(Heater, Pump)联系起来 使用 区别:上的的依赖是内部创建的,下面的依赖是外面传进来的 注入方式

  3. Samsung Galaxy S II GT-I9100 指令全集 部分指令请慎用

    英文版 谷歌翻译 Obtain/Change Device Information *#06# (Display IMEI number) *#1234# (Display current firmw ...

  4. 关于IE开发人员工具(F12)找不到的问题

    关于IE开发人员工具(F12)找不到的问题 解决方案:第一步,像往常一样F12或者,工具->开发人员工具,点击后,这个时候你是看不到工具界面(当然,如果你正好遇到了找不到这个问题);第二步,这个 ...

  5. iOS版本检测与版本升级

    14年苹果官方要求所有的APP不能出现 “当前版本”字样,是因为从iOS8系统开始,你可以在设置里面设置在WiFi情况下,自动更新安装的APP.此功能大大方便了用户,但是一些用户没有开 启此项功能,因 ...

  6. C3P0连接池使用小结

    C3P0在最近的demo中也用了(我用的是0.9.2.1版本),因为单例很难应付大量并发. 用法详见文档:http://www.mchange.com/projects/c3p0/ 基本的用法在htt ...

  7. c# abstract抽象类与继承类子类的构造函数_base

    http://blog.itpub.net/9240380/viewspace-718054/ http://blog.163.com/cloud_thegreat/blog/static/10367 ...

  8. js 重载i

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. A debugger is already attached

    Today is the last day that all the laptops of winXP OS should be upgrade to WIN7. After updated. whe ...

  10. PPAS上运行pg_dump经过II

    这一次,我用三台机器. 其他步骤和<PPAS上运行pg_dump经过>中讲到的一样. http://www.cnblogs.com/gaojian/p/3195321.html 只是,pg ...