QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#864116 | #2343. First of Her Name | winsun | WA | 0ms | 14060kb | C++14 | 3.1kb | 2025-01-20 10:35:44 | 2025-01-20 10:35:44 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#define fi first
#define se second
using namespace std;
using LL = long long;
using LLL = __int128;
const int MAXN = 1e6+5;
template <typename Tp> void read(Tp &res) {
char ch; bool op = 0; res = 0;
do ch = getchar(), op |= ch == '-'; while (ch < '0' || ch > '9');
do res = (res<<3)+(res<<1)+(ch&15), ch = getchar(); while(ch >= '0' && ch <= '9');
if (op) res = -res;
}
bool Mst;
int n, m, q;
int pa[MAXN][21], dep[MAXN], tot;
char str[MAXN];
int sa[MAXN], rk[MAXN], tmp[MAXN], cnt[MAXN], ht[MAXN];
bool Med;
void initpa(int x) {
for (int k = 1; pa[x][k-1]; ++k) pa[x][k] = pa[pa[x][k-1]][k-1];
}
void rsort() {
for (int i = 1; i <= m; ++i) cnt[i] = 0;
for (int i = 1; i <= n; ++i) ++cnt[rk[i]];
for (int i = 1; i <= m; ++i) cnt[i] += cnt[i-1];
for (int i = n; i; --i) sa[cnt[rk[tmp[i]]]--] = tmp[i];
}
void suffixSort() {
m = 26;
for (int i = 1; i <= n; ++i) tmp[i] = n - i + 1, rk[i] = str[i] - 'A' + 1;
rsort();
for (int k = 0; k <= 20; ++k) {
int x = 0;
for (int i = 1; i <= m; ++i) cnt[i] = 0;
for (int i = 1; i <= n; ++i) !pa[i][k] ? tmp[++x] = i : ++cnt[rk[pa[i][k]]];
for (int i = 1; i <= m; ++i) cnt[i] += cnt[i-1];
for (int i = n; i; --i) if (pa[i][k]) tmp[x+cnt[rk[pa[i][k]]]--] = i;
rsort();
for (int i = 1; i <= n; ++i) tmp[i] = rk[i];
auto cmp = [k](int x, int y) { return tmp[x] != tmp[y] || tmp[pa[x][k]] != tmp[pa[y][k]]; };
rk[sa[1]] = m = 1;
for (int i = 2; i <= n; ++i) rk[sa[i]] = m += cmp(sa[i], sa[i-1]);
}
}
char t[MAXN]; int len;
int cmp(int x) {
int lim = min(len, dep[x]);
for (int i = 1; i <= lim; x = pa[x][0], ++i) {
if (t[i] < str[x]) return 1;
if (t[i] > str[x]) return -1;
}
return lim != len;
}
int queryL() {
int l = 1, r = n + 1;
while (l < r) {
int mid = (l + r) >> 1;
cmp(sa[mid]) >= 0 ? r = mid : l = mid + 1;
}
return l;
}
int queryR() {
int l = 1, r = n + 1;
// for (int i = 1; i <= n; ++i) printf("%d%c", cmp(sa[i]), " \n"[i==n]);
while (l < r) {
int mid = (l + r) >> 1;
cmp(sa[mid]) > 0 ? r = mid : l = mid + 1;
}
return l;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("lg6257.in", "r", stdin);
freopen("lg6257.out", "w", stdout);
fprintf(stderr, "%.3lf\n", (&Mst - &Med) / 1048576.0);
#endif
read(n), read(q);
for (int i = 1; i <= n; ++i) {
int p;
scanf("%s%d", &str[i], &p);
dep[i] = dep[p] + 1;
pa[i][0] = p;
initpa(i);
}
suffixSort();
// for (int i = 1; i <= n; ++i) {
// int x = sa[i];
// for (; x; x = pa[x][0]) putchar(str[x]);
// putchar('\n');
// }
while (q--) {
scanf("%s", t + 1);
len = strlen(t + 1);
int lb = queryL(), rb = queryR();
printf("%d\n", rb - lb);
}
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 0ms
memory: 14060kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 14052kb
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 14060kb