http://poj.org/problem?id=3261

题意:一个长度为n的串,要求最长的子串的长度且这个子串的出现次数不少于k次。(1<=n<=20000, 2<=k<=n)

#include <cstdio>
#include <algorithm>
using namespace std; const int N=20015;
void sort(int *x, int *y, int *sa, int n, int m) {
static int c[N], i;
for(i=0; i<m; ++i) c[i]=0;
for(i=0; i<n; ++i) c[x[y[i]]]++;
for(i=1; i<m; ++i) c[i]+=c[i-1];
for(i=n-1; i>=0; --i) sa[--c[x[y[i]]]]=y[i];
}
void hz(int *r, int *sa, int n, int m) {
static int t1[N], t2[N];
static int *x, *y, *t, j, i, p=0;
x=t1; y=t2;
for(i=0; i<n; ++i) x[i]=r[i], y[i]=i;
sort(x, y, sa, n, m);
for(j=1, p=1; p<n; j<<=1, m=p) {
p=0;
for(i=n-j; i<n; ++i) y[p++]=i;
for(i=0; i<n; ++i) if(sa[i]-j>=0) y[p++]=sa[i]-j;
sort(x, y, sa, n, m);
for(t=x, x=y, y=t, x[sa[0]]=0, p=1, i=1; i<n; ++i)
x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
}
void geth(int *a, int *sa, int *rank, int *h, int n) {
static int k, i, j; k=0;
for(i=1; i<=n; ++i) rank[sa[i]]=i;
for(i=1; i<=n; h[rank[i++]]=k)
for(k?--k:0, j=sa[rank[i]-1]; a[i+k]==a[j+k]; ++k);
}
const int oo=~0u>>2;
int sa[N], rank[N], h[N], n, a[N], b[N], K;
bool check(int k) {
int cnt=1;
for(int i=2; i<=n; ++i) {
if(h[i]>=k) {
++cnt;
if(cnt>=K) return 1;
}
else cnt=1;
}
if(cnt>=K) return 1;
return 0;
}
int mp[1000005];
int main() {
scanf("%d%d", &n, &K);
for(int i=1; i<=n; ++i) scanf("%d", &a[i]), b[i]=a[i];
sort(b+1, b+1+n);
int tot=unique(b+1, b+1+n)-b-1;
for(int i=1; i<=tot; ++i) mp[b[i]]=i;
for(int i=1; i<=n; ++i) a[i]=mp[a[i]];
hz(a, sa, n+1, 200);
geth(a, sa, rank, h, n);
int mid, l=0, r=n;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) l=mid+1;
else r=mid-1;
}
printf("%d\n", l-1);
return 0;
}

  


经典题...同样是分组height...将高度>=二分值的分在一组,然后判断是否有大于等于K个元素即可

【POJ】3261 Milk Patterns的更多相关文章

  1. POJ 3261 Milk Patterns 【后缀数组 最长可重叠子串】

    题目题目:http://poj.org/problem?id=3261 Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Subm ...

  2. POJ 3261 Milk Patterns (求可重叠的k次最长重复子串)+后缀数组模板

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7586   Accepted: 3448 Cas ...

  3. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  4. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  5. POJ 3261 Milk Patterns 可重复k次的最长重复子串

    Milk PatternsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 Description ...

  6. POJ 3261 Milk Patterns(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过 ...

  7. POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次

    Milk Patterns   Description Farmer John has noticed that the quality of milk given by his cows varie ...

  8. poj 3261 Milk Patterns(后缀数组)(k次的最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7938   Accepted: 3598 Cas ...

  9. POJ 3261 Milk Patterns (后缀数组,求可重叠的k次最长重复子串)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16742   Accepted: 7390 Ca ...

随机推荐

  1. Centos 7 安装LAMP环境

    一.安装Centos 官网下载Centos 7刻录成光盘后安装 二.安装apache yum install httpd #根据提示,输入Y安装即可成功安装 systemctl start httpd ...

  2. nyoj 20

    http://acm.nyist.net/JudgeOnline/message.php?msg=已提交&url=status.php%3Fpid%3D20&second=0 #inc ...

  3. [ruby on rails] 跟我学之(9)删除数据

    首先需要在index页加个删除链接,并提供一个删除的确认,用户确认删除时,直接删除数据. 修改views 修改 app/views/posts/index.html.erb,如下: <h1> ...

  4. 使用nginx的proxy_cache做网站缓存

    为什么要做web cache,我想大家最主要的是解决流量的压力.随着网站流量的提升,如果只是单台机器既处理静态文件,又处理动态脚本,显然效率很难上升,不能处理日益上涨的流量压力.与此同时某些网站的页面 ...

  5. mysql中limit与in不能同时使用的解决方式.

    mysql中limit与in不能同时使用的解决方式. 分类: MySQL2011-10-31 13:53 1277人阅读 评论(0) 收藏 举报 mysqlsubquery MySQL5.1中子查询是 ...

  6. global-local-static-object

    [本文链接] http://www.cnblogs.com/hellogiser/p/global-local-static-object.html [分析] 1.生命周期问题:static变量在固定 ...

  7. 【转】MySQL Temporary Table相关问题的探究

    本文转载自:http://itindex.net/detail/10901-mysql-temporary-table 问题的引入 让我们先来观察几条非常简单的MySQL语句: mysql> c ...

  8. Java for LeetCode 030 Substring with Concatenation of All Words【HARD】

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  9. POJ 3904 Sky Code (容斥原理)

    B - Sky Code Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  10. gdalwarp切割tif参数

    可以去gdal官网查询gdalwarp工具的参数,但是具体的还是不知道怎么写,例如内置数据类型-ot 和压缩-co参数. 这里有一个经过雁阵更可以使用的参数 gdalwarp -te lon1 lat ...