后缀数组的应用。和男人八题那个后缀数组差不多。

 /* 2890 */
#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 maxn = ;
int a[maxn], b[maxn], aa[maxn];
int pos[maxn], pn;
int rrank[maxn], height[maxn], sa[maxn];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
int n, k, at; 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[wv[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, i=,p=,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[i+k]==r[j+k]; ++k);
} bool judge(int bound) {
int i = ;
int pn = ; while (i <= n) {
if (height[i] < bound) {
at = sa[i-];
sort(pos, pos+pn);
int kk = , p = pos[];
rep(j, , pn) {
if (pos[j]-p >= bound) {
p = pos[j];
++kk;
}
} if (kk >= k)
return true;
pn = ;
pos[pn++] = sa[i++];
} else {
pos[pn++] = sa[i++];
}
}
at = sa[n-];
sort(pos, pos+pn);
int kk = , p = pos[];
rep(j, , pn) {
if (pos[j]-p >= bound) {
p = pos[j];
++kk;
}
} if (kk >= k)
return true; return false;
} 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() {
sort(b, b+n);
int nn = unique(b, b+n) - b; rep(i, , n)
aa[i] = lower_bound(b, b+nn, a[i]) - b + ;
aa[n] = ; da(aa, sa, n+, nn+);
calheight(aa, sa, n); #ifndef ONLINE_JUDGE
// printSa(n);
// printHeight(n);
#endif int l = , r = n, mid;
int ans = , fr; while (l <= r) {
mid = (l + r) >> ;
if (judge(mid)) {
ans = mid;
fr = at;
l = mid + ;
} else {
r = mid - ;
}
} printf("%d\n", ans);
rep(i, , ans)
printf("%d\n", b[aa[fr+i]-]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &k);
rep(i, , n) {
scanf("%d", &a[i]);
b[i] = a[i];
}
solve();
if (t)
putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】2890 Longest Repeated subsequence的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 【leetcode】300.Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

  3. 【Lintcode】077.Longest Common Subsequence

    题目: Given two strings, find the longest common subsequence (LCS). Your code should return the length ...

  4. 【Lintcode】076.Longest Increasing Subsequence

    题目: Given a sequence of integers, find the longest increasing subsequence (LIS). You code should ret ...

  5. 【Leetcode_easy】594. Longest Harmonious Subsequence

    problem 594. Longest Harmonious Subsequence 最长和谐子序列 题意: 可以对数组进行排序,那么实际上只要找出来相差为1的两个数的总共出现个数就是一个和谐子序列 ...

  6. 【leetcode】521. Longest Uncommon Subsequence I

    problem 521. Longest Uncommon Subsequence I 最长非共同子序列之一 题意: 两个字符串的情况很少,如果两个字符串相等,那么一定没有非共同子序列,反之,如果两个 ...

  7. 【leetcode】1218. Longest Arithmetic Subsequence of Given Difference

    题目如下: Given an integer array arr and an integer difference, return the length of the longest subsequ ...

  8. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  9. 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...

随机推荐

  1. 粗谈Android中的对齐

    在谈这个之前先啰嗦几个概念. 基线:书写英语单词时为了规范书写会设有四条线,从上至下第三条就是基线.基线对齐主要是为了两个控件中显示的英文单词的基线对齐,如下所示: Start:在看API的时候经常会 ...

  2. vhost设定

    vhost设定   http.conf <Directory /> AllowOverride none #Require all denied </Directory>   ...

  3. shell脚本编程之for语句、if语句使用介绍

    介绍了shell脚本编程之for语句.if语句的使用方法. 上部: 面向过程: 顺序执行 选择执行: if, case 循环执行: for, while, until 一.for语句 格式:      ...

  4. 解决BLOB/TEXT column can't have a default value query问题

    Create table的时候,报错BLOB/TEXT column 'xxxxxx( 表名称)' can't have a default value query ,意思是TEXT类型的表字段不能够 ...

  5. PHP的$_SERVER['HTTP_HOST']获取服务器地址功能详解,$_SERVER['HTTP_X_FORWARDED_HOST']

    uchome的index文件中的二级域名功能判断,使用了php的$_SERVER['HTTP_HOST'],开始对这个不是很了解,所以百度了一下,发现一篇帖子有点意思,转发过来做个记录. 在php中, ...

  6. php校验

    //校验function filters($grams){    if(get_magic_quotes_gpc()) {        $resgram = trim($grams);       ...

  7. Flashback删除(闪回删除)

    oracle 9i以前,当drop一个表时,到该表的所有引用都会从数据字典中删除. oracle 10g及以后,当drop一个表时,数据库根本没有删除表,而只是重命名了并放入了回收站.即当发出drop ...

  8. drag drop小游戏

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. iOS百度地图路径规划和POI检索详细总结-b

    路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj.png 如上图所示,在infoplist里 ...

  10. linux 下安装JDK1.7

    安装JDK1.7 1. 打开网址http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u5-downloads-1591156.ht ...