POJ 3693 Maximum repetition substring(后缀数组)
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(后缀数组)的更多相关文章
- POJ 3693 Maximum repetition substring ——后缀数组
重复次数最多的字串,我们可以枚举循环节的长度. 然后正反两次LCP,然后发现如果长度%L有剩余的情况时,答案是在一个区间内的. 所以需要找到区间内最小的rk值. 两个后缀数组,四个ST表,$\Thet ...
- poj 3693 Maximum repetition substring (后缀数组)
其实是论文题.. 题意:求一个字符串中,能由单位串repeat得到的子串中,单位串重复次数最多的子串.若有多个重复次数相同的,输出字典序最小的那个. 解题思路:其实跟论文差不多,我看了很久没看懂,后来 ...
- POJ 3693 Maximum repetition substring (后缀数组+RMQ)
题意:给定一个字符串,求其中一个由循环子串构成且循环次数最多的一个子串,有多个就输出最小字典序的. 析:枚举循环串的长度ll,然后如果它出现了两次,那么它一定会覆盖s[0],s[ll],s[ll*2] ...
- POJ3693 Maximum repetition substring 后缀数组
POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ 3693 Maximum repetition substring(最多重复次数的子串)
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10461 Ac ...
- Maximum repetition substring 后缀数组
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7578 Acc ...
- POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
题目链接:https://vjudge.net/problem/POJ-3693 Maximum repetition substring Time Limit: 1000MS Memory Li ...
- POJ 3693 Maximum repetition substring(后缀数组+ST表)
[题目链接] poj.org/problem?id=3693 [题目大意] 求一个串重复次数最多的连续重复子串并输出,要求字典序最小. [题解] 考虑错位匹配,设重复部分长度为l,记s[i]和s[i+ ...
随机推荐
- 【Java】使用Atomic变量实现锁
Atomic原子操作 在 Java 5.0 提供了 java.util.concurrent(简称JUC)包,在此包中增加了在并发编程中很常用的工具类 Java从JDK1.5开始提供了java.uti ...
- 『ACM C++』 PTA 天梯赛练习集L1 | 007-011
真的是忙头晕了,学业.ACM打题.班级活动.自学新东西,哇这充实的大学~ ------------------------------------------------L1-007--------- ...
- kali linux 安装TIM or QQ(CrossOver 安装 QQ)
需要文件 http://www.crossoverchina.com/xiazai.html dpkg --add-architecture i386 apt-get update apt-get i ...
- 深入理解bit_or和bit_and,bit_count
bit_or:二进制数按位或,bit_and:二进制数按位与,bit_count:统计二进制数1个个数 下面以一个例子来说明用法:示例要实现的功能就是计算每月有几天有访问,先把示例摘录在这里.1234 ...
- Python入门 —— 03GUI界面编程
GUI(Graphical User Interface) 即图形用户接口,又称图形用户接口. 是指采用图形方式显示的计算机操作用户界面.GUI 是屏幕产品的视觉体验和互动操作部分. "你的 ...
- Python提高篇
Python提高篇 1.模块 1)模块定义 模块就是py文件,可以是你自己写的,也可以是python当中自带的工具,当你在某个py文件下想要引用其他模块的功能,就需要你把你把该py文件导入. 2)导入 ...
- Apache 错误:httpd: Could not open configuration file
神奇的事件,折磨我 电脑关机重启了一下关机之前正常的状态没有任何的异常出现,过了一会开机准备工作.神奇的事情tmd出现了!!!! 打开phpstudy 启动... 嗯?apache亮红报错?? 第一反 ...
- angular2路由之routerLinkActive指令
angular2的routerLinkActive指令在路由激活时添加样式class .red{ color: red;} <a routerLink="/user/login ...
- HDL代码风格建议(1)使用示例和IP
Recommended HDL Coding Styles HDL coding styles can have a significant effect on the quality of resu ...
- [原创]用python实现系统状态监测和故障转移
#coding: utf-8 import socket import select import time import os import threading def ser(): s = soc ...