POJ  3261

Description

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can't predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns -- 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

Input

Line 1: Two space-separated integers: N and K
Lines 2.. N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

Output

Line 1: One integer, the length of the longest pattern which occurs at least K times

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

题意: 给了N和K,接下来有N个数输入,N<=20000,每个数小于1000,000,求一个最长的子串,这个子串在这个串中至少出现K次,K>=2,保证至少存在一个串符合;

思路:我们可以通过二分子串的长度len来做,这时就将题目变成了是否存在重复次数至少为K次且长度不小len的字符串。首先我们可以把相邻的所有不小于len的height[]看成一组,这组内有多少个字符串,就相当于有多少个长度至少为len的重复的子串。之所以可以这么做,是因为排名第i的字符串和排名第j的字符串的最长公共前缀等于height[i],height[i+1],...,height[j]中的最小值,所以把所有不小于len的height[]看成一组就保证了组内任意两个字符串的最长公共前缀都至少为k,且长度为k的前缀是每个字符串共有的,因此这组内有多少个字符串,就相当于有多少个长度至少为k的重复的子串(任意一个子串都是某个后缀的前缀);
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#define rep(i,n) for(int i = 0;i < n; i++)
using namespace std;
const int size=,INF=<<;
int rk[size],sa[size],height[size],w[size],wa[size],res[size];
int N,K;
void getSa (int len,int up) {
int *k = rk,*id = height,*r = res, *cnt = wa;
rep(i,up) cnt[i] = ;
rep(i,len) cnt[k[i] = w[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[k[i]]] = i;
}
int d = ,p = ;
while(p < len){
for(int i = len - d; i < len; i++) id[p++] = i;
rep(i,len) if(sa[i] >= d) id[p++] = sa[i] - d;
rep(i,len) r[i] = k[id[i]];
rep(i,up) cnt[i] = ;
rep(i,len) cnt[r[i]]++;
rep(i,up) cnt[i+] += cnt[i];
for(int i = len - ; i >= ; i--) {
sa[--cnt[r[i]]] = id[i];
}
swap(k,r);
p = ;
k[sa[]] = p++;
rep(i,len-) {
if(sa[i]+d < len && sa[i+]+d <len &&r[sa[i]] == r[sa[i+]]&& r[sa[i]+d] == r[sa[i+]+d])
k[sa[i+]] = p - ;
else k[sa[i+]] = p++;
}
if(p >= len) return ;
d *= ,up = p, p = ;
}
} void getHeight(int len) {
rep(i,len) rk[sa[i]] = i;
height[] = ;
for(int i = ,p = ; i < len - ; i++) {
int j = sa[rk[i]-];
while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
p++;
}
height[rk[i]] = p;
p = max(,p - );
}
} int getSuffix(int s[]) {
int len =N,up = ;
for(int i = ; i < len; i++) {
w[i] = s[i];
up = max(up,w[i]);
}
w[len++] = ;
getSa(len,up+);
getHeight(len);
return len;
}
void solve()///二分;
{
int i,j,k,cnt,ans,mid,min,max;
min=,max=N;
for(;;)
{
mid = (max + min) / ;
if(mid==min)
break;
ans=cnt=;
for(i=;i<=N;i++)
{///计算连续的height[];
if(height[i]<mid)
{
if(cnt>ans)
ans=cnt;
cnt=;
}
else
{
if(!cnt)
cnt=;
else
++cnt;
}
}
if(cnt > ans)
ans = cnt;
if(ans >= K)
min = mid;
else
max = mid;
}
printf("%d\n", mid);
}
map<int,int>q;
int main()
{
int s[size],a[size];
while(scanf("%d%d",&N,&K)!=EOF)
{
for(int i=;i<N;i++)
{
scanf("%d",&s[i]);
a[i]=s[i];
}
sort(a,a+N);
int pre=,tot=;///离散化处理;
for(int i=;i<N;i++)
{
if(a[i]==pre)
{
a[i]=tot;
q[pre]=tot;
}
else
{
pre=a[i];
a[i]=++tot;
q[pre]=tot;
}
}
for(int i=;i<N;i++)
{
s[i]=q[s[i]];
}
getSuffix(s);
solve();
}
}

后缀数组---Milk Patterns的更多相关文章

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

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

  2. 【BZOJ-1717】Milk Patterns产奶的模式 后缀数组

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

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

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

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

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

  5. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )

    二分答案m, 后缀数组求出height数组后分组来判断. ------------------------------------------------------------ #include&l ...

  6. BZOJ_1717_[Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组

    BZOJ_1717_[Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他 ...

  7. [USACO06FEC]Milk Patterns --- 后缀数组

    [USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...

  8. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式_后缀数组_二分答案

    Milk Patterns 产奶的模式 bzoj-1717 Usaco-2006 Dec 题目大意:给定一个字符串,求最长的至少出现了$k$次的子串长度. 注释:$1\le n\le 2\cdot 1 ...

  9. Poj 3261 Milk Patterns(后缀数组+二分答案)

    Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...

随机推荐

  1. ORA-01654 索引 无法通过 表空间扩展

    "ORA-01654: 索引VGSM.AUDIT_DATA_I无法通过8192(在表空间KLDB中)扩展"   1.有可能是索引表空间不够 select sum(bytes/102 ...

  2. Jenkins 安装或更新插件失败

    试试这个插件网址是否可以在网页中打开 http://mirror.xmission.com/jenkins/updates/current/update-center.json   如可以,把这个网址 ...

  3. 在Win8.1系统下如何安装运行SQL Server 2005 (以及安装SQL Server 2005 Express打补丁)

    在Win8.1系统下SQL Server 2005 安装失败怎么办? 需要替换两个文件及打sqlserver sp4补丁. 以下是操作过程. 按正常情况,在Win8/Win8.1系统下安装微软的SQL ...

  4. centos 7.0 下触发器乱码的解决

    [root@iZ62jhlmmZ ~]# uname -a Linux iZ62jhlmsymZ 3.10.0-123.13.2.el7.x86_64 #1 SMP Thu Dec 18 14:09: ...

  5. 【工作代码】复杂 JSON 值替换处理

    总结下最近的工作遇到的点:入参复杂 JSON 层层嵌套,Java 怎么优雅的处理. 一.关于 JSON JSON 是类似 XML 用于存储和交互文本信息.但优于 XML ,其更小,更快,更易懂和解析. ...

  6. 用Jekyll在github上写博客——《搭建一个免费的,无限流量的Blog》的注脚

    本来打算买域名,买空间,用wordpress写博客的.后来问了一个师兄,他说他是用github的空间,用Jekyll写博客,说很多人都这么做.于是我就研究了一下. 比较有价值的文章有这么几篇: htt ...

  7. Contractive Auto-Encoder

    本博客已经迁往http://www.kemaswill.com/, 博客园这边也会继续更新, 欢迎关注~ Contractive Autoencoder(CAE)是Bengio等人在2011年提出的一 ...

  8. Auto CAD 2013的故障解决方法

    一.问题的提出 Auto CAD 2013在使用过程中出现了错误:“安全系统(软件锁许可管理器) 不起作用或未正确安装.” 二.问题的分析 网络上很多地方转载了这么一个方法: 1) 启动Windows ...

  9. 对依赖倒置原则(DIP)及Ioc、DI、Ioc容器的一些理解

    1.概述 所谓依赖倒置原则(Dependence Inversion Principle)就是要依赖于抽象,不要依赖于具体.简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模 ...

  10. android手势创建及识别

    使用一些浏览器或者输入法应用时会有一些手势操作,还可以自定义手势.这些神奇的操作是怎么做的呢?这一篇重点记录手势的识别和创建.这篇的内容使用到的是android.gesture包,具体的例子参考的是S ...