이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dou;
#define pii pair<int, int>
#define pb push_back
#define fi first
#define se second
bool maximize(ll &x, ll y ){
if (x < y) {x = y; return true;};
return false;
}
bool minimize(ll &x, ll y){
if (x > y) {x = y; return true;}
return false;
}
const int maxn = 1e6+5;
int n, m, a[maxn], s[maxn];
int kmp[maxn], match[maxn];
int ind1[maxn], ind2[maxn], point1[maxn], point2[maxn], corind[maxn]; //for every prefix
bool check(int i, int temp, int val[]) {
return ((ind1[temp]==0 || val[i]>val[i-temp+ind1[temp]])&&
(ind2[temp]==0 || val[i]<val[i-temp+ind2[temp]]));
}
void setupkmp() {
memset(ind1, 0, sizeof(ind1));
memset(ind2, 0, sizeof(ind2));
point1[0] = point2[0] = 0;
for (int i = 1; i <= n; i++) {
point1[i] = i-1;
point2[i] = i+1;
if (i == n) point2[i] = 0;
}
for (int i = n; i; i--) {
ind1[i] = corind[point1[s[i]]];
ind2[i] = corind[point2[s[i]]];
point1[point2[s[i]]] = point1[s[i]];
point2[point1[s[i]]] = point2[s[i]];
}
int temp = kmp[1] = 0;
for (int i = 2; i <= n; i++) {
while (temp > 0 && !check(i, temp+1, s)) temp = kmp[temp];
kmp[i] = check(i, temp+1, s) ? ++temp : 0;
}
}
void kmpsolve() {
int ans = 0;
int temp = match[0] = 0;
for (int i= 1; i <= m; i++) {
if (temp == n) --temp;
while (temp > 0 && !check(i, temp+1, a)) temp = kmp[temp];
match[i] = check(i, temp+1, a) ? ++temp : 1;
if (match[i] == n) ans++;
}
cout << ans << "\n";
for (int i = 1; i <= m; i++) {
if (match[i] == n) cout << i-n+1 << ' ';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
corind[0] = 0;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> corind[i];
s[corind[i]] = i;
}
for (int i= 1; i <= m; i++) {
cin >> a[i];
}
setupkmp();
kmpsolve();
}
/*
5 10
2 1 5 3 4
5 6 3 8 12 7 1 10 11 9
*/
# | 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... |