QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#551173 | #2343. First of Her Name | RngBased# | AC ✓ | 163ms | 357372kb | C++17 | 2.5kb | 2024-09-07 15:47:53 | 2024-09-07 15:47:53 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
const int S = 1'000'006;
const int sigma = 26;
const char base = 'A';
struct AhoCorasick
{
int ch[S][sigma] = {{}}, f[S] = {-1}, tag[S], mv[S][sigma], jump[S], cnt[S];
int idx = 0, t = -1;
vector<int> E[S], q;
pii o[S];
int insert(string &s)
{
int j = 0;
for (int i = 0; i < (int)s.size(); i++)
{
if (!ch[j][s[i] - base])
ch[j][s[i] - base] = ++idx;
j = ch[j][s[i] - base];
}
tag[j] = 1;
return j;
}
int next(int u, int c)
{
return u < 0 ? 0 : mv[u][c];
}
void dfs(int u)
{
o[u].F = ++t;
for (auto v : E[u]) dfs(v);
o[u].S = t;
}
void build()
{
int k = -1;
q.emplace_back(0);
while (++k < (int)q.size())
{
int u = q[k];
for (int v = 0; v < sigma; v++)
{
if (ch[u][v])
{
f[ch[u][v]] = next(f[u], v);
q.emplace_back(ch[u][v]);
}
mv[u][v] = (ch[u][v] ? ch[u][v] : next(f[u], v));
}
if (u) jump[u] = (tag[f[u]] ? f[u] : jump[f[u]]);
}
reverse(all(q));
for (int i = 1; i <= idx; i++)
E[f[i]].emplace_back(i);
dfs(0);
}
void prequery()
{
fill(cnt, cnt + idx + 1, 0);
}
void finalize()
{
for (int i : q)
if (f[i] > 0) cnt[f[i]] += cnt[i];
}
} ac;
int n, p[S], q, idx[S], state[S];
char c[S];
string s[S];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++)
cin >> c[i] >> p[i];
for (int i = 1; i <= q; i++)
{
cin >> s[i];
reverse(all(s[i]));
idx[i] = ac.insert(s[i]);
}
ac.build();
ac.prequery();
for (int i = 1; i <= n; i++)
{
state[i] = ac.next(state[p[i]], c[i] - base);
ac.cnt[state[i]]++;
}
ac.finalize();
for (int i = 1; i <= q; i++)
cout << ac.cnt[idx[i]] << '\n';
}
/*
10 5
S 0
Y 1
R 2
E 3
N 4
E 5
A 6
D 7
Y 7
R 9
RY
E
N
S
AY
*/
Details
Test #1:
score: 100
Accepted
time: 8ms
memory: 182824kb
Test #2:
score: 0
Accepted
time: 3ms
memory: 184932kb
Test #3:
score: 0
Accepted
time: 8ms
memory: 182848kb
Test #4:
score: 0
Accepted
time: 16ms
memory: 184948kb
Test #5:
score: 0
Accepted
time: 8ms
memory: 182976kb
Test #6:
score: 0
Accepted
time: 12ms
memory: 182832kb
Test #7:
score: 0
Accepted
time: 16ms
memory: 182980kb
Test #8:
score: 0
Accepted
time: 7ms
memory: 182880kb
Test #9:
score: 0
Accepted
time: 19ms
memory: 182972kb
Test #10:
score: 0
Accepted
time: 15ms
memory: 184892kb
Test #11:
score: 0
Accepted
time: 146ms
memory: 357372kb
Test #12:
score: 0
Accepted
time: 11ms
memory: 184960kb
Test #13:
score: 0
Accepted
time: 11ms
memory: 182828kb
Test #14:
score: 0
Accepted
time: 12ms
memory: 182884kb
Test #15:
score: 0
Accepted
time: 15ms
memory: 182904kb
Test #16:
score: 0
Accepted
time: 12ms
memory: 182820kb
Test #17:
score: 0
Accepted
time: 8ms
memory: 185024kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 184928kb
Test #19:
score: 0
Accepted
time: 3ms
memory: 184908kb
Test #20:
score: 0
Accepted
time: 4ms
memory: 182916kb
Test #21:
score: 0
Accepted
time: 16ms
memory: 184864kb
Test #22:
score: 0
Accepted
time: 7ms
memory: 184924kb
Test #23:
score: 0
Accepted
time: 11ms
memory: 183604kb
Test #24:
score: 0
Accepted
time: 20ms
memory: 185256kb
Test #25:
score: 0
Accepted
time: 15ms
memory: 182948kb
Test #26:
score: 0
Accepted
time: 16ms
memory: 183024kb
Test #27:
score: 0
Accepted
time: 3ms
memory: 184912kb
Test #28:
score: 0
Accepted
time: 11ms
memory: 185048kb
Test #29:
score: 0
Accepted
time: 19ms
memory: 187048kb
Test #30:
score: 0
Accepted
time: 12ms
memory: 184936kb
Test #31:
score: 0
Accepted
time: 7ms
memory: 185472kb
Test #32:
score: 0
Accepted
time: 8ms
memory: 189652kb
Test #33:
score: 0
Accepted
time: 23ms
memory: 187528kb
Test #34:
score: 0
Accepted
time: 70ms
memory: 188164kb
Test #35:
score: 0
Accepted
time: 77ms
memory: 196932kb
Test #36:
score: 0
Accepted
time: 163ms
memory: 305148kb
Test #37:
score: 0
Accepted
time: 91ms
memory: 247956kb
Test #38:
score: 0
Accepted
time: 94ms
memory: 191240kb
Test #39:
score: 0
Accepted
time: 147ms
memory: 329992kb
Test #40:
score: 0
Accepted
time: 132ms
memory: 325748kb
Test #41:
score: 0
Accepted
time: 68ms
memory: 188160kb
Test #42:
score: 0
Accepted
time: 157ms
memory: 290384kb
Test #43:
score: 0
Accepted
time: 70ms
memory: 190148kb
Test #44:
score: 0
Accepted
time: 113ms
memory: 191172kb
Test #45:
score: 0
Accepted
time: 116ms
memory: 191108kb
Test #46:
score: 0
Accepted
time: 7ms
memory: 184872kb
Test #47:
score: 0
Accepted
time: 11ms
memory: 182896kb
Test #48:
score: 0
Accepted
time: 12ms
memory: 182840kb
Test #49:
score: 0
Accepted
time: 4ms
memory: 180864kb
Test #50:
score: 0
Accepted
time: 11ms
memory: 182912kb
Test #51:
score: 0
Accepted
time: 11ms
memory: 182972kb
Test #52:
score: 0
Accepted
time: 12ms
memory: 184936kb
Test #53:
score: 0
Accepted
time: 8ms
memory: 183092kb
Test #54:
score: 0
Accepted
time: 12ms
memory: 185260kb
Test #55:
score: 0
Accepted
time: 7ms
memory: 182916kb
Test #56:
score: 0
Accepted
time: 16ms
memory: 184996kb