Maximum repetition substring
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8669   Accepted: 2637

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.

/*
poj 3693 后缀数组 重复次数最多的连续重复子串 给你一个字符串,求里面重复次数最多的字符串,卒
ccabababc -> ababab = 3 ababa = 1 表示论文里面的思路并没看懂,主要还是参考别人写好的代码的
首先,枚举l(用来重复的长度),判断suff[i],suff[i+l]
如果公共前缀k%l != 0,则说明这个长度不合适,修改后再进行判断。
于是考虑k%i,可以看成后面多了k%l个字符,但可以看成前面少了m = l-k%l
个字符,于是成了求 l-i-m,l-m的情况,再与之前的结果取较大值即可
然后记录最大次数cnt和符合条件的所有解a[] 最后进行判断,因为要求字典序最小,所以从sa[1]开始判断,如果
su[sa[i]]和su[sa[i]+a[j]]的公共前缀大于等于(cnt-1)*a[j]
则说明满足 hhh-2016-03-13 21:37:55
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
typedef long long ll;
typedef long double ld;
#define lson (i<<1)
#define rson ((i<<1)|1)
const int maxn = 100100; int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] &&r[l+a] == r[l+b];
} void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
n++;
int p,*x=t1,*y=t2;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
for(int j = 1; j <= n; j <<= 1)
{
p = 0;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; swap(x,y);
p = 1;
x[sa[0]] = 0;
for(int i = 1; i < n; i++)
x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(int i = 0; i <= n; i++)
Rank[sa[i]] = i;
for(int i = 0; i < n; i++)
{
if(k) k--;
int j = sa[Rank[i]-1];
while(str[i+k] == str[j+k]) k++;
height[Rank[i]] = k;
}
} int mm[maxn];
int dp[20][maxn];
int Rank[maxn],height[maxn];
int sa[maxn],str[maxn];
char ts[maxn]; void ini_RMQ(int n)
{
mm[0] = -1;
for(int i = 1;i <= n;i++)
mm[i] = (((i & (i-1)) == 0) ? mm[i-1]+1:mm[i-1]); for(int i =1;i <= n;i++)
dp[0][i] = height[i];
for(int i = 1;i <= mm[n];i++)
{
for(int j = 1;j+(1<<i)-1 <= n;j++)
{
int a = dp[i-1][j];
int b = dp[i-1][j+(1<<(i-1))];
dp[i][j] = min(a,b);
}
}
} int askRMQ(int a,int b)
{
if(a > b) swap(a,b);
a++;
int t = mm[b-a+1];
b -= (1<<t)-1;
return min(dp[t][a],dp[t][b]);
}
int a[maxn];
int main()
{
int cas = 1;
while(scanf("%s",ts) != EOF)
{
if(ts[0] == '#')
break;
int len = strlen(ts);
for(int i = 0; i < len; i++)
str[i] = ts[i];
str[len] = 0;
printf("Case %d: ",cas++);
get_sa(str,sa,Rank,height,len,150);
ini_RMQ(len);
int cnt = 0,tot = 0;
for(int i = 1;i <= len;i++)
{
for(int j = i;j < len;j+=i)
{
int tk = askRMQ(Rank[j-i],Rank[j]);
int m = i - tk%i; if(j > i && tk%i) tk = max(tk,askRMQ(Rank[j-i-m],Rank[j-m]));
if(tk % i) tk = 0;
if(tk) tk = tk/i+1;
if(tk > cnt)
cnt = tk,tot=0,a[tot++]=i;
else if(tk == cnt && a[tot-1] != i)
a[tot++] = i;
}
}
// cout <<cnt <<endl;
int flag = 0;
for(int i = 1;i < len && !flag;i++)
{
for(int j = 0;j < tot && !flag;j++)
{
if(askRMQ(Rank[sa[i]],Rank[sa[i]+a[j]])>=a[j]*(cnt-1))
{
ts[sa[i]+a[j]*cnt] = '\0';
printf("%s\n",ts+sa[i]);
flag = 1;
}
}
}
}
return 0;
}

  

最新文章

  1. MySQL延迟复制--percona-toolkit和MASTER TO MASTER_DELAY
  2. matlab里的svmtrain的输出model里,各参数的含义
  3. DateTime 详解
  4. 支付宝APP支付开发- IOException : DerInputStream.getLength(): lengthTag=127, too big.
  5. CF 405C Unusual Product(想法题)
  6. HTML表单元素登陆界面
  7. poj 2653 (线段相交判断)
  8. 函数xdes_set_bit
  9. 在MVC项目中使用RDLC报表
  10. 44、网页启动Activity,网页传值Activity
  11. 创建Unity新项目并编译成游戏程序
  12. UVa 11488 - Hyper Prefix Sets
  13. PHP 类的封装和使用
  14. JS框架设计读书笔记之-小知识
  15. Filebeat轻量级日志采集工具
  16. 事后诸葛亮分析——Beta版本
  17. pycallgraph 追踪Python函数内部调用
  18. (转)Spring Boot 2 (九):【重磅】Spring Boot 2.1.0 权威发布
  19. js间隔一段时间打印数据库中的值
  20. Python—迭代器与生成器

热门文章

  1. mongodb 集群分片
  2. find命令之(-atime,-ctime,-mtime)
  3. VMware虚拟机误删除vmdk文件后如何恢复?
  4. slf4j 与 log4j2 基本用法
  5. jupyter notebook下python2和python3共存(Ubuntu)
  6. JQ.ajax 各种参数及属性设置 ( 转载 )
  7. LxmlLinkExtractor类参数解析
  8. Spark入门(1-5)Spark统一了TableView和GraphView
  9. Spring Security 入门(3-10)Spring Security 的四种使用方式
  10. 表单提交中的input、button、submit的区别