POJ 3261:Milk Patterns
2016-02-01 23:47
393 查看
Milk Patterns
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
Sample Output
找出至少重复k次的最长重复子串。
还是二分这个答案x。对height值进行分组,如果某一组里面的height值都大于等于x,且至少有k个,返回true。
代码:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 12821 | Accepted: 5699 | |
Case Time Limit: 2000MS |
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
找出至少重复k次的最长重复子串。
还是二分这个答案x。对height值进行分组,如果某一组里面的height值都大于等于x,且至少有k个,返回true。
代码:
#pragma warning(disable:4996) #include <iostream> #include <algorithm> #include <cstring> #include <cstring> #include <vector> #include <string> #include <cmath> #include <queue> #include <map> using namespace std; typedef long long ll; #define INF 0x3fffffff const int mod = 1e9 + 7; const int maxn = 1000002; int nx, kx; int sa[maxn], num[maxn]; int ran[maxn], height[maxn]; int wa[maxn], wb[maxn], wv[maxn], wss[maxn]; int 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 n, int m) // 倍增算法0(nlgn)。 { int i, j, p, *x = wa, *y = wb, *t; for (i = 0; i < m; i++) { wss[i] = 0; } for (i = 0; i < n; i++) { wss[x[i] = r[i]] ++; } for (i = 1; i < m; i++) { wss[i] += wss[i - 1]; } for (i = n - 1; i >= 0; i--) { sa[--wss[x[i]]] = i; } for (j = 1, p = 1; p < n; j *= 2, m = p) { for (p = 0, i = n - j; i < n; i++) { y[p++] = i; } for (i = 0; i < n; i++)//sa[x]=y 表示排第x的是y位置 { if (sa[i] >= j)//假设sa[i]=x,便表示排第i位的是第x位置, //要满足x>=j,第二关键字赋值才有意义 //如果满足了这个关系,即在y上累加,做记录(前面比它小的都做完记录了) { y[p++] = sa[i] - j; } } for (i = 0; i < n; i++) { wv[i] = x[y[i]];//y[i]=x 表示第二关键字排第i位的是x //x数组保存的就是当前的rank值 //x[i]=j 表示i后缀 排到了第j位 //x[y[i]]的意思就是把第二关键字排第i位 //的那个第一关键字搞出来,按照第一关键字排序 //相等的第一关键字 按照第二关键字排序 } for (i = 0; i < m; i++) { wss[i] = 0; } for (i = 0; i < n; i++) { wss[wv[i]] ++; } for (i = 1; i < m; i++) { wss[i] += wss[i - 1]; } for (i = n - 1; i >= 0; i--) { sa[--wss[wv[i]]] = y[i];//y[i]=x 表示第二关键字排第i位的是x //sa[x]=y表示总体记录,整个的字符串排在第x位的是y } for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) { x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } } } void calHeight(int *r, int n) // 求height数组 { int i, j, k = 0; for (i = 1; i <= n; i++) { ran[sa[i]] = i; } for (i = 0; i < n; i++) { if (k) { k--; } else { k = 0; } j = sa[ran[i] - 1];//表示后缀i排的位置前一位的 位置 while (r[i + k] == r[j + k]) { k++; } height[ran[i]] = k; } } bool check(int x) { int i, k = 0; for (i = 1; i <= nx; i++) { if (k == 0) { if (height[i] >= x) { k = k + 2; if (k>= kx) return true; } } else { if (height[i] >= x) { k++; if (k >= kx) return true; } else { k = 0; } } } return false; } void input() { int i; for (i = 0; i < nx; i++) { scanf("%d", num + i); } } void solve() { num[nx] = 0; da(num, nx + 1, 1000000); calHeight(num, nx); int le, ri, mid; le = 0; ri = nx; while (le < ri) { mid = (le + ri + 1) / 2; if (check(mid)) { le = mid; } else { ri = mid - 1; } } printf("%d\n", le); } int main() { //freopen("i.txt", "r", stdin); //freopen("o.txt", "w", stdout); while (scanf("%d%d", &nx, &kx) != EOF) { input(); solve(); } //system("pause"); return 0; }
相关文章推荐
- UVa 11323 Satisfying Constraints
- HashMap扩容函数解读
- AR--未来技术提前探索<4>[在汉明码上叠加一个视频]
- POJ 3259 -- 最短路径 ( bellman ford算法, 允许负权边)
- 密码学初级教程(五)消息认证码MAC-Message Authentication Code
- 括号配对问题
- 定时器的应用(一):延时提示框
- 虚拟服务器Windwos server 2003远程安装SQL server 2008R2提示0x2d2816fe错误解决办法
- SSH项目开发中,将jsp页面放在WEB-INF的原因解析
- Java初学笔记
- Error retrieving parent for item: No resource found that matches the given name 'android:TextAppeara
- SGU 536 Berland Chess(状压+BFS)
- 《秦始皇》读书笔记
- node-webkit is renamed NW.js
- VS使用总结
- POJ 1743:Musical Theme
- 3-2 wordcount执行流程 wordcount流程
- vim显示^M的解决办法
- java List集合类的用法
- ZOJ 2853 Evolution (矩阵快速幂+数学)