题目链接:https://vjudge.net/problem/POJ-3693

Maximum repetition substring
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 —— 后缀数组 重复次数最多的连续重复子串的更多相关文章

  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 (后缀数组+rmq)

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

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

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

  5. 【POJ 3693】Maximum repetition substring 重复次数最多的连续重复子串

    后缀数组的论文里的例题,论文里的题解并没有看懂,,, 求一个重复次数最多的连续重复子串,又因为要找最靠前的,所以扫的时候记录最大的重复次数为$ans$,扫完后再后从头暴力扫到尾找重复次数为$ans$的 ...

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

    传送门:POJ - 3693   题意:给你一个字符串,求重复次数最多的连续重复子串,如果有一样的,取字典序小的字符串. 题解: 比较容易理解的部分就是枚举长度为L,然后看长度为L的字符串最多连续出现 ...

  7. poj 3693 后缀数组 重复次数最多的连续重复子串

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

  8. spoj687 后缀数组重复次数最多的连续重复子串

    REPEATS - Repeats no tags  A string s is called an (k,l)-repeat if s is obtained by concatenating k& ...

  9. SPOJ - REPEATS —— 后缀数组 重复次数最多的连续重复子串

    题目链接:https://vjudge.net/problem/SPOJ-REPEATS REPEATS - Repeats no tags  A string s is called an (k,l ...

随机推荐

  1. 奇怪!post提交 地址栏参数竟然可见

    转:    http://blog.csdn.net/yuebinghaoyuan/article/details/7727802 在做项目中,form标签中method="post&quo ...

  2. 【AngularJS】Yeoman安装

    看不到PPT的请自行解决DNS污染问题.

  3. zepto jquery和zepto的区别?

    jQuery 由于强大的生命力基本上是一个事实标准,所以大部分工具 lib 在 DOM 操作.动画等功能上或多或少都会是 jQuery-like 的. Zepto 的 API 就是完全兼容 jQuer ...

  4. 【Python】python3中urllib爬虫开发

    以下是三种方法 ①First Method 最简单的方法 ②添加data,http header 使用Request对象 ③CookieJar import urllib.request from h ...

  5. Java使用笔记之对象比较

    1.关于java对象的比较,经常会遇见比较某个两个对象的多个属性是否相等,可以通过重写对象equals方法来实现. 比如有两个User,如果姓名和年龄相等的话,我们就可以认为他们重复的数据.那么我们就 ...

  6. VueJS组件之全局组件与局部组件

    全局组件 所有实例都能用全局组件. HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  7. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

  8. HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程

    在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...

  9. Android API Guides---Supporting Tablets and Handsets

    在Android平台上的各种屏幕尺寸的执行和系统调整大小正常应用程序的用户界面.以适应每一个人. 通常情况下,你须要做的是设计你的UI是灵活的,并通过提供替代资源(如又一次定位的一些看法观点或替代尺寸 ...

  10. J2EE——开发环境搭建

    WEB环境搭建 1.J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 2.JAVA运行环境和J2EE运行环境的搭建 3.jsp开发所需要的eclipse插件(lomboz.tom ...