poj3261Milk Patterns 后缀数组
题目地址:http://poj.org/problem?id=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
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 思路:直接上后缀数组模板,跑个height数组。不过此时要注意两点:
1.每个数的范围在0~1e6,所以要先离散化。不过此题数据较水,你可以不需要离散化,直接把字符最大值设为1e5就可以a了。不过直接开个1e6的数组应该也没事吧(没试过,待证实)
2.这个数可能是0,所以要么把每个读取的数先+1,或者把ss[n]设为-1;
然后二分答案,对height数组分组看符不符合答案。
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int N = +;
int wa[N], wb[N], ws[N], wv[N];
int rank[N], height[N];
int sa[N],ss[N],n,k;
bool cmp(int r[], int a, int b, int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
} void da(int r[], int sa[], int n, int m) {
int i, j, p, *x = wa, *y = wb;
for (i = ; i < m; ++i) ws[i] = ;
for (i = ; i < n; ++i) ws[x[i]=r[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[x[i]]] = i;
for (j = , p = ; p < n; j *= , m = p) {
for (p = , i = n - j; i < n; ++i) y[p++] = i;
for (i = ; i < n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = ; i < n; ++i) wv[i] = x[y[i]];
for (i = ; i < m; ++i) ws[i] = ;
for (i = ; i < n; ++i) ws[wv[i]]++;
for (i = ; i < m; ++i) ws[i] += ws[i-];
for (i = n-; i >= ; --i) sa[--ws[wv[i]]] = y[i];
for (swap(x, y), p = , x[sa[]] = , i = ; i < n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p- : p++;
}
} void calheight(int r[], int sa[], int n) {
int i, j, k = ;
for (i = ; i <= n; ++i) rank[sa[i]] = i;
for (i = ; i < n; height[rank[i++]] = k)
for (k?k--:, j = sa[rank[i]-]; r[i+k] == r[j+k]; k++);
}
int check(int len)
{
int i=,cnt=;
while()
{
while(i<=n && height[i]>=len)
cnt++,i++;
if(cnt+>=k)return ;
if(i>=n)return ;
while(i <=n &&height[i]<len)
i++;
cnt=;
}
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<n;i++)
scanf("%d",&ss[i]),ss[i]++;
ss[n]=;
da(ss,sa,n+,N);
calheight(ss,sa,n);
int l=,r=n,ans=,mid;
while(l<=r)
{
mid=(l+r)/;
if(check(mid))
l=mid+,ans=mid;
else
r=mid-;
}
printf("%d\n",ans);
return ;
}
poj3261Milk Patterns 后缀数组的更多相关文章
- POJ-3261-Milk Patterns(后缀数组)
题意: 给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 分析: 先二分答案,然后将后缀分成若干组. 不同的是,这里要判断的是有没有一个组的后缀个数不小于k. 如果有,那么存在k ...
- [USACO06FEC]Milk Patterns --- 后缀数组
[USACO06FEC]Milk Patterns 题目描述: Farmer John has noticed that the quality of milk given by his cows v ...
- POJ3261 Milks patterns(后缀数组)
Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...
- Poj 3261 Milk Patterns(后缀数组+二分答案)
Milk Patterns Case Time Limit: 2000MS Description Farmer John has noticed that the quality of milk g ...
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
- POJ 3261 Milk Patterns(后缀数组+单调队列)
题意 找出出现k次的可重叠的最长子串的长度 题解 用后缀数组. 然后求出heigth数组. 跑单调队列就行了.找出每k个数中最小的数的最大值.就是个滑动窗口啊 (不知道为什么有人写二分,其实写啥都差不 ...
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
随机推荐
- 安装loadrunner时出现”命令行选项语法错误键入命令 \?获得帮助“的解决方法
安装LR11 时,安装Microsoft Visual c++2005 sp1运行时组件,就会提示命令行选项语法错误,键入“命令/?”可获取帮肋信息1.进入loadrunner-11\Addition ...
- MathType可以编辑带圈乘号吗
在数学中有很多符号,可能这些符号我们用得上,也有些符号我们很少用,甚至用不上,但是我们用不上,不代表不存在这个符号,也不代表别人用不上,只是各自所涉及到的知识领域不一样而已.而对于加减乘除运算,几乎每 ...
- (转)使用 python Matplotlib 库绘图
运行一个简单的程序例子: import matplotlib.pyplot as plt plt.plot([1,2,3]) plt.ylabel('some numbers') plt.show() ...
- Oracle数据库不能创建表空间及表中文乱码问题
1.不能创建表空间问题 datafile为表空间的存放位置,没有将表空间存放路径指定为orcl数据库时,创建表空间出错如下 查看自己的Oracle安装位置,我的Oracle10g安装在虚拟XP系统中, ...
- UIImagePickerController在UIPopoverController中 旋屏问题
1弧度=180/π度1度=π/180弧度今天遇到了 一个问题.UIImagePickerController在UIPopoverController中 旋屏问题. 在查找了许多资料后方知,此乃iOS系 ...
- cannot be cast to javax.servletFilter
java.lang.ClassCastException: org.springframework.web.filter.CharacterEncodingFilter cannot be cast ...
- HDU 3388 Coprime(容斥原理+二分)
Coprime Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- plotplayer声道设置原声
1.打开plotplayer.根据按键F5,选择声音——>语言/同步/其他-流选择 2.重启plotplayer
- <td></td>之间的
今天开发中遇到了一个很丢脸的事:我把下图中别人写的 给去掉了,我觉得这个很多余,结果被在大神们痛斥了一顿.因为去掉这个 会导致td之间没有数据时td的边框不显示的问题,所以几下这个惨痛的教训,以自警. ...
- 史上最易懂的大数据 OTO
史上最易懂的大数据 OTO http://network.51cto.com/art/201503/467068.htm 终于有人把O2O.C2C.B2B.B2C的区别讲透了 http://tech. ...