POJ3261(后缀数组+2分枚举)
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
Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.
Output
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分枚举)的更多相关文章
- poj3261 后缀数组求重复k次可重叠的子串的最长长度
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 13669 Accepted: 6041 Ca ...
- Boring counting HDU - 3518 (后缀数组)
Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...
- HDU2459 后缀数组+RMQ
题目大意: 在原串中找到一个拥有连续相同子串最多的那个子串 比如dababababc中的abababab有4个连续的ab,是最多的 如果有同样多的输出字典序最小的那个 这里用后缀数组解决问题: 枚举连 ...
- CF #244 D. Match & Catch 后缀数组
题目链接:http://codeforces.com/problemset/problem/427/D 大意是寻找两个字符串中最短的公共子串,要求子串在两个串中都是唯一的. 造一个S#T的串,做后缀数 ...
- 后缀数组的第X种求法
后缀自动机构造后缀数组. 因为有个SB题洛谷5115,它逼迫我学习后缀数组...(边分树合并是啥?). 一些定义:sa[i]表示字典序排第i的后缀是从哪里开始的.Rank[i]表示后缀i的排名.hei ...
- [bzoj2251][2010Beijing Wc]外星联络——后缀数组+暴力求解
Brief Description 找到 01 串中所有重复出现次数大于 1 的子串.并按字典序输出他们的出现次数. Algorithm Design 求出后缀数组之后,枚举每一个后缀,对于每个后缀从 ...
- UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组
题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...
- POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串
题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS Memory Limit: 131072 ...
- 【BZOJ 1031】[JSOI2007]字符加密Cipher(后缀数组模板)
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1031 [题意] [题解] 后缀数组模板题; 把整个字符串扩大一倍. 即长度乘2 然后搞 ...
随机推荐
- 专訪阿里陶辉:大规模分布式系统、高性能server设计经验分享
http://www.csdn.net/article/2014-06-27/2820432 摘要:先后就职于在国内知名的互联网公司,眼下在阿里云弹性计算部门做架构设计与核心模块代码的编写,主要负责云 ...
- 读《疯狂Java讲义》笔记总结三
1.初始化块 实际上初始化块是一个假象,使用javac命令编译Java类后,该Java类中的初始化块会消失--初始化块中代码会被 "还原" 到每一个构造器中,且位于构造器全部代码的 ...
- 使用".."指定git提交范围与"..."指定git提交范围的区别
http://blog.csdn.net/hansel/article/details/8952967 使用".."(两个点)和"..."(三个点)都可以指定一 ...
- 算法排序-lowB三人组
冒泡排序思路: 选择排序思路: 插入排序思路: 小结: 详细代码解释看下一篇
- SecureCRT 7.0 如何自动记录日志
设置步骤如下: 1.打开SecureCRT ,在菜单里选择“选项”-->“全局选项” 2.然后选择“常规”--> “默认会话”--> “编辑默认设置” 3.然后选择“日志 ...
- git和github菜鸟使用步骤
刚刚在windows7下安装完git.奉上安装步骤. git安装 安装git程序.运行以下操作: 1. $ cd ~/.ssh //检查计算机ssh密钥 2.假设没有提示:No such fil ...
- cURL实现Get和Post
1.Get请求: //初始化 $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "http://www.jb51.n ...
- Leetcode 001-twosum
#Given an array of integers, return indices of the two numbers such that they add up to a specific t ...
- 1367: [Baltic2004]sequence
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MB Submit: 1090 Solved: 432 [Submit ...
- 负载均衡实现,一个域名对应多个IP地址
负载均衡实现,一个域名对应多个IP地址 - 宏宇 - 博客园 https://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.h ...