1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <bits/stdc++.h> using namespace std;
const int N = 1e6 + 66;
int n, m, t; int a[N], b[N]; int nex[N], xa[N], xb[N], f[N], q[N];
inline void pres_dou() { n = read(), m = read(); memset(nex, 0, sizeof nex); for (int i = 1; i <= n; ++ i) { a[i] = read(); xa[i] = i - nex[a[i]], nex[a[i]] = i; } memset(nex, 0, sizeof nex); for (int i = 1; i <= m; ++ i) { b[i] = read(); xb[i] = i - nex[b[i]], nex[b[i]] = i; } return; }
inline void solve() { t = xb[m + 1] = 0; int j = 0; for (int i = 2; i <= m; ++ i) { while (j && min(j + 1, xb[i]) != xb[j + 1]) j = f[j]; if (min(j + 1, xb[i]) == xb[j + 1]) ++ j; f[i] = j; } j = 0; for (int i = 1; i <= n; ++ i) { while (j && min(j + 1, xa[i]) != xb[j + 1]) j = f[j]; if (min(j + 1, xa[i]) == xb[j + 1]) ++ j; if (j == m) q[++ t] = i; } put(t); for (int i = 1; i <= t; ++ i) printf ("%d ", q[i] - m + 1); puts(""); return; }
signed main() { int T = read(), C = read(); while (T --) pres_dou(), solve(); return 0; }
|