码迷,mamicode.com
首页 > 编程语言 > 详细

[POJ3261] Milk Patterns (后缀数组+二分)

时间:2015-11-20 19:44:05      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

  • 题目概述:

  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.

  老司机FJ有一个长为n序列,请你帮忙找出最长的出现了至少k次的子串(子串之间可重叠)。

  • 输入格式:

  第一行包括两个空格隔开的整数n和k,表示序列的长度和要求出现的最少次数。

  第2..n+1行,表示序列中的每一个数。

  • 输出格式:

  输出一个整数,表示满足题意的子串的长度。

  • 样例输入:

  8 2

  1

  2

  3

  2

  3

  2

  3

  1

  • 样例输出:

  4

  • 思路:

  首先,不用说,肯定是后缀数组做一遍。

  因为子串长度与出现次数有某种负相关,所以二分最长的长度,对于一段连续的h[i]>=mid,子串出现次数即为连续后缀的个数。

  Linux好难用呀,不过第三道后缀数组,总算AC了。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 const int MAXN = 20005;
 6 int s[MAXN], sx[MAXN], sy[MAXN], ssum[20010], sa[MAXN], rk[MAXN], h[MAXN]; //ssum好像不需要开到1,000,000,也许数据弱
 7 inline bool cmp(int* y, int i, int j, int k) {
 8     return y[i] == y[j] && y[i + k] == y[j + k];
 9 }
10 
11 void getsa(int n, int m) {
12     int i, j, p, *x = sx, *y = sy;
13     for(i = 0; i < m; ++i) ssum[i] = 0;
14     for(i = 0; i < n; ++i) ++ssum[x[i] = s[i]];
15     for(i = 1; i < m; ++i) ssum[i] += ssum[i - 1];
16     for(i = n - 1; ~i; --i) sa[--ssum[x[i]]] = i;
17     for(p = j = 1; p < n; j <<= 1, m = p) {
18         for(p = 0, i = n - j; i < n; ++i) y[p++] = i;
19         for(i = 0; i < n; ++i) if(sa[i] >= j) y[p++] = sa[i] - j;
20         for(i = 0; i < m; ++i) ssum[i] = 0;
21         for(i = 0; i < n; ++i) ++ssum[x[y[i]]];
22         for(i = 1; i < m; ++i) ssum[i] += ssum[i - 1];
23         for(i = n - 1; ~i; --i) sa[--ssum[x[y[i]]]] = y[i];
24         swap(x, y); x[sa[0]] = 0;
25         for(p = i = 1; i < n; ++i) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++;
26     }
27 }
28 
29 void geth(int n) {
30     int j, k = 0;
31     for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
32     for(int i = 0; i < n; h[rk[i++]] = k)
33         for(k ? k-- : 0, j = sa[rk[i] - 1]; s[i + k] == s[j + k]; ++k);
34 }
35 
36 int solve(int x, int k, int n) {
37     int i = 2;
38     while(1) {
39     int ans = 1;
40         while(i <= n && h[i] < x) ++i;
41         if(i > n) break;
42         while(i <= n && h[i] >= x) {
43             ++ans; ++i;
44         }
45         if(ans >= k) return true;
46     }
47     return false;
48 }
49 
50 int main() {
51     int n, k;
52     scanf("%d%d", &n, &k);
53     for(int i = 0; i < n; ++i) scanf("%d", s + i);
54     getsa(n + 1, 20005); geth(n);
55     int l = 1, r = (n >> 1) + 1;
56     while(l < r - 1) {
57         int mid = l + r >> 1;
58         if(solve(mid, k, n)) l = mid;
59         else r = mid;
60     }
61     printf("%d\n", l);
62     return 0;
63 }

 

  最近得和字符串大干一场了......  

[POJ3261] Milk Patterns (后缀数组+二分)

标签:

原文地址:http://www.cnblogs.com/CtrlCV/p/4981709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!