Milk Patterns [Coaches, 2004]

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. PROBLEM NAME: patterns INPUT FORMAT: * 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. SAMPLE INPUT (file patterns.in): 8 2 1 2 3 2 3 2 3 1 OUTPUT FORMAT: * Line 1: One integer, the length of the longest pattern which occurs at least K times SAMPLE OUTPUT (file patterns.out): 4
ქართული თარგმანი არ არის დამატებული

patterns

The simple way to solve this is to binary search for the answer. To determine whether there are at least K occurrences of some length L sequence, iterate over all length L sequences and count how many of each there are. Of course, this is easier said than done, since blindingly comparing two such sequences may take O(L) time, and so even if one used a sort or a search tree, O(NL.log N) steps may be necessary. Using a hash table reduces this to O(NL), provided that collisions are not excessive. However, with the right hash function, we can do all the hashing operations in constant time. To hash a sequence a0, a1, a2, ..., aL-1 we use the function a0*x^(L-1) + a1*x^(L-2) + ... + aL-2*x + aL-1 (modulo the table size, although GCC's hash_map does this internally). This is called a polynomial hash function for obvious reasons. If the hash above is H, then the hash of a1, a2, ..., aL is x*H - a0*x^L + aL i.e., we can update the hash for the next substring of length L in constant time (this technique is also used in the Rabin-Karp string-matching algorithm). The worst case is still O(NL) for a single value of L, because if the majority of the substrings are duplicates then they require a full comparison (although if there are K duplicated one may early-out). However, the worst case will not occur for all tested values of L, so this solution is fast enough for all the test data. It is also theoretically possible to solve the problem in linear time, using a data structure called a suffix tree. This is a trie built from every suffix of the input data. While this structure appears to contain O(N^2) data, it can be compactly encoded into O(N) space, and even more amazingly it can be computed in O(N) time. If you are interested in the details of the algorithm you should search online; suffice it to say that it is extremely subtle and not something one is likely to implement during a contest. Once one has a suffix tree, it is possible to compute the number of occurrences of any substring: simply walk down the tree to find the node corresponding to that substring, then count the leaves that are descendants of that node. So the answer is the depth of the deepest node with at least K leaf descendants. By working bottom-up in the compressed suffix tree, this can be computed in O(N) time. #include <fstream> #include <algorithm> #include <cstddef> #include <ext/hash_map> #include <cassert> using namespace std; using namespace __gnu_cxx; #define MAGIC 10000019 #define MAXV 1000000 struct range { int *s, *e; size_t h; }; struct eq { bool operator()(const range &a, const range &b) const { return a.h == b.h && equal(a.s, a.e, b.s); } }; struct hsh { size_t operator()(const range &a) const { return a.h; } }; int main() { int N, K; int data[20001]; ifstream in("patterns.in"); in >> N >> K; for (int i = 0; i < N; i++) { in >> data[i]; assert(data[i] >= 0 && data[i] <= MAXV); } int low = 1; int high = N; while (high - low > 1) { int l = (low + high) / 2; int s = 1; hash_map<range, int, hsh, eq> h; range cur = {data, data + l, 0}; bool found = false; for (int i = 0; i < l; i++) { cur.h = cur.h * MAGIC + data[i]; s *= MAGIC; } for (int i = 0; i + l <= N; i++) { if (++h[cur] >= K) { found = true; break; } cur.h = cur.h * MAGIC - s * data[i] + data[i + l]; cur.s++; cur.e++; } if (found) low = l; else high = l; } ofstream out("patterns.out"); out << low << "\n"; return 0; }

Below is Bruce's solution

#include <fstream>
#include <algorithm>
#include <cstddef>
#include <ext/hash_map>
#include <cassert>

using namespace std;
using namespace __gnu_cxx;

#define MAGIC 10000019
#define MAXV 1000000

struct range {
    int *s, *e;
    size_t h;
};

struct eq {
    bool operator()(const range &a, const range &b) const
    { return a.h == b.h && equal(a.s, a.e, b.s); }
};

struct hsh {
    size_t operator()(const range &a) const { return a.h; }
};

int main() {
    int N, K;
    int data[20001];
    ifstream in("patterns.in");
    in >> N >> K;
    for (int i = 0; i < N; i++) {
        in >> data[i];
        assert(data[i] >= 0 && data[i] <= MAXV);
    }

    int low = 1;
    int high = N;
    while (high - low > 1) {
        int l = (low + high) / 2;
        int s = 1;
        hash_map<range, int, hsh, eq> h;
        range cur = {data, data + l, 0};
        bool found = false;

        for (int i = 0; i < l; i++) {
            cur.h = cur.h * MAGIC + data[i];
            s *= MAGIC;
        }
        for (int i = 0; i + l <= N; i++) {
            if (++h[cur] >= K) { found = true; break; }
            cur.h = cur.h * MAGIC - s * data[i] + data[i + l];
            cur.s++;
            cur.e++;
        }

        if (found) low = l;
        else high = l;
    }
    ofstream out("patterns.out");
    out << low << "\n";
    return 0;
}