HihoCoder-1403 suffix Array one • Repeat Melody

Source: Internet
Author: User

Description

Small hi usually a big hobby is playing the piano. We know that a musical melody is represented as a series of numbers consisting of N in length.

Little hi has practiced a lot of music and found that many of the works themselves contain the same melody. Melody is a continuous sequence of numbers, similar melodies can overlap in the original sequence. In 1 2 3 2 3 2 1, for example, 2 3 2 have occurred.

Little hi want to know what is the longest melody in a melody at least k times?

input

The first line is two integers n and K. 1≤n≤20000 1≤k≤n

Next there are N integers that represent the number of each tone. 1≤ Digital ≤100

Output

An integer line that represents the answer.

Sample Input

8 212323231

Sample Output

  4 


> It is obvious that the maximum value of the minimum value of a continuous K-height array is asked,

" /strong> Then there are two ways to do a binary or monotone queue
Here is a monotone queue
#include <bits/stdc++.h>using namespace Std;const int maxn = 200005;int N, K, RK[MAXN], SA[MAXN], Y[MAXN], LN, p, M, C[maxn];int H[MAXN], WR[MAXN], Q[MAXN], L, R, A[maxn], POS[MAXN], k;bool cmp (int k1, int k2, int ls) {return WR[K1] = = WR [K2] && wr[k1 + ls] = = wr[k2 + ls];}  void SA (int x) {m = x;for (int i = 1; I <= n; ++i) c[rk[i]]++;for (int i = 1; I <= m; ++i) c[i] + = c[i-1];for (int i = n; I >= 1; i) sa[c[rk[i]]--] = I;ln = 1, p = 0;while (P < n) {k = 0;for (int i = n-ln + 1; I <= n; ++i) y[++k] = i;for (int i = 1; I <= N;  ++i) if (Sa[i] > ln) y[++k] = Sa[i]-Ln;memset (c, 0, sizeof c); for (int i = 1; I <= n; ++i) Wr[i] = rk[i];for (int i = 1; I <= N; ++i) c[wr[y[i]]]++; for (int i = 1; I <= m; ++i) c[i] + = c[i-1];for (int i = n; i >= 1; i.) sa[c[wr[y[i]]]--] = y[i];for (int i = 1; I <= N; ++i) Wr[i] = rk[i];rk[sa[1]] = 1; p = 1;for (int i = 2; I <= n; ++i) {if (!cmp (Sa[i], sa[i-1], LN)) ++p;rk[sa[i]] = p;}} ln <<=1; m = P;} void Gh () {k = 0;for (int i = 1; I <= n; ++i) rk[sa[i]] = i;for (int i = 1; I <= n; ++i) {if (k)--k; int J = Sa[rk[i ]-1];while (a[i + K] = = A[j + K]) ++k; H[rk[i]] = k;}} int main () {scanf ("%d%d", &n, &k), for (int i = 1; I <= n; ++i) scanf ("%d", &rk[i]), a[i] = Rk[i];sa ("G"); H (); int ans = 0;for (int i = 1; I <= n; ++i) {while (L < R && H[i] < Q[r]) r--;q[++r] = H[i]; Pos[r] = I;while (L < R && Pos[l] <= i-k + 1) ++l;if (i >= K) ans = max (ans, q[l]);} printf ("%d\n", ans); for (int i = 1; I <= n; ++i) cout << h[i] << ""; return 0;}

  

HihoCoder-1403 suffix Array one • Repeat Melody

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.