QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#67570 | #2343. First of Her Name | alpha1022 | AC ✓ | 694ms | 323764kb | C++23 | 2.2kb | 2022-12-10 17:33:21 | 2022-12-10 17:33:24 |
Judging History
answer
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1e6;
int n, m, k;
char s[N + 5];
struct node {
int ch[26];
int ed;
} tr[N + 5];
namespace SAM {
struct node {
int ch[26];
int fa, len;
} sam[(N << 1) + 5];
int tot = 1, las = 1, sz[(N << 1) + 5];
inline void insert(int x) {
int cur = las, p = las = ++tot, nxt;
sam[p].len = sam[cur].len + 1, sz[p] = 1;
for (; cur && !sam[cur].ch[x]; cur = sam[cur].fa)
sam[cur].ch[x] = p;
if (!cur)
sam[p].fa = 1;
else {
int q = sam[cur].ch[x];
if (sam[cur].len + 1 == sam[q].len)
sam[p].fa = q;
else {
nxt = ++tot;
sam[nxt] = sam[q], sam[nxt].len = sam[cur].len + 1, sam[p].fa = sam[q].fa = nxt;
for (; cur && sam[cur].ch[x] == q; cur = sam[cur].fa)
sam[cur].ch[x] = nxt;
}
}
}
int c[N + 5], a[(N << 1) + 5];
void dfs(int p) {
static int s[N + 5], len = 0;
if (len) {
las = 1;
for (register int i = 1; i <= len; ++i)
insert(s[i]);
++sz[las];
}
for (register int i = 0; i < 26; ++i)
if (tr[p].ch[i])
s[++len] = i, dfs(tr[p].ch[i]), --len;
}
inline void init() {
//dfs(1);
for (register int i = 1; i <= tot; ++i)
++c[sam[i].len];
for (register int i = 1; i <= N; ++i)
c[i] += c[i - 1];
for (register int i = tot; i > 1; --i)
a[c[sam[i].len]--] = i;
for (register int i = tot; i > 1; --i)
sz[sam[a[i]].fa] += sz[a[i]];
}
}
int q[N + 5], head, tail;
int main() {
scanf("%d%d", &n, &k);
char ch;
int fa;
for (register int i = 1; i <= n; ++i)
scanf(" %c%d", &ch, &fa), tr[++fa].ch[ch - 'A'] = i + 1;
q[tail = 1] = 1, tr[1].ed = 1;
for (register int p; head < tail;) {
p = q[++head];
for (register int i = 0; i < 26; ++i)
if (tr[p].ch[i])
SAM::las = tr[p].ed, SAM::insert(i), tr[tr[p].ch[i]].ed = SAM::las, q[++tail] = tr[p].ch[i];
}
SAM::init();
for (; k; --k) {
scanf("%s", s + 1), m = strlen(s + 1);
int p = 1, flag = 0;
for (register int i = m; i; --i) {
if (!SAM::sam[p].ch[s[i] - 'A']) {
puts("0"), flag = 1;
break;
}
p = SAM::sam[p].ch[s[i] - 'A'];
}
if (!flag)
printf("%d\n", SAM::sz[p]);
}
}
Details
Test #1:
score: 100
Accepted
time: 1ms
memory: 7588kb
Test #2:
score: 0
Accepted
time: 5ms
memory: 5520kb
Test #3:
score: 0
Accepted
time: 5ms
memory: 5528kb
Test #4:
score: 0
Accepted
time: 1ms
memory: 5316kb
Test #5:
score: 0
Accepted
time: 1ms
memory: 5524kb
Test #6:
score: 0
Accepted
time: 5ms
memory: 5468kb
Test #7:
score: 0
Accepted
time: 1ms
memory: 5472kb
Test #8:
score: 0
Accepted
time: 1ms
memory: 5552kb
Test #9:
score: 0
Accepted
time: 1ms
memory: 5544kb
Test #10:
score: 0
Accepted
time: 5ms
memory: 5568kb
Test #11:
score: 0
Accepted
time: 257ms
memory: 233040kb
Test #12:
score: 0
Accepted
time: 1ms
memory: 7560kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 5572kb
Test #14:
score: 0
Accepted
time: 1ms
memory: 5468kb
Test #15:
score: 0
Accepted
time: 1ms
memory: 5584kb
Test #16:
score: 0
Accepted
time: 2ms
memory: 5564kb
Test #17:
score: 0
Accepted
time: 5ms
memory: 5488kb
Test #18:
score: 0
Accepted
time: 1ms
memory: 5540kb
Test #19:
score: 0
Accepted
time: 1ms
memory: 5520kb
Test #20:
score: 0
Accepted
time: 1ms
memory: 5548kb
Test #21:
score: 0
Accepted
time: 1ms
memory: 5552kb
Test #22:
score: 0
Accepted
time: 1ms
memory: 5544kb
Test #23:
score: 0
Accepted
time: 2ms
memory: 7812kb
Test #24:
score: 0
Accepted
time: 4ms
memory: 7752kb
Test #25:
score: 0
Accepted
time: 10ms
memory: 8540kb
Test #26:
score: 0
Accepted
time: 3ms
memory: 8740kb
Test #27:
score: 0
Accepted
time: 2ms
memory: 8032kb
Test #28:
score: 0
Accepted
time: 3ms
memory: 8076kb
Test #29:
score: 0
Accepted
time: 5ms
memory: 8080kb
Test #30:
score: 0
Accepted
time: 1ms
memory: 8096kb
Test #31:
score: 0
Accepted
time: 3ms
memory: 7772kb
Test #32:
score: 0
Accepted
time: 14ms
memory: 14176kb
Test #33:
score: 0
Accepted
time: 8ms
memory: 16460kb
Test #34:
score: 0
Accepted
time: 236ms
memory: 232092kb
Test #35:
score: 0
Accepted
time: 694ms
memory: 323764kb
Test #36:
score: 0
Accepted
time: 613ms
memory: 318928kb
Test #37:
score: 0
Accepted
time: 287ms
memory: 271400kb
Test #38:
score: 0
Accepted
time: 371ms
memory: 272700kb
Test #39:
score: 0
Accepted
time: 589ms
memory: 238600kb
Test #40:
score: 0
Accepted
time: 582ms
memory: 238836kb
Test #41:
score: 0
Accepted
time: 303ms
memory: 249328kb
Test #42:
score: 0
Accepted
time: 333ms
memory: 293952kb
Test #43:
score: 0
Accepted
time: 277ms
memory: 293460kb
Test #44:
score: 0
Accepted
time: 345ms
memory: 232076kb
Test #45:
score: 0
Accepted
time: 608ms
memory: 272748kb
Test #46:
score: 0
Accepted
time: 1ms
memory: 5548kb
Test #47:
score: 0
Accepted
time: 1ms
memory: 5544kb
Test #48:
score: 0
Accepted
time: 2ms
memory: 5500kb
Test #49:
score: 0
Accepted
time: 2ms
memory: 5544kb
Test #50:
score: 0
Accepted
time: 1ms
memory: 5472kb
Test #51:
score: 0
Accepted
time: 1ms
memory: 5500kb
Test #52:
score: 0
Accepted
time: 5ms
memory: 5664kb
Test #53:
score: 0
Accepted
time: 1ms
memory: 6264kb
Test #54:
score: 0
Accepted
time: 9ms
memory: 7248kb
Test #55:
score: 0
Accepted
time: 0ms
memory: 6156kb
Test #56:
score: 0
Accepted
time: 8ms
memory: 11468kb