QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#227106 | #7188. Aho-Corasick Automaton | PPP# | WA | 4ms | 21968kb | C++17 | 3.1kb | 2023-10-26 22:17:42 | 2023-10-26 22:17:42 |
Judging History
answer
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
typedef long double ld;
const int mod = 2017;
int sum(int a, int b) {
int s = a + b;
if (s >= mod) s -= mod;
return s;
}
int mult(int a, int b) {
return (1LL * a * b) % mod;
}
int sub(int a, int b) {
int s = a - b;
if (s < 0) s += mod;
return s;
}
int n;
const int maxN = 2e5 + 10;
int p[maxN];
int c[maxN];
int len[maxN];
int cnt[maxN];
const int BUBEN = 502;
vector<int> f[maxN];
int link[maxN];
int ord[maxN];
int MEM[maxN / BUBEN + 2][maxN];
int cur[maxN];
vector<int> g[maxN];
int id[maxN];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> p[i];
len[i] = len[p[i]] + 1;
g[p[i]].emplace_back(i);
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
f[p[i]].emplace_back(c[i]);
}
for (int i = 0; i <= n; i++) {
sort(g[i].begin(), g[i].end(), [&](int a, int b) {
return c[a] < c[b];
});
sort(f[i].begin(), f[i].end());
}
iota(ord + 1, ord + n + 1, 1);
sort(ord + 1, ord + n + 1, [&](int x, int y) {
return len[x] < len[y];
});
for (int x : g[0]) {
MEM[0][c[x]] = x;
}
memset(id, -1, sizeof id);
id[0] = 0;
link[0] = 0;
int SZ = 1;
for (int i = 1; i <= n; i++) {
int who = ord[i];
int symb = c[who];
if (p[i] == 0) {
link[i] = 0;
}
else {
int D = link[p[i]];
int vert = -1;
while (id[D] == -1) {
int ps = lower_bound(f[D].begin(), f[D].end(), symb) - f[D].begin();
if (ps != f[D].size() && f[D][ps] == symb) {
vert = g[D][ps];
break;
}
D = link[D];
}
if (vert == -1) {
assert(id[D] != -1);
vert = MEM[id[D]][symb];
}
link[i] = vert;
}
cur[i] = cur[link[i]] + 1;
if (cur[i] >= BUBEN && SZ < maxN / BUBEN) {
for (int j = 1; j <= n; j++) {
int D = i;
int vert = -1;
while (id[D] == -1) {
int ps = lower_bound(f[D].begin(), f[D].end(), j) - f[D].begin();
if (ps != f[D].size() && f[D][ps] == j) {
vert = g[D][ps];
break;
}
D = link[D];
}
if (vert == -1) {
assert(id[D] != -1);
vert = MEM[id[D]][symb];
}
MEM[SZ][j] = vert;
}
id[i] = SZ;
SZ++;
}
}
for (int i = 1; i <= n; i++) cout << link[i] << " ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 18524kb
input:
2 0 0 1 2
output:
0 0
result:
ok 2 number(s): "0 0"
Test #2:
score: 0
Accepted
time: 0ms
memory: 19252kb
input:
2 0 1 1 1
output:
0 1
result:
ok 2 number(s): "0 1"
Test #3:
score: 0
Accepted
time: 0ms
memory: 21968kb
input:
1 0 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: -100
Wrong Answer
time: 3ms
memory: 18820kb
input:
50 0 1 1 1 0 0 4 4 3 7 1 11 1 11 2 15 13 11 4 11 5 7 22 17 4 19 19 26 19 27 3 17 9 4 14 8 15 33 33 33 9 40 24 18 5 28 10 1 47 25 20 20 31 1 43 3 3 21 3 3 3 22 43 3 1 20 3 43 43 20 3 20 1 3 20 20 3 1 43 3 20 20 20 29 3 3 3 3 20 1 3 3 3 3 20 3 3 25 3 1
output:
0 6 1 1 0 0 48 13 11 0 0 1 6 0 1 2 5 5 11 0 6 6 0 0 4 14 14 35 14 35 2 21 14 2 1 1 2 35 5 35 20 4 6 0 0 11 6 6 6 7
result:
wrong answer 2nd numbers differ - expected: '1', found: '6'