POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串
题目链接:https://vjudge.net/problem/POJ-3693
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11250 | Accepted: 3465 |
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
题意:
给出一个字符串,求该字符串的重复次数最多的连续重复子串,输出该子串,如果有多个答案,输出字典序最小的那个。
题解:
SPOJ - REPEATS的加强版,需要输出目标串。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; bool cmp(int *r, int a, int b, int l)
{
return r[a]==r[b] && r[a+l]==r[b+l];
} int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
int t1[MAXN], t2[MAXN], c[MAXN];
void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
{
n++;
int i, j, p, *x = t1, *y = t2;
for(i = ; i<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[i] = str[i]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
for(j = ; j<=n; j <<= )
{
p = ;
for(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<m; i++) c[i] = ;
for(i = ; i<n; i++) c[x[y[i]]]++;
for(i = ; i<m; i++) c[i] += c[i-];
for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i]; swap(x, y);
p = ; x[sa[]] = ;
for(i = ; i<n; i++)
x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++; if(p>=n) break;
m = p;
} int k = ;
n--;
for(i = ; i<=n; i++) Rank[sa[i]] = i;
for(i = ; i<n; i++)
{
if(k) k--;
j = sa[Rank[i]-];
while(str[i+k]==str[j+k]) k++;
height[Rank[i]] = k;
}
} int dp[MAXN][], mm[MAXN];
void initRMQ(int n, int b[])
{
mm[] = -;
for(int i = ; i<=n; i++)
dp[i][] = b[i], mm[i] = ((i&(i-))==)?mm[i-]+:mm[i-];
for(int j = ; j<=mm[n]; j++)
for(int i = ; i+(<<j)-<=n; i++)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int x, int y)
{
if(x>y) swap(x, y);
x++;
int k = mm[y-x+];
return min(dp[x][k], dp[y-(<<k)+][k]);
} char str[MAXN];
int Len[MAXN];
int main()
{
int kase = ;
while(scanf("%s", str) && str[]!='#')
{
int n = strlen(str);
for(int i = ; i<n; i++)
r[i] = str[i];
r[n] = ;
DA(r, sa, Rank, height, n, );
initRMQ(n, height); int times = , cnt = ;
for(int len = ; len<=n; len++)
for(int pos = ; pos+len<n; pos += len)
{
int LCP = RMQ(Rank[pos], Rank[pos+len]);
int supplement = len - LCP%len;
int k = pos - supplement;
if(k>= && LCP%len && RMQ(Rank[k],Rank[k+len])>=supplement)
LCP += supplement;
/*
当不能加上supplement时,以pos为起点的子串不一定是字典序最小,
而应该在[pos, pos+LCP%len]里面取最小,所以为了取得字典序最小,
先不记录位置,而只记录出现次数最大的情况下有多少种循环节,等
统计完之后,再按排名从前到后,为每个sa[i]匹配循环节,匹配成功
即为答案。
*/
int tmp = LCP/len+;
if(tmp>times) times = tmp, Len[cnt=] = len;
else if(tmp==times) Len[++cnt] = len;
}
int L, R, flag = true;
for(int i = ; i<=n && flag; i++)
for(int j = ; j<=cnt; i++)
{
int len = Len[j];
int LCP = RMQ(i, Rank[sa[i]+len]);
if(LCP>=len*(times-))
{
L = sa[i];
R = sa[i]+len*times-;
flag = false;
break;
}
}
printf("Case %d: ", ++kase);
for(int i = L; i<=R; i++)
putchar(str[i]);
putchar('\n');
}
}
POJ3693 Maximum repetition substring —— 后缀数组 重复次数最多的连续重复子串的更多相关文章
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- POJ3693 Maximum repetition substring 后缀数组
POJ - 3693 Maximum repetition substring 题意 输入一个串,求重复次数最多的连续重复字串,如果有次数相同的,则输出字典序最小的 Sample input ccab ...
- poj3693 Maximum repetition substring (后缀数组+rmq)
Description The repetition number of a string is defined as the maximum number R such that the strin ...
- POJ-3693-Maximum repetition substring(后缀数组-重复次数最多的连续重复子串)
题意: 给出一个串,求重复次数最多的连续重复子串 分析: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现几次. 既然长度为L的串重复出现,那么str[0],str[l],str ...
- 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串
后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...
- POJ - 3693 Maximum repetition substring(重复次数最多的连续重复子串)
传送门:POJ - 3693 题意:给你一个字符串,求重复次数最多的连续重复子串,如果有一样的,取字典序小的字符串. 题解: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现 ...
- poj 3693 后缀数组 重复次数最多的连续重复子串
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8669 Acc ...
- spoj687 后缀数组重复次数最多的连续重复子串
REPEATS - Repeats no tags A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...
- SPOJ - REPEATS —— 后缀数组 重复次数最多的连续重复子串
题目链接:https://vjudge.net/problem/SPOJ-REPEATS REPEATS - Repeats no tags A string s is called an (k,l ...
随机推荐
- curses.h头文件不存在解决办法
sudo apt-get install libncurses5-dev安装,系统自带库文件一般在/usr/include下面,这个是安装curses.h的 conio不是c语言标准库,也不是posi ...
- Scala 中Array,List,Tuple的差别
尽管学了一段时间的Scala了,可是总认为基础不是太扎实,还有非常多的基础知识比較模糊.于是近期又打算又一次学习基础. Scala中的三种集合类型包含:Array,List,Tuple.那么究竟这三种 ...
- 上传项目至svn服务器,从svn上获取项目
1.在桌面右键->TortoiseSVn->Repo_brower->输入地址,进入 ,ok 2.在地址目录上右键==>>add folder==>>选择你要 ...
- void 0 或者 undefined
Problem 在检查一个值是否为undefined的时候.你们是假设去測试的? 要是之前的我会这样子測试 function isUndefined(obj){ return obj === unde ...
- centos部署Python环境
在centos上部署Python之前,我们需要先配置开发环境. 1.安装Python依赖的开发工具包 gcc自然少不了,可以直接用“Development Tools”: yum grouplist ...
- nyist oj 37 回文字符串 (动态规划经典)
回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串.从左到右读和从右到左读是全然一样的.比方"aba".当 ...
- python学习(十)赋值、表达式、if、while、for
明天以搞定这几个应该不难 赋值.表达式.if.while.for 函数.作用域.参数.函数高级话题 迭代和解析一.二 还有我的<30天自制操作系统>没看 #!/usr/bin/python ...
- 巧用redis位图存储亿级数据与访问
业务背景 现有一个业务需求,需要从一批很大的用户活跃数据(2亿+)中判断用户是否是活跃用户.由于此数据是基于用户的各种行为日志清洗才能得到,数据部门不能提供实时接口,只能提供包含用户及是否活跃的指定格 ...
- java log4j 日志文件
开发中经常会用到log日志文件,根据业务需要可能不产生很大日志文件给维护和[排错带来了麻烦.所以我们希望能够每天或每个月产生一个日志文件,这样文件不至于过大. 或者根据日志文件大小来判断,超过规定大小 ...
- 【BZOJ3630】[JLOI2014]镜面通道 几何+最小割
[BZOJ3630][JLOI2014]镜面通道 Description 在一个二维平面上,有一个镜面通道,由镜面AC,BD组成,AC,BD长度相等,且都平行于x轴,B位于(0,0).通道中有n个外表 ...