QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#182820 | #2343. First of Her Name | PetroTarnavskyi | AC ✓ | 1086ms | 462384kb | C++17 | 2.2kb | 2023-09-18 16:18:17 | 2023-09-18 16:18:17 |
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 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 + AL, -1);
fill(nxt, nxt + AL, -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)
return A[x].link;
if (x == 0 || A[x].p == 0)
return 0;
return A[x].link = go(getLink(A[x].p), A[x].c);
}
} 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: 10ms
memory: 57292kb
Test #2:
score: 0
Accepted
time: 4ms
memory: 57084kb
Test #3:
score: 0
Accepted
time: 8ms
memory: 57288kb
Test #4:
score: 0
Accepted
time: 10ms
memory: 57084kb
Test #5:
score: 0
Accepted
time: 11ms
memory: 57192kb
Test #6:
score: 0
Accepted
time: 10ms
memory: 57284kb
Test #7:
score: 0
Accepted
time: 7ms
memory: 55220kb
Test #8:
score: 0
Accepted
time: 7ms
memory: 54980kb
Test #9:
score: 0
Accepted
time: 0ms
memory: 57140kb
Test #10:
score: 0
Accepted
time: 10ms
memory: 57188kb
Test #11:
score: 0
Accepted
time: 190ms
memory: 447268kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 57180kb
Test #13:
score: 0
Accepted
time: 3ms
memory: 55032kb
Test #14:
score: 0
Accepted
time: 11ms
memory: 57144kb
Test #15:
score: 0
Accepted
time: 11ms
memory: 55032kb
Test #16:
score: 0
Accepted
time: 7ms
memory: 57256kb
Test #17:
score: 0
Accepted
time: 7ms
memory: 57200kb
Test #18:
score: 0
Accepted
time: 3ms
memory: 57064kb
Test #19:
score: 0
Accepted
time: 3ms
memory: 57212kb
Test #20:
score: 0
Accepted
time: 8ms
memory: 57188kb
Test #21:
score: 0
Accepted
time: 0ms
memory: 57076kb
Test #22:
score: 0
Accepted
time: 5ms
memory: 57204kb
Test #23:
score: 0
Accepted
time: 7ms
memory: 60992kb
Test #24:
score: 0
Accepted
time: 4ms
memory: 60772kb
Test #25:
score: 0
Accepted
time: 4ms
memory: 57268kb
Test #26:
score: 0
Accepted
time: 9ms
memory: 59664kb
Test #27:
score: 0
Accepted
time: 4ms
memory: 57252kb
Test #28:
score: 0
Accepted
time: 4ms
memory: 59504kb
Test #29:
score: 0
Accepted
time: 4ms
memory: 57380kb
Test #30:
score: 0
Accepted
time: 8ms
memory: 58144kb
Test #31:
score: 0
Accepted
time: 17ms
memory: 59772kb
Test #32:
score: 0
Accepted
time: 7ms
memory: 68012kb
Test #33:
score: 0
Accepted
time: 17ms
memory: 71620kb
Test #34:
score: 0
Accepted
time: 128ms
memory: 200980kb
Test #35:
score: 0
Accepted
time: 215ms
memory: 88420kb
Test #36:
score: 0
Accepted
time: 267ms
memory: 293688kb
Test #37:
score: 0
Accepted
time: 151ms
memory: 232636kb
Test #38:
score: 0
Accepted
time: 117ms
memory: 79572kb
Test #39:
score: 0
Accepted
time: 1086ms
memory: 462384kb
Test #40:
score: 0
Accepted
time: 1065ms
memory: 458084kb
Test #41:
score: 0
Accepted
time: 116ms
memory: 176128kb
Test #42:
score: 0
Accepted
time: 239ms
memory: 381192kb
Test #43:
score: 0
Accepted
time: 113ms
memory: 200880kb
Test #44:
score: 0
Accepted
time: 143ms
memory: 230540kb
Test #45:
score: 0
Accepted
time: 203ms
memory: 99324kb
Test #46:
score: 0
Accepted
time: 3ms
memory: 57292kb
Test #47:
score: 0
Accepted
time: 4ms
memory: 57200kb
Test #48:
score: 0
Accepted
time: 7ms
memory: 57356kb
Test #49:
score: 0
Accepted
time: 3ms
memory: 57256kb
Test #50:
score: 0
Accepted
time: 4ms
memory: 57204kb
Test #51:
score: 0
Accepted
time: 3ms
memory: 59216kb
Test #52:
score: 0
Accepted
time: 7ms
memory: 57076kb
Test #53:
score: 0
Accepted
time: 6ms
memory: 59404kb
Test #54:
score: 0
Accepted
time: 4ms
memory: 59420kb
Test #55:
score: 0
Accepted
time: 4ms
memory: 57232kb
Test #56:
score: 0
Accepted
time: 7ms
memory: 57620kb