题目链接: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. 【Salvation】——项目进展&已取得的成果

    写在前面:这个项目为原创团体项目,其中美术设计与部分关卡功能为其他成员完成,我负责的部分以角色动画和登录注册为主. 一.游戏美术设计 游戏背景,道具,动物,人物帧动画制作全部完成. 1.人物 2.游戏 ...

  2. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...

  3. 【日常学习】【并查集+map】codevs2639 约会计划题解

    然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系 ...

  4. maven springmvc-hibernate搭建以及源码下载

    见:https://blog.csdn.net/fengshizty/article/details/43635305 下载源码参见:http://www.cnblogs.com/ljy2013/p/ ...

  5. struts2获取服务器临时目录

      CreateTime--2017年9月7日08:57:39 Author:Marydon struts2获取服务器(tomcat.WebLogic)的临时目录 需要导入: import java. ...

  6. layui-字体图标

    layui官网下载:GitHub:https://github.com/sentsin/layui/ layui官网首页-下载:http://www.layui.com/ layui-字体图标-官方网 ...

  7. Objective-C 执行AppleScript脚本

    在Objective-C里事实上也能够执行AppleScript 第一种方式是Source 将脚本写到变量字符串里 NSAppleEventDescriptor *eventDescriptor = ...

  8. Struts2学习三----------Action搜索顺序

    © 版权声明:本文为博主原创文章,转载请注明出处 Struts2的Action的搜索顺序 http://localhost:8080/path1/path2/student.action 1)判断pa ...

  9. 关于C++项目指针对象未被初始化的问题(0xcdcdcd)

    http://blog.csdn.net/devfun/article/details/6900086 昨天我试图将一个封装好的模块加入到正在开发的项目中,这个模块不是单独的类,而且对应的声明和实例. ...

  10. centOS解决乱码问题

    问题描述:输入javac出现乱码,部分字符不能显示解决方法 echo 'export LANG=en_US.UTF-8' >> ~/.bashrc