Milk Patterns
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 12972   Accepted: 5769
Case Time Limit: 2000MS

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
思路:用后缀数组求出lcp后,2分枚举L使得连续的lcp[i]>=L 的个数>=k-1;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=;
int buf[MAXN];
int sa[MAXN];
int rnk[MAXN];
int tmp[MAXN];
int lcp[MAXN];
int len,k;
int t; bool comp(int i,int j)
{
if(rnk[i]!=rnk[j]) return rnk[i]<rnk[j];
else
{
int ri=(i+k<=len)?rnk[i+k]:-;
int rj=(j+k<=len)?rnk[j+k]:-;
return ri<rj;
}
} void getsa()
{
memset(sa,,sizeof(sa));
memset(rnk,,sizeof(rnk));
memset(tmp,,sizeof(tmp)); for(int i=;i<len;i++)
{
sa[i]=i;
rnk[i]=buf[i];
}
sa[len]=len;
rnk[len]=-; for(k=;k<=len;k*=)
{
sort(sa,sa+len+,comp); tmp[sa[]]=;
for(int i=;i<=len;i++)
{
tmp[sa[i]]=tmp[sa[i-]]+(comp(sa[i-],sa[i])?:);
} for(int i=;i<=len;i++)
{
rnk[i]=tmp[i];
}
} } void getlcp()
{
getsa();
memset(rnk,,sizeof(rnk));
memset(lcp,,sizeof(lcp));
for(int i=;i<=len;i++)
{
rnk[sa[i]]=i;
} int h=;
lcp[]=h;
for(int i=;i<len;i++)
{
int j=sa[rnk[i]-];
if(h>) h--;
for(;i+h<len&&j+h<len;h++)
{
if(buf[i+h]!=buf[j+h]) break;
}
lcp[rnk[i]-]=h;
} } void debug()
{
for(int i=;i<=len;i++)
{
int l=sa[i];
if(l==len)
{
printf("0\n");
}
else
{
for(int j=sa[i];j<len;j++)
{
printf("%d ",buf[j]);
}
printf(" %d\n",lcp[i]);
}
} } bool judge(int l)
{
int cnt=;
for(int i=;i<len;i++)
{
if(lcp[i]>=l)//求前缀大于等于l的连续长度
{
cnt++;
}
else
cnt=;
if(cnt==t-) return true;
}
return false;
} void solve()
{ int l=,r=len;
int ans=;
while(l<=r)
{
int mid=(l+r)>>;
if(judge(mid))//2分枚举长度
{
ans=max(ans,mid);
l=mid+;
}
else r=mid-;
}
printf("%d\n",ans);
} int main()
{
while(scanf("%d%d",&len,&t)!=EOF)
{
for(int i=;i<len;i++)
scanf("%d",&buf[i]);
getlcp();
// debug()
solve();
}
return ;
}
												

POJ3261(后缀数组+2分枚举)的更多相关文章

  1. poj3261 后缀数组求重复k次可重叠的子串的最长长度

    Milk Patterns Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13669   Accepted: 6041 Ca ...

  2. Boring counting HDU - 3518 (后缀数组)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  3. HDU2459 后缀数组+RMQ

    题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...

  4. CF #244 D. Match & Catch 后缀数组

    题目链接:http://codeforces.com/problemset/problem/427/D 大意是寻找两个字符串中最短的公共子串,要求子串在两个串中都是唯一的. 造一个S#T的串,做后缀数 ...

  5. 后缀数组的第X种求法

    后缀自动机构造后缀数组. 因为有个SB题洛谷5115,它逼迫我学习后缀数组...(边分树合并是啥?). 一些定义:sa[i]表示字典序排第i的后缀是从哪里开始的.Rank[i]表示后缀i的排名.hei ...

  6. [bzoj2251][2010Beijing Wc]外星联络——后缀数组+暴力求解

    Brief Description 找到 01 串中所有重复出现次数大于 1 的子串.并按字典序输出他们的出现次数. Algorithm Design 求出后缀数组之后,枚举每一个后缀,对于每个后缀从 ...

  7. UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组

    题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...

  8. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  9. 【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1031 [题意] [题解] 后缀数组模板题; 把整个字符串扩大一倍. 即长度乘2 然后搞 ...

随机推荐

  1. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  2. [Hibernate开发之路](3)基础配置

    一 JDBC链接 通常你希望SessionFactory来为你创建和缓存(pool)JDBC连接. 假设你採用这样的方式, 仅仅须要例如以下例所看到的那样,打开一个Session: Session s ...

  3. PS 如何制作WIN7的玻璃化透明窗口效果

    1 绘制一个圆角矩形,并将不透明度设为16%以及添加投影效果   2 再次添加外发光效果   3 新建一个图层,再填充一下这个圆角矩形(可以填充为任意颜色,只要和别的颜色区分开来)   4 选中这个区 ...

  4. php如何读取ini文件

    很多时候,我们使用配置文件来读取配置,那么php如何使用ini文件呢? 代码如下: 例如将:数据库信息存到ini文件中,进行读取. <?php header('content-type:text ...

  5. 怎么设置MySQL就能让别人访问本机的数据库了?

    for all ips use below GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION; for particular ...

  6. python(20)- 列表生成式和生成器表达式练习Ⅱ

    题目一: 有两个列表,分别存放来老男孩报名学习linux和python课程的学生名字linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']python= ...

  7. iOS----FMDB---看这个可以解决大部分你遇到的问题

    SQLite (http://www.sqlite.org/docs.html) 是一个轻量级的关系数据库. iOS SDK很早就支持了SQLite,在使用时,只需要加入 libsqlite3.dyl ...

  8. javascript 高级编程系列 - 基本数据类型

    javascript中的基本数据类型包括: Undefined, Null, Boolean, Number, String 5种数据类型 1. Undefined 类型 (只有一个值 undefin ...

  9. Tomcat服务器改主页 & jeesite框架改首页

    Tomcat服务器改主页: 方法一: 把原来的 ROOT 目录清空: 发布你自己的项目到 ROOT 目录下: 发布程序 /webapps/ROOT/WEB-INF/web.xml 中需要有默认首页定义 ...

  10. JS实现搜索模糊匹配

      Js代码 <script type="text/javascript"> var websites = [["1231","账上1&q ...