POJ3693(SummerTrainingDay10-J 后缀数组)
Maximum repetition substring
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 10241 | Accepted: 3157 |
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
//2017-08-10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int inf = 0x3f3f3f3f;
char str[N];
int n, r[N];
int wa[N], wb[N], wv[N], wss[N];
int Suffix[N];//Str下标为i ~ Len的连续子串(即后缀)
int SA[N];//满足Suffix[SA[1]] < Suffix[SA[2]] …… < Suffix[SA[Len]],即排名为i的后缀为Suffix[SA[i]](与Rank是互逆运算)
int Rank[N];//Suffix[i]在所有后缀中的排名
int Height[N];//height[i]表示Suffix[SA[i]]和Suffix[SA[i-1]]的最长公共前缀,也就是排名相邻的两个后缀的最长公共前缀
int H[N];//等于Height[Rank[i]],也就是后缀Suffix[i]和它前一名的后缀的最长公共前缀 //比较母串r中起始位置为a和b,长度都为len的子串是否相等
int cmp(int *r, int a, int b, int len)
{
return r[a]==r[b] && r[a+len]==r[b+len];
} //倍增算法求SA数组。
void da(int *r, int *SA, int n, int m)
{
int i, j, p, *x = wa, *y = wb, *t;
for(i = ; i < m; i++)wss[i] = ;
for(i = ; i < n; i++)wss[x[i]=r[i]]++;
for(i = ; i < m; i++)wss[i]+=wss[i-];
for(i = n-; i >= ; i--)SA[--wss[x[i]]]=i;
for(j = , p = ; p < n; j *= , m = p){
for(p = , 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 < n; i++)
wv[i] = x[y[i]];
for(i = ; i < m; i++)
wss[i] = ;
for(i = ; i < n; i++)
wss[wv[i]]++;
for(i = ; i < m; i++)
wss[i] += wss[i-];
for(i = n-; i >= ; i--)
SA[--wss[wv[i]]] = y[i];
for(t = x, x = y, y = t, p = , x[SA[]]=, i = ; i < n; i++)
x[SA[i]] = cmp(y, SA[i-], SA[i], j)?p-:p++;
}
} //计算height数组
void cal_Height(int *r, int *SA, int n)
{
int i, j, k = ;
for(i = ; i <= n; i++)Rank[SA[i]] = i;
for(i = ; i < n; Height[Rank[i++]] = k)
for(k?k--:, j=SA[Rank[i]-]; r[i+k]==r[j+k]; k++)
;
} int st[N][]; void init_rmq(int n)
{
for(int i=;i<=n;i++) st[i][]=Height[i];
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
{
st[i][j]=min(st[i][j-],st[i+(<<(j-))][j-]);
}
} //询问后缀i和后缀j的最长公共前缀
int lcp(int i,int j)
{
i = Rank[i];
j = Rank[j];
if(i>j) swap(i,j);
i++;
int k=;
while(i+(<<(k+)) <= j) k++;
return min(st[i][k],st[j-(<<k)+][k]);
} int main()
{
int kase = ;
while(scanf("%s", str)!=EOF)
{
if(str[] == '#')break;
n = strlen(str);
for(int i = ; i < n; i++)
r[i] = str[i]-'a'+;
da(r, SA, n+, );
cal_Height(r, SA, n);
init_rmq(n);
int ans = , bg = , ed = , a, b, c;
for(int L = ; *L <= n; L++)
{
for(int i = ; (i+)*L+ < n; i++)
{
a = i*L;
b = (i+)*L;
if(str[a] != str[b])continue;
c = lcp(a, b);
int ll = ;
int rr = b+c-;
for(int j = ; j < L; j++)
{
if(a - j < || str[a-j] != str[b-j])break;
ll = a - j;
int cnt = (rr-ll+)/L;
if(cnt > ans || (cnt == ans && Rank[ll] < Rank[bg]))
{
ans = cnt;
bg = ll;
ed = ll+cnt*L-;
}
}
}
}
printf("Case %d: ", ++kase);
if(ans == )printf("%c\n", str[SA[]]);
else{
for(int i = bg; i <= ed; i++)
printf("%c", str[i]);
printf("\n");
}
} return ;
}
POJ3693(SummerTrainingDay10-J 后缀数组)的更多相关文章
- 【poj3693】Maximum repetition substring(后缀数组+RMQ)
题意:给定一个字符串,求重复次数最多的连续重复子串. 传说中的后缀数组神题,蒟蒻真的调了很久才对啊.感觉对后缀数组和RMQ的模版都不是很熟,导致还是会有很多各种各样的小错误= = 首先,枚举重复子串的 ...
- poj3693(后缀数组)
poj3693 题意 给出一个串,求重复次数最多的连续重复子串,输出字典序最小的. 分析 论文 例8(P21). Sparse-Table算法预处理出任意两个后缀串的LCP. code #includ ...
- 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 ...
- 关于后缀数组的倍增算法和height数组
自己看着大牛的论文学了一下后缀数组,看了好久好久,想了好久好久才懂了一点点皮毛TAT 然后就去刷传说中的后缀数组神题,poj3693是进化版的,需要那个相同情况下字典序最小,搞这个搞了超久的说. 先简 ...
- 【UVA10829】 L-Gap Substrings (后缀数组)
Description If a string is in the form UVU, where U is not empty, and V has exactly L characters, we ...
- 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
题意:UVU形式的串的个数,V的长度规定,U要一样,位置不同即为不同字串 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&am ...
- 【距离GDOI:131天】 后缀数组完毕
用了近两周的时间,终于把罗神那篇后缀数组应用看完了,题目也写了一遍,T了无数次...详见前几篇博文... 后缀数组很重要的是那个height数组,可以用来做各种奇奇怪怪的东西...常用方法去是去二分, ...
- 后缀数组基本问题QAQ
以下题目均来自罗穗骞的论文... No.1最长公共前缀 最长公共前缀: 题目: 给定一个字符串,询问某两个后缀的最长公共前缀. 分析: 某两个后缀的最长公共前缀就是区间height最小值,转化为RMQ ...
- (17/34)AC自动机/后缀数组/后缀自动机(施工中)
快补题别再摸鱼了(17/34) 1.AC自动机 #define maxnode 1000010 #define maxsize 26 struct ahocT{ int ch[maxnode][max ...
随机推荐
- mybatis 插件原理
[传送门]:mybatis 插件原理
- Mac下如何安装配置Homebrew
Last login: Mon Aug 7 13:57:29 on consolexiashenbindeMacBook-Pro:~ xiashenbin$ ruby -e "$(curl ...
- Django自带的后台管理样式找不到的问题。
今天发现自己用uwsgi,nginx部署完服务器后,又想用自带的Django服务器进行后台管理调试,发现Django后代管理页面样式找不到.又查看了路径发现是正确的.网上看了很多方法.最后才发现自己把 ...
- java list集合运算
list集合运算 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.I ...
- Selenium3 + Python3自动化测试系列四——鼠标事件和键盘事件
一.鼠标事件 在 WebDriver 中, 将这些关于鼠标操作的方法封装在 ActionChains 类提供. ActionChains 类提供了鼠标操作的常用方法. ActionChains 类的成 ...
- info.plist的选项含义
1. Application does not run in background(鍵名:UIApplicationExistsOnSuspend)自從iOS4.0之後,當你在應用程式執行的時 ...
- Dev Label显示不同颜色字体
labelControl1.Text = "<size=14>Size = 14<br>" + "<b>Bold</b> ...
- 一、Linq简介
语言集成查询Language Integrated Query(LINQ)是一系列将查询功能集成到C#语言的技术统称. 传统数据查询的缺点: 简单的字符串查询,没有编译时类型检查或Intellisen ...
- vector源码1(参考STL源码--侯捷):源码
vector源码1(参考STL源码--侯捷) vector源码2(参考STL源码--侯捷) vector源码(参考STL源码--侯捷)-----空间分配导致迭代器失效 vector源码3(参考STL源 ...
- 微信小程序图片变形解决方法
微信小程序的image标签中有个mode属性,使用aspectFill即可 注:image组件默认宽度300px.高度225px mode 有效值: mode 有 13 种模式,其中 4 种是缩放模式 ...