QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#178244 | #2343. First of Her Name | PetroTarnavskyi# | AC ✓ | 1044ms | 423388kb | C++17 | 2.2kb | 2023-09-13 20:04:36 | 2023-09-13 20:04:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b) - 1; i >= (a); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef unsigned long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int MAX = 1'000'447;
const int AL = 26;
struct node
{
int p;
int c;
int g[AL];
int nxt[AL];
int link;
void init()
{
c = -1;
p = -1;
fill(g, g + 26, -1);
fill(nxt, nxt + 26, -1);
link = -1;
}
};
struct AC
{
node A[MAX];
int sz;
void init()
{
A[0].init();
sz = 1;
}
int addStr(const string& s)
{
int x = 0;
FOR (i, 0, SZ(s))
{
int c = s[i] - 'A';
if (A[x].nxt[c] == -1)
{
A[x].nxt[c] = sz;
A[sz].init();
A[sz].c = c;
A[sz].p = x;
sz++;
}
x = A[x].nxt[c];
}
return x;
}
int go(int x, int c)
{
if (A[x].g[c] == -1)
{
if (A[x].nxt[c] != -1)
A[x].g[c] = A[x].nxt[c];
else if (x != 0)
A[x].g[c] = go(getLink(x), c);
else
A[x].g[c] = 0;
}
return A[x].g[c];
}
int getLink(int x)
{
if (A[x].link == -1)
{
if (x == 0 || A[x].p == 0) return 0;
A[x].link = go(getLink(A[x].p), A[x].c);
}
return A[x].link;
}
} A;
int c[MAX];
VI g[MAX];
VI g2[MAX];
int a[MAX];
int val[MAX];
void dfs(int v, int x)
{
x = A.go(x, c[v] - 'A');
val[x]++;
for (auto to : g[v])
dfs(to, x);
}
void dfs2(int v)
{
for (auto to : g2[v])
{
if (to != 0)
{
dfs2(to);
val[v] += val[to];
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
int n, k;
cin >> n >> k;
vector<string> v(k);
FOR (i, 0, n)
{
char ch;
int par;
cin >> ch >> par;
c[i + 1] = ch;
g[par].PB(i + 1);
}
A.init();
FOR (i, 0, k)
{
cin >> v[i];
reverse(ALL(v[i]));
a[i] = A.addStr(v[i]);
}
FOR (i, 0, A.sz)
{
g2[A.getLink(i)].PB(i);
}
dfs(1, 0);
dfs2(0);
FOR (i, 0, k)
{
cout << val[a[i]] << '\n';
}
cerr << double(clock()) / CLOCKS_PER_SEC << '\n';
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 9ms
memory: 50640kb
Test #2:
score: 0
Accepted
time: 3ms
memory: 50620kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 50816kb
Test #4:
score: 0
Accepted
time: 11ms
memory: 50660kb
Test #5:
score: 0
Accepted
time: 11ms
memory: 50800kb
Test #6:
score: 0
Accepted
time: 7ms
memory: 50568kb
Test #7:
score: 0
Accepted
time: 7ms
memory: 50592kb
Test #8:
score: 0
Accepted
time: 3ms
memory: 50744kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 50828kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 50752kb
Test #11:
score: 0
Accepted
time: 171ms
memory: 398636kb
Test #12:
score: 0
Accepted
time: 8ms
memory: 50684kb
Test #13:
score: 0
Accepted
time: 3ms
memory: 50680kb
Test #14:
score: 0
Accepted
time: 4ms
memory: 50720kb
Test #15:
score: 0
Accepted
time: 8ms
memory: 50812kb
Test #16:
score: 0
Accepted
time: 8ms
memory: 50704kb
Test #17:
score: 0
Accepted
time: 8ms
memory: 50704kb
Test #18:
score: 0
Accepted
time: 8ms
memory: 50808kb
Test #19:
score: 0
Accepted
time: 7ms
memory: 50772kb
Test #20:
score: 0
Accepted
time: 10ms
memory: 50704kb
Test #21:
score: 0
Accepted
time: 3ms
memory: 50724kb
Test #22:
score: 0
Accepted
time: 0ms
memory: 50800kb
Test #23:
score: 0
Accepted
time: 3ms
memory: 54184kb
Test #24:
score: 0
Accepted
time: 13ms
memory: 52924kb
Test #25:
score: 0
Accepted
time: 7ms
memory: 51060kb
Test #26:
score: 0
Accepted
time: 3ms
memory: 53068kb
Test #27:
score: 0
Accepted
time: 4ms
memory: 50856kb
Test #28:
score: 0
Accepted
time: 9ms
memory: 53120kb
Test #29:
score: 0
Accepted
time: 8ms
memory: 51072kb
Test #30:
score: 0
Accepted
time: 8ms
memory: 51388kb
Test #31:
score: 0
Accepted
time: 7ms
memory: 54836kb
Test #32:
score: 0
Accepted
time: 10ms
memory: 59516kb
Test #33:
score: 0
Accepted
time: 12ms
memory: 63148kb
Test #34:
score: 0
Accepted
time: 100ms
memory: 149644kb
Test #35:
score: 0
Accepted
time: 219ms
memory: 84888kb
Test #36:
score: 0
Accepted
time: 229ms
memory: 291396kb
Test #37:
score: 0
Accepted
time: 178ms
memory: 201036kb
Test #38:
score: 0
Accepted
time: 136ms
memory: 75632kb
Test #39:
score: 0
Accepted
time: 1044ms
memory: 423388kb
Test #40:
score: 0
Accepted
time: 1042ms
memory: 418336kb
Test #41:
score: 0
Accepted
time: 120ms
memory: 134320kb
Test #42:
score: 0
Accepted
time: 270ms
memory: 333096kb
Test #43:
score: 0
Accepted
time: 98ms
memory: 149908kb
Test #44:
score: 0
Accepted
time: 166ms
memory: 182868kb
Test #45:
score: 0
Accepted
time: 210ms
memory: 98424kb
Test #46:
score: 0
Accepted
time: 3ms
memory: 50676kb
Test #47:
score: 0
Accepted
time: 4ms
memory: 50688kb
Test #48:
score: 0
Accepted
time: 12ms
memory: 50664kb
Test #49:
score: 0
Accepted
time: 3ms
memory: 50692kb
Test #50:
score: 0
Accepted
time: 8ms
memory: 50808kb
Test #51:
score: 0
Accepted
time: 3ms
memory: 50888kb
Test #52:
score: 0
Accepted
time: 7ms
memory: 50852kb
Test #53:
score: 0
Accepted
time: 4ms
memory: 53148kb
Test #54:
score: 0
Accepted
time: 9ms
memory: 54404kb
Test #55:
score: 0
Accepted
time: 6ms
memory: 50728kb
Test #56:
score: 0
Accepted
time: 8ms
memory: 51280kb