天天看點

POJ 3261 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 

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

字尾數組加二分驗證

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
int T, m;

struct Sa
{
  int s[N];
  int rk[2][N], sa[N], h[N], w[N], now, n;
  //int rmq[N][20], lg[N];

  bool GetS()
  {
    if (scanf("%d%d", &n, &m) == EOF) return false;
    rep(i, 1, n) scanf("%d", &s[i]);
    return true;
  }

  void getsa(int z, int &m)
  {
    int x = now, y = now ^= 1;
    rep(i, 1, z) rk[y][i] = n - i + 1;
    for (int i = 1, j = z; i <= n; i++)
      if (sa[i] > z) rk[y][++j] = sa[i] - z;

    rep(i, 1, m) w[i] = 0;
    rep(i, 1, n) w[rk[x][rk[y][i]]]++;
    rep(i, 1, m) w[i] += w[i - 1];
    per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
    for (int i = m = 1; i <= n; i++)
    {
      int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
      rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
    }
  }

  void getsa(int m)
  {
    now = 0;  //n = strlen(s + 1);
    rep(i, 1, m) w[i] = 0;
    rep(i, 1, n) w[s[i]]++;
    rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
    rep(i, 1, m) w[i] += w[i - 1];
    rep(i, 1, n) rk[0][i] = rk[1][s[i]];
    rep(i, 1, n) sa[w[s[i]]--] = i;
    for (int x = 1, y = rk[1][m]; x <= n && y < n; x <<= 1) getsa(x, y);
    for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : 0)
      while (s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
  }

  /*void getrmq()
  {
    lg[1] = 0;
    rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
    for (int i = 1; (1 << i) <= n; i++)
    {
      rep(j, 2, n)
      {
        if (j + (1 << i) > n + 1) break;
        rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
      }
    }
  }

  int lcp(int x, int y)
  {
    int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
    return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
  }
  */

  bool check(int x)
  {
    int y = 1;
    rep(i, 2, n)
    {
      if (h[i] >= x) y++;
      else if (y >= m) return true; else y = 1;
    }
    return y >= m;
  }
  int work()
  {
    int l = 0, r = n - 1;
    while (l <= r)
    {
      if (check(l + r >> 1)) l = (l + r >> 1) + 1;
      else r = (l + r >> 1) - 1;
    }
    return r;
  }
}sa;

int main()
{
  while (sa.GetS())
  {
    sa.getsa(N - 1);
    printf("%d\n", sa.work());
  }
  return 0;
}