题面

vjudge

Sol

二分答案+分组,判断有没有一个组的后缀个数不小于 k

做法

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(20010); IL ll Read(){
RG char c = getchar(); RG ll x = 0, z = 1;
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
} int n, a[_], sa[_], rk[_], y[_], h[_], height[_], t[1000010], ans, K, mx; IL bool Cmp(RG int i, RG int j, RG int k){ return y[i] == y[j] && y[i + k] == y[j + k]; } IL void Sort(){
RG int m = mx;
for(RG int i = 1; i <= n; ++i) ++t[rk[i] = a[i]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[i]]--] = i;
for(RG int k = 1; k <= n; k <<= 1){
RG int l = 0;
for(RG int i = n - k + 1; i <= n; ++i) y[++l] = i;
for(RG int i = 1; i <= n; ++i) if(sa[i] > k) y[++l] = sa[i] - k;
for(RG int i = 0; i <= m; ++i) t[i] = 0;
for(RG int i = 1; i <= n; ++i) ++t[rk[y[i]]];
for(RG int i = 1; i <= m; ++i) t[i] += t[i - 1];
for(RG int i = n; i; --i) sa[t[rk[y[i]]]--] = y[i];
swap(rk, y); rk[sa[1]] = l = 1;
for(RG int i = 2; i <= n; ++i) rk[sa[i]] = Cmp(sa[i - 1], sa[i], k) ? l : ++l;
if(l >= n) break; m = l;
}
for(RG int i = 1; i <= n; ++i){
h[i] = max(0, h[i - 1] - 1);
if(rk[i] == 1) continue;
while(a[i + h[i]] == a[sa[rk[i] - 1] + h[i]]) ++h[i];
}
for(RG int i = 1; i <= n; ++i) height[i] = h[sa[i]];
} IL bool Check(RG int x){
RG int cnt = 0;
for(RG int i = 2; i <= n; ++i){
if(height[i] < x) cnt = 0;
else cnt++;
if(cnt >= K - 1) return 1;
}
return 0;
} int main(RG int argc, RG char* argv[]){
n = Read(); K = Read();
for(RG int i = 1; i <= n; ++i) a[i] = Read(), mx = max(a[i], mx);
Sort();
RG int l = 0, r = n;
while(l <= r){
RG int mid = (l + r) >> 1;
if(Check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
return 0;
}

POJ3261:Milk Patterns的更多相关文章

  1. POJ3261:Milk Patterns——题解

    http://poj.org/problem?id=3261 给一个序列,求至少出现 k 次的最长重复子串,这 k 个子串可以重叠. 论文题+傻逼题. 上一道题(POJ1743)会做即可. 还是二分长 ...

  2. 【poj3261】 Milk Patterns

    http://poj.org/problem?id=3261 (题目链接) 题意 给出n个数和k,求在给出的数中,最长的出现至少k次的可重叠子串. solution 后缀数组论文题,感觉分组思想可能会 ...

  3. 【BZOJ1717&POJ3261】Milk Patterns(后缀数组,二分)

    题意:求字符串的可重叠的k次最长重复子串 n<=20000 a[i]<=1000000 思路:后缀数组+二分答案x,根据height分组,每组之间的height>=x 因为可以重叠, ...

  4. POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串

    题目链接:https://vjudge.net/problem/POJ-3261 Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Tot ...

  5. poj3261 -- Milk Patterns

                                                                        Milk Patterns Time Limit: 5000MS ...

  6. POJ-3261 Milk Patterns,后缀数组+二分。。

                                                        Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...

  7. Milk Patterns poj3261(后缀数组)

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9274   Accepted: 4173 Cas ...

  8. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

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

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

随机推荐

  1. Nginx与Tomcat/PHP架构优化的技术分享

    PHP性能优化 一般我们是在/usr/local/php5/etc/php-fpm.conf这个文件里面进行相应的配置. 1)       如果设置成static,php-fpm进程数自始至终都是pm ...

  2. linux下利用shell脚本实现添加crontab任务

    本来直接用crontab -e 就可以打开vim,输入要执行的任务保存退出就可以添加任务直接启动运行了.但是今天组长说能不能写个shell不用打开vi就能添加到crontab的. 最先想到的是怎么在s ...

  3. 提高SQL查询效率

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  4. Linux 安装配置 FTP 服务 (vsftpd)

    1. 安装 vsftpd yum install vsftpd -y 2. 创建用户 record adduser -s /bin/nologin -d /var/RecordFile/ record ...

  5. ajaxfileupload批量上传文件+图片尺寸限制

    1.首先展示ajaxfileupload代码,在这里修改为批量上传 //ajaxfileupload不展示全部代码,这是修改前与修改后代码对比,目的是上传多个文件 createUploadForm: ...

  6. HDU 3001(状态压缩dp)

    状态压缩dp的第一题! 题意:Mr ACMer想要进行一次旅行,他决定访问n座城市.Mr ACMer 可以从任意城市出发,必须访问所有的城市至少一次,并且任何一个城市访问的次数不能超过2次.n座城市间 ...

  7. iOS isa指针

    在Objective-C中,任何类的定义都是对象.类和类的实例没有任何本质上的区别.任何对象都有isa指针. isa:是一个Class 类型的指针. 每个实例对象有个isa的指针,它指向对象的类,而C ...

  8. WAMP环境搭建过程中遇到的种种问题

    1,可以选择已经打包好的继承安装包,通常包含apache,mysql,PHP,phpMyadmin.如appserv和wamp. 2,自己分别安装. 第一步:安装mysql,注意设置root对应的密码 ...

  9. LeetCode第七天

    ==数组 Medium== 40.(162)Find Peak Element JAVA //斜率思想,二分法 class Solution { public int findPeakElement( ...

  10. 如何使用 VS生成动态库?

    如何使用 VS生成动态库? //.cpp 文件默认定义了 __cplusplus 宏 #ifdef __cplusplus extern "C"{ #endif xxx #ifde ...