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.

题目大意:给一个字符串,求重复次数最多的连续重复子串,如有多个答案输出字典序最小的。

思路:对于一个由长度为L的字符串重复R次形成的子串,那么对于s[0]、s[L]、s[2*L]……,该子串必然包含其中的两个字符

那么,我们从1~n穷举长度L

对于每一个s[i*L]、s[(i+1)*L]看看它们能往前和往后同时匹配多长

记这个长度为K,那么K/L+1就是可以重复的次数(对于一个字符串如果他是由R个长度为L的字符串重复形成的,那么必然有lcp(suffix(0), suffix(L))==L*(R-1))

然后,从两个点往后匹配好求,但往前匹配就不好办了,虽然可以把字符串反过来再弄一个后缀数组,但是这不够优美。

比较优美的方法就是,对于某个连续重复子串,假设它的包含的最前面的两个是s[i*L]和s[(i+1)*L],设p=L-lcp%L(当lcp mod L ≠ 0)

那么只需要测试lcp(i*L-p, (i+1)*L-p)就行了,因为再往前,重复的次数也不会增加。

然后我们就可以得到最大重复次数了。

但是题目要求的是字典序最小的答案耶……要在算重复次数的时候也算出来好像很有难度(应该说是很麻烦,大概就是DISCUSS里面那个说3个RMQ的人……)

但是,我们再算最大重复次数的时候,把长度也算出来,设为K

那么只要遍历一下字符串,把lcp(i, i + K / R) ≥ K - K / R的找出来,这些都是符合条件的答案开头,因为有后缀数组,直接找rank最小的就行了。

这样,这题就做完了。

代码(313MS):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ; char s[MAXN];
int sa[MAXN], height[MAXN], rank[MAXN], c[MAXN], tmp[MAXN];
int n; void makesa(int m) {
memset(c, , m * sizeof(int));
for(int i = ; i < n; ++i) ++c[rank[i] = s[i]];
for(int i = ; i < m; ++i) c[i] += c[i - ];
for(int i = ; i < n; ++i) sa[--c[rank[i]]] = i;
for(int k = ; k < n; k <<= ) {
for(int i = ; i < n; ++i) {
int j = sa[i] - k;
if(j < ) j += n;
tmp[c[rank[j]]++] = j;
}
int j = c[] = sa[tmp[]] = ;
for(int i = ; i < n; ++i) {
if(rank[tmp[i]] != rank[tmp[i - ]] || rank[tmp[i] + k] != rank[tmp[i - ] + k])
c[++j] = i;
sa[tmp[i]] = j;
}
memcpy(rank, sa, n * sizeof(int));
memcpy(sa, tmp, n * sizeof(int));
}
} void calheight() {
for(int i = , k = ; i < n; height[rank[i++]] = k) {
if(k > ) --k;
int j = sa[rank[i] - ];
while(s[i + k] == s[j + k]) ++k;
}
} int logn[MAXN];
int best[][MAXN]; void initRMQ() {
logn[] = -;
for(int i = ; i <= n; ++i)
logn[i] = (i & (i - )) == ? logn[i - ] + : logn[i - ];
for(int i = ; i <= n; ++i) best[][i] = height[i];
for(int i = ; i <= logn[n]; ++i) {
int ed = n - ( << i) + ;
for(int j = ; j <= ed; ++j)
best[i][j] = min(best[i - ][j], best[i - ][j + ( << (i - ))]);
}
} int lcp(int a, int b) {
a = rank[a], b = rank[b];
if(a > b) swap(a, b);
++a;
int t = logn[b - a + ];
return min(best[t][a], best[t][b - ( << t) + ]);
} void solve() {
int ans = , ansL = , ansR = ;
for(int i = ; i < n - ; ++i) if(s[i] < s[ans]) ans = i;
for(int i = ; i < n; ++i) {
for(int j = ; j + i < n - ; j += i) {
int t = lcp(j, j + i), p = ;
if(t % i) {
p = i - t % i;
if(j < p) p = ;
t = max(t, lcp(j - p, j + i - p));
}
if(t / i + > ansR || (t / i + == ansR && rank[j] < rank[ans])) {
ans = j - p;
ansR = t / i + ;
ansL = ansR * i;
}
}
}
for(int i = ; i < n - ; ++i)
if(lcp(i, i + ansL / ansR) >= ansL - ansL / ansR && rank[i] < rank[ans]) ans = i;
for(int i = ans; i < ans + ansL; ++i) putchar(s[i]);
puts("");
} int main() {
int kase = ;
while(scanf("%s", s) != EOF) {
if(*s == '#') break;
n = strlen(s) + ;
makesa();
calheight();
initRMQ();
printf("Case %d: ", ++kase);
solve();
}
}

POJ 3693 Maximum repetition substring(后缀数组)的更多相关文章

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

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

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

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

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

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

  4. POJ3693 Maximum repetition substring 后缀数组

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

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

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

  6. POJ 3693 Maximum repetition substring(最多重复次数的子串)

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10461   Ac ...

  7. Maximum repetition substring 后缀数组

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

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

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

  9. POJ 3693 Maximum repetition substring(后缀数组+ST表)

    [题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...

随机推荐

  1. javascript根据文件字节数返回文件大小

    function getFileSize(fileByte) { var fileSizeByte = fileByte; var fileSizeMsg = ""; if(fil ...

  2. php函数strtotime结合date时间修饰语的使用

    下面简单介绍在项目开发中date时间函数和strtotime所遇到的问题,以及解决办法. 原文地址:小时刻个人技术博客 > http://small.aiweimeng.top/index.ph ...

  3. thinkphp3.2.3 HTML 页面跳转

    1.   http://域名/index.php(入口文件)/模块/控制器/方法 2.{:U('控制器/方法')}

  4. Python系列之入门篇——python2.7.13安装

    Python2.7.13 安装 说明 以下所有操作都基于centos6.9 1. Issue zlib zlib-devel是安装setuptools依赖的模块,需要在安装python之前先安装这两个 ...

  5. CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes']

    CentOS6升级Python2.6到3.7,错误处理[No module named '_ctypes'] 因开发需要,在CentOS 6 服务器将Python2进行升级到Python3.由于工作中 ...

  6. Python学习 :集合

    集合 Set 集合的创建 集合的创建只有一种方式 集合中的元素必须是不可变的数据类型 集合是无序的,可以通过 for 循环来遍历或者迭代器进行筛选 s=set('xiaoming') s1=['ale ...

  7. VMWare共享文件

    windows与虚拟机的linux共享windows下的一个文件夹 1.重新安装VMware Tools,在VMware面板上选择“虚拟机-重新安装VMware tools…” 2.使用命令 Ctrl ...

  8. EEPROM读写学习笔记与I2C总线(二)

    无论任何电子产品都会涉及到数据的产生与数据的保存,这个数据可能并不是用来长久保存,只是在运行程序才会用到,有些数据体量较大对于获取时效性并不太强,各种各样的数据也就有不同的存储载体,这次在EEPROM ...

  9. BZOJ1303_中位数图_KEY

    题目传送门 较水,开两个桶即可. 题目可以理解为,将大于B的数看为1,小于B的数看为-1,将以B这个数为中位数的序列左右分为两半,加起来为0. code: #include <cstdio> ...

  10. BZOJ1053_反素数_KEY

    题目传送门 初看这道题,以为是一道挺难的题目,但仔细看发现,不是只要爆搜就好了吗? 只需要对前12个素数进行爆搜即可. 一个数的因数个数=素数次数+1全部乘起来. code: /*********** ...