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 57 58
| #include <iostream> #include <algorithm> #include <vector> #define maxn 1000010 #define lowbit(i) ((i) & (-i)) using namespace std;
int n, m, s[maxn], t[maxn];
int Bit[maxn]; void add(int i, int v) { while (i <= n) Bit[i] += v, i += lowbit(i); } int get_sum(int i) { int s = 0; while (i) s += Bit[i], i -= lowbit(i); return s; } void clear(int l, int r, int *a) { for (int i = l; i <= r; ++i) add(a[i], -1); }
int b[maxn]; void init_hash(int *a) { for (int i = 1; i <= n; ++i) b[i] = a[i]; sort(b + 1, b + n + 1); int cnt = unique(b + 1, b + n + 1) - b - 1; for (int i = 1; i <= n; ++i) a[i] = lower_bound(b + 1, b + cnt + 1, a[i]) - b; }
int nxt[maxn], cnt[maxn]; void init_nxt(int *s, int n) { for (int i = 1; i <= n; ++i) cnt[i] = get_sum(s[i]), add(s[i], 1); fill(Bit + 1, Bit + n + 1, 0); cnt[m + 1] = -1; int k = 0; nxt[1] = 0; for (int i = 2; i <= n; ++i) { while (k && cnt[k + 1] != get_sum(s[i])) clear(i - 1 - k + 1, i - 1 - nxt[k], s), k = nxt[k]; if (cnt[k + 1] == get_sum(s[i])) ++k, add(s[i], 1); nxt[i] = k; } }
void kmp(int *s, int n, int m) { vector<int> ans; fill(Bit + 1, Bit + n + 1, 0); int k = 0; for (int i = 1; i <= n; ++i) { while (k && cnt[k + 1] != get_sum(s[i])) clear(i - 1 - k + 1, i - 1 - nxt[k], s), k = nxt[k]; if (cnt[k + 1] == get_sum(s[i])) ++k, add(s[i], 1); if (k == m) ans.push_back(i - k + 1); } cout << ans.size() << "\n"; for (auto t : ans) cout << t << " "; cout << "\n"; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> m >> n; for (int i = 1, x; i <= m; ++i) cin >> x, t[x] = i; for (int i = 1; i <= n; ++i) cin >> s[i]; init_hash(s); for (int i = 1; i <= n; ++i) Bit[i] = 0; init_nxt(t, m); kmp(s, n, m); return 0; }
|