이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int MAX_N = 1e6 + 6;
int n, m;
int Arr[MAX_N];
int Brr[MAX_N];
int Crr[MAX_N];
int F[MAX_N];
int sm[MAX_N];
int bg[MAX_N];
vector <int> ans;
bool can (int *list, int l, int r) {
int ind = r - l;
if (list[r] < list[l + sm[ind]])
return false;
if (list[l + bg[ind]] < list[r])
return false;
return true;
}
void KMP() {
F[1] = 0;
F[2] = 1;
int ind = 1;
for (int i = 3; i <= n; i++) {
while (ind && !can(Arr, i - ind - 1 , i - 1))
ind = F[ind];
ind++;
F[i] = ind;
}
}
void processRel() {
sm[Crr[0]] = Crr[0];
for (int i = 1; i < n; i++) {
sm[Crr[i]] = Crr[i];
int ind = Crr[i - 1];
while (sm[ind] != ind && Crr[i] < ind)
ind = sm[ind];
if (ind <= Crr[i])
sm[Crr[i]] = ind;
}
bg[Crr[n - 1]] = Crr[n - 1];
for (int i = n - 2; ~i; i--) {
bg[Crr[i]] = Crr[i];
int ind = Crr[i + 1];
while (bg[ind] != ind && Crr[i] < ind)
ind = bg[ind];
if (ind <= Crr[i])
bg[Crr[i]] = ind;
}
}
void findMatching() {
int L = 0, R = 0;
while (L < m) {
while (R + 1 < L + n && R + 1 < m && can(Brr, L, R + 1))
R++;
if (R - L == n - 1)
ans.push_back(L + 1);
if (L == R)
L++, R++;
else
L += R - L + 1 - F[R - L + 1];
}
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> Crr[i];
Crr[i]--;
Arr[Crr[i]] = i;
}
processRel();
for (int i = 0; i < m; i++)
cin >> Brr[i];
KMP();
findMatching();
cout << ans.size() << "\n";
for (int l: ans)
cout << l << (ans.back() == l? "\n": " ");
return 0;
}
// S --> 5:45
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |