Description

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。

Input

* Line 1: 两个整数 N,K。

* Lines 2..N+1: 每行一个整数表示当天的质量值。

Output

* Line 1: 一个整数:N天中最长的出现了至少K次的模式的长度

Sample Input

8 2
1
2
3
2
3
2
3
1

Sample Output

4

题解

  求出相邻lcp后,二分长度,判断相邻的是不是连续超过k个即可。

  为什么这样是对的。

  因为两串相同的最长超过k的长度,那么绝对是相邻lcp超过k次对吧,脑补。

 #include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<iostream> #define N 20007
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if (ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,k;
int s[N],sa[N],rk[N*],A[N];
int a[N],b[N],cnta[N],cntb[N],tsa[N],height[N]; void Get_SA()
{
for (int i=;i<=n;i++)cnta[i]=;
for (int i=;i<=n;i++)cnta[s[i]]++;
for (int i=;i<=n;i++)cnta[i]+=cnta[i-];
for (int i=n;i>=;i--)sa[cnta[s[i]]--]=i;
rk[sa[]]=;
for (int i=;i<=n;i++)rk[sa[i]]=rk[sa[i-]]+(s[sa[i]]!=s[sa[i-]]);
for (int i=;rk[sa[n]]!=n;i<<=)
{
for (int j=;j<=n;j++)a[j]=rk[j],b[j]=rk[j+i];
for (int j=;j<=n;j++)cnta[j]=cntb[j]=;
for (int j=;j<=n;j++)cnta[a[j]]++,cntb[b[j]]++;
for (int j=;j<=n;j++)cnta[j]+=cnta[j-],cntb[j]+=cntb[j-];
for (int j=n;j>=;j--)tsa[cntb[b[j]]--]=j;
for (int j=n;j>=;j--)sa[cnta[a[tsa[j]]]--]=tsa[j];
rk[sa[]]=;
for (int j=;j<=n;j++)
rk[sa[j]]=rk[sa[j-]]+(a[sa[j]]!=a[sa[j-]]||b[sa[j]]!=b[sa[j-]]);
}
}
void Get_Height()
{
int len=;
for (int i=;i<=n;i++)
{
if (len) len--;
while(s[i+len]==s[sa[rk[i]-]+len])len++;
height[rk[i]]=len;
}
}
bool check(int x)
{
int tot=;
for (int i=;i<=n;i++)
{
if (height[i]>=x)tot++;else tot=;
if (tot>=k-)return ;
}
return ;
}
int mid_find(int l,int r)
{
while(l<r)
{
int mid=(l+r+)>>;
if (check(mid))l=mid;
else r=mid-;
}
return l;
}
int main()
{
n=read(),k=read();
for (int i=;i<=n;i++) A[i]=s[i]=read();
sort(A+,A+n+);
int cnt=n;cnt=unique(A+,A+cnt+)-A-;
for (int i=;i<=n;i++)
s[i]=lower_bound(A+,A+cnt+,s[i])-A;
Get_SA();
Get_Height();
/*for (int i=1;i<=n;i++)cout<<height[i]<<" ";
cout<<endl;*/
printf("%d\n",mid_find(,n));
}

【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 SA+二分的更多相关文章

  1. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...

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

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

  3. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+二分)

    /* 求可重叠的至少重复K次的最长字串 以1为下标起点,因为a[i]最大到1000000,所以要先离散一下 二分长度len 然后O(n)检验 后看h[i]是否有连续的一段h[i]大于len的,并且h[ ...

  4. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)

    以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...

  5. [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式——后缀数组

    Brief Description 给定一个字符串,求至少出现k次的最长重复子串. Algorithm Design 先二分答案,然后将后缀分成若干组.判断有没有一个组的后缀个数不小于k.如果有,那么 ...

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

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

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

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

  8. BZOJ#1717:[Usaco2006 Dec]Milk Patterns 产奶的模式(后缀数组+单调队列)

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Description 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的 ...

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

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

随机推荐

  1. Problem O: 国家排序

    Problem O: 国家排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 405  Solved: 253[Submit][Status][Web ...

  2. MySQL-08 MySQL8.0新特性

    性能 MySQL 8.0 在一定的用户访问条件下,速度要比 MySQL 5.7 快 2 倍.MySQL 8.0 在以下方面带来了更好的性能:读/写工作负载.IO 密集型工作负载.以及高竞争(" ...

  3. ssh整合思想 Spring与Hibernate和Struts2的action整合 调用action添加数据库 使用HibernateTemplate的save(entity)方法 update delete get 等方法crud操作

    UserAction类代码: package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.sw ...

  4. 修改broadcom 4322无线网卡ID教程,不再显示第三方无线网卡

    本帖最后由 hellokingabc 于 2016-1-11 03:07 编辑 黑苹果已经基本完美,但是无线网卡总是出现问题,经常断网,经过搜索,原因在于无线网卡在OSX系统下显示为第三方无线网卡,只 ...

  5. 27. Remove Element@python

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  6. Falsy Bouncer-freecodecamp算法题目

    Falsy Bouncer(过滤数组假值) 要求 删除数组中的所有假值.(在JavaScript中,假值有false.null.0."".undefined 和 NaN.) 思路 ...

  7. WCF_基础学习

    1.https://www.cnblogs.com/swjian/p/8126202.html 2.https://www.cnblogs.com/dotnet261010/p/7407444.htm ...

  8. python爬虫用到的一些东西

    原装requests >>> import requests >>> response = requests.get('http://www.baidu.com') ...

  9. 【Shiro】调用doGetAuthenticationInfo进行认证成功之后,isAuthenticated是false的问题。

    使用@Configuration配置shiro无状态登录时出现的问题,在subject.login之后当前线程重新绑定了一个假定subject,isAuthenticated. 这里自定义的访问拦截器 ...

  10. LeetCode(189) Rotate Array

    题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...