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. Hadoop2.x源码-编译剖析

    1.概述 最近,有小伙伴涉及到源码编译.然而,在编译期间也是遇到各种坑,在求助于搜索引擎,技术博客,也是难以解决自身所遇到的问题.笔者在被询问多次的情况下,今天打算为大家来写一篇文章来剖析下编译的细节 ...

  2. iOS开发 - AVPlayer实现流音频边播边存

    边播边下有三套左右实现思路,本文使用AVPlayer + AVURLAsset实现. 概述 1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为 ...

  3. Castle.ActiveRecord (V3.0.0.130)

    为项目添加 Castle.ActiveRecord 的引用: 安装成功后,查看项目的引用如图: 配置文件 App.Config (MySQL) <?xml version="1.0&q ...

  4. swift 方法

    swift的类,结构体,枚举中都可以定义方法. 1:实例方法.类似于类成员方法 1.1实例方法是属于类,结构体,枚举的实例的方法.通过其实例访问. class CShow{ func testShow ...

  5. 【Android】如何写一个JsBridge

    JsBridge 简介 Android JsBridge 就是用来在 Android app的原生 java 代码与 javascript 代码中架设通信(调用)桥梁的辅助工具. 原文地址点这里 gi ...

  6. DotNetBar RibbonControl控件office2007风格

    在使用DotNetBar RibbonControl控件的时候如果想吧效果做成下图这种效果 把主窗体继承Office2007RibbonForm 然后要删除删除styleManager1 才会出现上图 ...

  7. POJ 2082 Terrible Sets

    Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2747   Accepted: 1389 Des ...

  8. 理解RxJava:(四)Reactive Android

    在前三部分,我在通用层面介绍了RxJava的工作原理.但是作为一个Android开发者,如何在工作中使用它呢?下面是一些给Android开发者的RxJava的具体应用. RxAndroid RxAnd ...

  9. textView字体颜色根据不同状态显示不同颜色

    XML file saved at res/color/button_text.xml: <?xml version="1.0" encoding="utf-8&q ...

  10. Qt5 从头学(1)-- 环境

    对我来说MFC太过麻烦了,同样是桌面开发工具,Qt就完全不一样了.Qt使用C++语言可以轻松实现"一次编写,到处编译"的跨平台性能,并且可以做出很多炫酷的界面效果.目前支持几乎所有 ...