QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#236487 | #2343. First of Her Name | PetroTarnavskyi | AC ✓ | 1051ms | 463912kb | C++17 | 2.3kb | 2023-11-04 00:15:32 | 2023-11-04 00:15:32 |
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, a, b) for(int i = (a) - 1; i >= (b); 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 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
{
vector<Node> a;
int sz;
void init(int n)
{
a.resize(n);
a[0].init();
sz = 1;
}
int addStr(const string& s)
{
int v = 0;
FOR (i, 0, SZ(s))
{
int c = s[i] - 'A'; // change to [0 AL)
if (a[v].nxt[c] == -1)
{
a[v].nxt[c] = sz;
a[sz].init();
a[sz].c = c;
a[sz].p = v;
sz++;
}
v = a[v].nxt[c];
}
return v;
}
int go(int v, int c)
{
if (a[v].g[c] != -1)
return a[v].g[c];
if (a[v].nxt[c] != -1)
a[v].g[c] = a[v].nxt[c];
else if (v != 0)
a[v].g[c] = go(getLink(v), c);
else
a[v].g[c] = 0;
return a[v].g[c];
}
int getLink(int v)
{
if (a[v].link != -1)
return a[v].link;
if (v == 0 || a[v].p == 0)
return 0;
return a[v].link=go(getLink(a[v].p), a[v].c);
}
};
const int N = 1'000'447;
AC A;
int c[N];
VI g[N];
VI g2[N];
int a[N];
int val[N];
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);
A.init(N);
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);
}
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: 23ms
memory: 269348kb
Test #2:
score: 0
Accepted
time: 16ms
memory: 267108kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 267276kb
Test #4:
score: 0
Accepted
time: 15ms
memory: 267176kb
Test #5:
score: 0
Accepted
time: 20ms
memory: 267068kb
Test #6:
score: 0
Accepted
time: 7ms
memory: 265060kb
Test #7:
score: 0
Accepted
time: 15ms
memory: 265064kb
Test #8:
score: 0
Accepted
time: 11ms
memory: 267300kb
Test #9:
score: 0
Accepted
time: 19ms
memory: 264988kb
Test #10:
score: 0
Accepted
time: 20ms
memory: 267372kb
Test #11:
score: 0
Accepted
time: 194ms
memory: 384992kb
Test #12:
score: 0
Accepted
time: 19ms
memory: 267364kb
Test #13:
score: 0
Accepted
time: 8ms
memory: 267172kb
Test #14:
score: 0
Accepted
time: 16ms
memory: 267160kb
Test #15:
score: 0
Accepted
time: 23ms
memory: 267304kb
Test #16:
score: 0
Accepted
time: 19ms
memory: 267220kb
Test #17:
score: 0
Accepted
time: 11ms
memory: 267216kb
Test #18:
score: 0
Accepted
time: 16ms
memory: 267212kb
Test #19:
score: 0
Accepted
time: 31ms
memory: 264972kb
Test #20:
score: 0
Accepted
time: 19ms
memory: 267200kb
Test #21:
score: 0
Accepted
time: 16ms
memory: 267076kb
Test #22:
score: 0
Accepted
time: 15ms
memory: 267212kb
Test #23:
score: 0
Accepted
time: 12ms
memory: 268592kb
Test #24:
score: 0
Accepted
time: 24ms
memory: 266140kb
Test #25:
score: 0
Accepted
time: 23ms
memory: 267576kb
Test #26:
score: 0
Accepted
time: 19ms
memory: 267732kb
Test #27:
score: 0
Accepted
time: 15ms
memory: 267868kb
Test #28:
score: 0
Accepted
time: 16ms
memory: 267784kb
Test #29:
score: 0
Accepted
time: 36ms
memory: 267256kb
Test #30:
score: 0
Accepted
time: 16ms
memory: 267828kb
Test #31:
score: 0
Accepted
time: 31ms
memory: 269556kb
Test #32:
score: 0
Accepted
time: 32ms
memory: 269908kb
Test #33:
score: 0
Accepted
time: 12ms
memory: 270876kb
Test #34:
score: 0
Accepted
time: 116ms
memory: 349268kb
Test #35:
score: 0
Accepted
time: 188ms
memory: 289392kb
Test #36:
score: 0
Accepted
time: 255ms
memory: 297332kb
Test #37:
score: 0
Accepted
time: 168ms
memory: 344528kb
Test #38:
score: 0
Accepted
time: 130ms
memory: 289728kb
Test #39:
score: 0
Accepted
time: 1051ms
memory: 463912kb
Test #40:
score: 0
Accepted
time: 1022ms
memory: 461764kb
Test #41:
score: 0
Accepted
time: 122ms
memory: 336280kb
Test #42:
score: 0
Accepted
time: 208ms
memory: 364724kb
Test #43:
score: 0
Accepted
time: 99ms
memory: 349320kb
Test #44:
score: 0
Accepted
time: 140ms
memory: 382332kb
Test #45:
score: 0
Accepted
time: 197ms
memory: 313304kb
Test #46:
score: 0
Accepted
time: 16ms
memory: 267304kb
Test #47:
score: 0
Accepted
time: 8ms
memory: 267196kb
Test #48:
score: 0
Accepted
time: 11ms
memory: 267076kb
Test #49:
score: 0
Accepted
time: 11ms
memory: 267308kb
Test #50:
score: 0
Accepted
time: 19ms
memory: 267376kb
Test #51:
score: 0
Accepted
time: 15ms
memory: 267680kb
Test #52:
score: 0
Accepted
time: 7ms
memory: 265328kb
Test #53:
score: 0
Accepted
time: 28ms
memory: 267432kb
Test #54:
score: 0
Accepted
time: 23ms
memory: 268140kb
Test #55:
score: 0
Accepted
time: 16ms
memory: 267380kb
Test #56:
score: 0
Accepted
time: 39ms
memory: 267708kb