后缀数组的应用。和男人八题那个后缀数组差不多。
/* 2890 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
int a[maxn], b[maxn], aa[maxn];
int pos[maxn], pn;
int rrank[maxn], height[maxn], sa[maxn];
int wa[maxn], wb[maxn], wc[maxn], wv[maxn];
int n, k, at; bool 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 *sa, int n, int m) {
int i, j, *x=wa, *y=wb, *t, p; for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[x[i]=r[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[x[i]]] = i;
for (j=,p=; p<n; j*=, m=p) {
for (p=,i=n-j; i<n; ++i) y[p++] = i;
for (i=; i<n; ++i) if (sa[i] >= j) y[p++] = sa[i] - j;
for (i=; i<n; ++i) wv[i] = x[y[i]];
for (i=; i<m; ++i) wc[i] = ;
for (i=; i<n; ++i) wc[wv[i]]++;
for (i=; i<m; ++i) wc[i] += wc[i-];
for (i=n-; i>=; --i) sa[--wc[wv[i]]] = y[i];
for (t=x, x=y, y=t, i=,p=,x[sa[]]=; i<n; ++i)
x[sa[i]] = cmp(y, sa[i-], sa[i], j) ? p-:p++;
}
} void calheight(int *r, int *sa, int n) {
int i, j, k=; for (i=; i<=n; ++i) rrank[sa[i]] = i;
for (i=; i<n; height[rrank[i++]]=k)
for (k?k--:, j=sa[rrank[i]-]; r[i+k]==r[j+k]; ++k);
} bool judge(int bound) {
int i = ;
int pn = ; while (i <= n) {
if (height[i] < bound) {
at = sa[i-];
sort(pos, pos+pn);
int kk = , p = pos[];
rep(j, , pn) {
if (pos[j]-p >= bound) {
p = pos[j];
++kk;
}
} if (kk >= k)
return true;
pn = ;
pos[pn++] = sa[i++];
} else {
pos[pn++] = sa[i++];
}
}
at = sa[n-];
sort(pos, pos+pn);
int kk = , p = pos[];
rep(j, , pn) {
if (pos[j]-p >= bound) {
p = pos[j];
++kk;
}
} if (kk >= k)
return true; return false;
} void printSa(int n) {
for (int i=; i<=n; ++i)
printf("%d ", sa[i]);
putchar('\n');
} void printHeight(int n) {
for (int i=; i<=n; ++i)
printf("%d ", height[i]);
putchar('\n');
} void solve() {
sort(b, b+n);
int nn = unique(b, b+n) - b; rep(i, , n)
aa[i] = lower_bound(b, b+nn, a[i]) - b + ;
aa[n] = ; da(aa, sa, n+, nn+);
calheight(aa, sa, n); #ifndef ONLINE_JUDGE
// printSa(n);
// printHeight(n);
#endif int l = , r = n, mid;
int ans = , fr; while (l <= r) {
mid = (l + r) >> ;
if (judge(mid)) {
ans = mid;
fr = at;
l = mid + ;
} else {
r = mid - ;
}
} printf("%d\n", ans);
rep(i, , ans)
printf("%d\n", b[aa[fr+i]-]);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
while (t--) {
scanf("%d %d", &n, &k);
rep(i, , n) {
scanf("%d", &a[i]);
b[i] = a[i];
}
solve();
if (t)
putchar('\n');
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}