POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-3261
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 17157 | Accepted: 7592 | |
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
Source
题意:
给出一个字符串(数字串),问至少出现了k次并且可以重叠的最长子串的长度。
题解:
1.POJ1743 Musical Theme此题的加强版。
2.此题要求至少出现了k次,那么相应地在每一组内统计:是否存在一个长度不小于mid(二分时的mid),并且出现了至少k次的公共前缀。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <map>
- #include <string>
- #include <set>
- using namespace std;
- typedef long long LL;
- const double EPS = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e5;
- const int MAXN = 1e6+;
- bool cmp(int *r, int a, int b, int l)
- {
- return r[a]==r[b] && r[a+l]==r[b+l];
- }
- int r[MAXN], sa[MAXN], Rank[MAXN], height[MAXN];
- int t1[MAXN], t2[MAXN], c[MAXN];
- void DA(int str[], int sa[], int Rank[], int height[], int n, int m)
- {
- n++;
- int i, j, p, *x = t1, *y = t2;
- for(i = ; i<m; i++) c[i] = ;
- for(i = ; i<n; i++) c[x[i] = str[i]]++;
- for(i = ; i<m; i++) c[i] += c[i-];
- for(i = n-; i>=; i--) sa[--c[x[i]]] = i;
- for(j = ; j<=n; j <<= )
- {
- p = ;
- for(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<m; i++) c[i] = ;
- for(i = ; i<n; i++) c[x[y[i]]]++;
- for(i = ; i<m; i++) c[i] += c[i-];
- for(i = n-; i>=; i--) sa[--c[x[y[i]]]] = y[i];
- swap(x, y);
- p = ; x[sa[]] = ;
- for(i = ; i<n; i++)
- x[sa[i]] = cmp(y, sa[i-], sa[i], j)?p-:p++;
- if(p>=n) break;
- m = p;
- }
- int k = ;
- n--;
- for(i = ; i<=n; i++) Rank[sa[i]] = i;
- for(i = ; i<n; i++)
- {
- if(k) k--;
- j = sa[Rank[i]-];
- while(str[i+k]==str[j+k]) k++;
- height[Rank[i]] = k;
- }
- }
- bool test(int mid, int n, int k)
- {
- int cnt = ;
- for(int i = ; i<=n; i++)
- {
- if(height[i]<mid)
- cnt = ;
- else
- {
- cnt++;
- if(cnt==k) return true;
- }
- }
- return false;
- }
- int main()
- {
- int n, k;
- while(scanf("%d%d",&n,&k)!=EOF)
- {
- for(int i = ; i<n; i++) scanf("%d", &r[i]);
- r[n] = ;
- DA(r, sa, Rank, height, n, 1e6+);
- int l = , r = n/;
- while(l<=r)
- {
- int mid = (l+r)>>;
- if(test(mid, n, k))
- l = mid + ;
- else
- r = mid - ;
- }
- printf("%d\n", r);
- }
- }
POJ3261 Milk Patterns —— 后缀数组 出现k次且可重叠的最长子串的更多相关文章
- poj3261 Milk Patterns 后缀数组求可重叠的k次最长重复子串
题目链接:http://poj.org/problem?id=3261 思路: 后缀数组的很好的一道入门题目 先利用模板求出sa数组和height数组 然后二分答案(即对于可能出现的重复长度进行二分) ...
- POJ-3261 Milk Patterns,后缀数组+二分。。
Milk Patterns 题意:求可重叠的至少重复出现k次的最长的字串长. 这题的做法和上一题 ...
- POJ 3261 Milk Patterns ( 后缀数组 && 出现k次最长可重叠子串长度 )
题意 : 给出一个长度为 N 的序列,再给出一个 K 要求求出出现了至少 K 次的最长可重叠子串的长度 分析 : 后缀数组套路题,思路是二分长度再对于每一个长度进行判断,判断过程就是对于 Height ...
- POJ 3261 Milk Patterns(后缀数组+单调队列)
题意 找出出现k次的可重叠的最长子串的长度 题解 用后缀数组. 然后求出heigth数组. 跑单调队列就行了.找出每k个数中最小的数的最大值.就是个滑动窗口啊 (不知道为什么有人写二分,其实写啥都差不 ...
- POJ 3261 Milk Patterns 后缀数组求 一个串种 最长可重复子串重复至少k次
Milk Patterns Description Farmer John has noticed that the quality of milk given by his cows varie ...
- [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 ...
- 【poj 3261】Milk Patterns 后缀数组
Milk Patterns 题意 给出n个数字,以及一个k,求至少出现k次的最长子序列的长度 思路 和poj 1743思路差不多,二分长度,把后缀分成若干组,每组任意后缀公共前缀都>=当前二分的 ...
随机推荐
- 2017.3.27 集成modeler后的一些主要路径(持续更新)
1.设计器访问路径 项目名:wfs_web edtor-app和modeler.html的存放位置:webapp/designer/editor-app app-cfg.js中根路径设置:'conte ...
- PropertyGrid—属性类别排序
属性默认按照字母顺序排序,有时,我们想要按自定义的顺序排序 这个工具类可以把每个属性类别里的属性排序,但是不能把属性类别排序. 为属性类添加属性:[TypeConverter(typeof(Prope ...
- Another unnamed CacheManager already exists in the same VM
今天学习Spring 缓存机制.遇到不少问题~ 好不easy缓存的单元測试用例调试成功了,在同一项目下单元測试另外一个文件时,发生了异常: org.springframework.beans.fact ...
- Java中对象、对象引用、堆、栈、值传递以及引用传递的详解
Java中对象.对象引用.堆.栈.值传递以及引用传递的详解 1.对象和对象引用的差别: (1).对象: 万物皆对象.对象是类的实例. 在Java中new是用来在堆上创建对象用的. 一个对象能够被多个引 ...
- ps快捷键记录
alt+delete 前景色填充 ctrl+delete 背景色填充 alt+shift+鼠标调节 变换选取,做圆环 ctrl+t 自由变换 alt+鼠标拖动 快捷复制某区域 delete ...
- 多系统启动光盘制作---WIN7+WinXP+老毛桃PE工具箱
1.工具: ⑴ Windows 7 ISO: ⑵ Windows XP ISO: ⑶ 老毛桃U盘启动盘制作工具V2013 制作得的ISO (含PE.DOS等): ⑷ UltraISO.EasyBoot ...
- java程序如何优化--技巧总结
http://www.douban.com/group/topic/17850695/
- NativeBase准备工作
环境 node>= 4.0 npm>= 3.0 rnpm (only if React Native version < 0.29) ReactNativeCLI 安装及运行 ht ...
- XFire Web Service客户端开发
一.项目创建: 创建一个Maven的web工程 Maven包导入pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- 巧用Excel提高工作效率
程序员如何巧用Excel提高工作效率 主要讲解下Excel中VLOOKUP函数的使用,相比于上一篇中的内容,个人觉得这个相对高级一些. 1.使用背景 为什么会使用到这个函数呢,背景是这样的,有两个系统 ...