【HDOJ】2459 Maximum repetition substring
后缀数组+RMQ。
/* 2459 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int INF = 0x3f3f3f3f;
const int maxn = 1e5+;
char s[maxn];
int a[maxn];
int rrank[maxn], height[maxn], sa[maxn];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
int dp[maxn][];
int mx[maxn], L[maxn]; bool cmp(int *r, int a, int b, int l) {
return r[a]==r[b] && r[a+l]==r[b+l];
} void da(int *r, int *sa, int n, int m) {
int i, j, *x=wa, *y=wb, *t, p; for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[x[i]=r[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[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) wc[i] = ;
for (i=; i<n; ++i) wc[x[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[wv[i]]] = y[i];
for (t=x, x=y, y=t, p=, i=, x[sa[]]=; i<n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
} void calheight(int *r, int *sa, int n) {
int i, j, k = ; for (i=; i<=n; ++i) rrank[sa[i]] = i;
for (i=; i<n; height[rrank[i++]]=k)
for (k?k--:, j=sa[rrank[i]-]; r[j+k]==r[i+k]; ++k) ;
} void init_RMQ(int n) {
int i, j; for (i=; i<=n; ++i)
dp[i][] = height[i];
dp[][] = INF;
for (j=; (<<j)<=n; ++j)
for (i=; i+(<<j)-<=n; ++i)
dp[i][j] = min(dp[i][j-], dp[i+(<<(j-))][j-]);
} int RMQ(int l, int r) {
if (l > r)
swap(l, r); int k = ;
++l;
while (<<(k+) <= r-l+)
++k; return min(dp[l][k], dp[r-(<<k)+][k]);
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} void solve() {
int n; for (int i=; ; ++i) {
if (s[i] == '\0') {
n = i;
break;
}
a[i] = s[i] - 'a' + ;
}
a[n] = ; da(a, sa, n+, );
calheight(a, sa, n);
init_RMQ(n); int n_ = n >> , tmp;
rep(i, , n_) {
mx[i] = ;
for (int p=, q=i; q<n; p=q, q+=i) {
tmp = RMQ(rrank[p], rrank[q]);
mx[i] = max(mx[i], tmp/i+);
if (tmp%i) {
tmp = RMQ(rrank[p-(i-tmp%i)], rrank[q-(i-tmp%i)]);
mx[i] = max(mx[i], tmp/i+);
}
}
} int ans = ;
rep(i, , n_)
ans = max(ans, mx[i]); int ln = ;
rep(i, , n_) {
if (mx[i] == ans) {
L[ln++] = i;
}
} int beg = , len = ; rep(i, , n+) {
rep(j, , ln) {
int p = sa[i];
if (p+L[j] >= n)
continue; tmp = RMQ(rrank[p], rrank[p+L[j]]);
if (tmp/L[j]+ >= ans) {
beg = p;
len = L[j] * ans;
goto _output;
}
}
} _output:
s[beg+len] = '\0';
puts(s+beg);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t = ; while (scanf("%s", s)!=EOF && (s[]!='#')) {
printf("Case %d: ", ++t);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
数据生成器。
from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 20
bound = 10**2
lc = list(string.lowercase)
for tt in xrange(t):
length = randint(100, 500)
line = ""
for i in xrange(length):
idx = randint(0, 25)
line += lc[idx]
fout.write("%s\n" % line)
fout.write("#\n") def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()
【HDOJ】2459 Maximum repetition substring的更多相关文章
- HDU 2459 Maximum repetition substring
题目:Maximum repetition substring 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2459 题意:给你一个字符串,求连续重复出现 ...
- 【后缀数组】poj3693 Maximum repetition substring
sa在清空方面存在一些奇怪的问题……难以ac.(留坑?)
- 【HDOJ】1403 Longest Common Substring
后缀数组2倍增可解. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXM 28 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- Maximum repetition substring 后缀数组
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7578 Acc ...
- Maximum repetition substring (poj3693 后缀数组求重复次数最多的连续重复子串)
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6328 Acc ...
- 【SP1812】LCS2 - Longest Common Substring II
[SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...
- 【SP1811】LCS - Longest Common Substring
[SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...
随机推荐
- DTcms会员中心添加新页面-会员投稿,获得所有文章并分页
DAL.article.cs /// <summary> /// 自定义:获得查询分页数据 /// </summary> public DataSet GetList(int ...
- System.UnauthorizedAccessException: 拒绝访问 temp 目录。用来运行 XmlSerializer 的标识“NT AUTHORITY\NETWORK SERVICE”没有访问 temp 目录的足够权限。CodeDom 将使用进程正在使用的用户帐户进行编译,这样,如
解决方案:IIS的应用程序池权限不够,应用程序给localsystem账号权限即可. 以客户的服务器系统2003sp2为例,修改步骤如下: 控制面板---管理工具--Internet 信息服务(IIS ...
- 【C#】数据库备份及还原的实现代码【转载】
[转载]http://www.codesky.net/article/200908/128600.html C#数据库备份及还原1.在用户的配置时,我们需要列出当前局域网内所有的数据库服务器,并且要列 ...
- LAMP(Ubuntu+apache+mysql+php)+Zend Studio 新手の PHP的开发环境搭建
因为工作需要,就从c#转型过来研究PHP.可是没想到从一开始就遇上了问题,环境配置方面的问题足足令我头疼了两天.因为博主本人对于linux的接触非常少,所以在解决这个问题的时候也学到了不少东西, 非常 ...
- Demo学习: DownloadDemo
DownloadDemo 学习文件下载 1. 几个获取临时路径的函数: UniServerModule.TempFolderURL //当前程序路径下"Temp"文件夹: Uni ...
- vsftpd本地用户登录密码错误
今天发现自己虚拟机的vsftp使用本地用户名无法登陆,于是重新配置,但配置了很多次都没成功,一直显示 530 Login incorrect. Login failed 尝试了网上很多修改PAM的方案 ...
- 手把手教你写LKM rookit! 之 杀不死的pid&root后门
......上一节,我们编写了一个基本的lkm模块,从功能上来说它还没有rootkit的特征,这次我们给它添加一点有意思的功能.我们让一个指定的进程杀不死, 曾经,想写一个谁也杀不死的进程,进程能捕捉 ...
- C++程序员笔试复习概要(一)
第8章 类和对象的创建 [内容提要] 类和对象 构造函数和析构函数 对象数组与对象指针 静态成员 友元 静态函数 虚函数 [重点与难点] 8.1 类和对象 8.1.1 类的定义 类实质上是用户 ...
- Android Studio 单刷《第一行代码》系列 07 —— Broadcast 广播
前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...
- idea14使用maven创建web工程
参考博客:http://geeksun.iteye.com/blog/2179658 http://www.bubuko.com/infodetail-387387.html ------------ ...