QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#556847 | #6816. Invincible Hotwheels | ZepX_D | RE | 8ms | 52620kb | C++14 | 3.0kb | 2024-09-10 21:25:57 | 2024-09-10 21:25:57 |
Judging History
answer
#include <bits/stdc++.h>
#define fr first
#define se second
#define U unsigned
#define LL long long
#define pb push_back
#define pii pair<int,int>
#define __ __int128
using namespace std;
inline int read()
{
int x = 0;char ch = getchar();
while(!isdigit(ch)) ch = getchar();
while(isdigit(ch)) x = x*10+ch-48,ch = getchar();
return x;
}
const int N = 1e6+5;
int ed[N],edp[N/10],tr[N][26],cnt,fa[N],dfn[N],d,siz[N],len[N/10];
pii pre[N];
string s[N/10];
vector < int > ve[N];
void ins(int id)
{
int p = 0;
for (char c : s[id])
{
int x = c-'a';
if(!tr[p][x]) tr[p][x] = ++cnt;
p = tr[p][x];
}
ed[p] = id,edp[id] = p,pre[p].fr = id;
}
void build()
{
queue < int > q;
for (int i = 0;i < 26;i++)
if(tr[0][i]) q.push(tr[0][i]);
while(q.size())
{
int x = q.front();q.pop();
ve[fa[x]].pb(x);
if(!ed[x]) pre[x] = pre[fa[x]];
else pre[x].se = pre[fa[x]].fr;
for (int i = 0;i < 26;i++)
{
if(tr[x][i]) fa[tr[x][i]] = tr[fa[x]][i],q.push(tr[x][i]);
else tr[x][i] = tr[fa[x]][i];
}
}
}
void D(int x)
{
dfn[x] = ++d,siz[x] = 1;
for (int y : ve[x]) D(y),siz[x] += siz[y];
}
int c1[N],c2[N];
void A1(int i,int k){while(i <= d) c1[i] += k,i += i&-i;}
int Q1(int i)
{
int res = 0;
while(i) res += c1[i],i -= i&-i;
return res;
}
int W(int a,int b)
{
if(a == -1 || b == -1) return -1;
if(!a || !b) return a|b;
if(a == b) return a;
return -1;
}
int Len;
void A2(int i,int k){while(i) c2[i] = W(c2[i],k),i -= i&-i;}
int Q2(int i)
{
int res = 0;
while(i <= Len) res = W(res,c2[i]),i += i&-i;
return res;
}
struct seg
{
int l,r,id;
bool operator < (const seg &b) const
{return l == b.l ? r > b.r : l < b.l;}
}a[N<<1];
int b[N],bl[N];
bool vis[N];
int main()
{
int n = read(),ans = 0;
for (int i = 1;i <= n;i++)
cin >> s[i],ins(i),len[i] = s[i].size();
build(),D(0);
for (int i = 1;i <= n;i++)
{
int p = 0,ct1 = 0,ct2 = 0;
for (int j = 0;j < len[i];j++)
{
int x = s[i][j]-'a';p = tr[p][x];
if(j == len[i]-1) p = fa[p];
auto [p1,p2] = pre[p];
// cout << edp[p1] << " " << edp[p2] << '\n';
if(p2)
{
A1(dfn[fa[edp[p2]]],1);
if(p2 != i)
{
if(!vis[p2]) b[++ct2] = p2,vis[p2] = 1,bl[p2] = 0;
a[++ct1] = {j-len[p2]+2,j+1,p2};
}
}
if(p1)
{
if(p1 != i)
{
if(!vis[p1]) b[++ct2] = p1,vis[p1] = 1,bl[p1] = 0;
a[++ct1] = {j-len[p1]+2,j+1,p1};
}
}
}
sort(a+1,a+ct1+1);
Len = len[i];
for (int j = 1;j <= Len;j++) c2[j] = 0;
for (int j = 1;j <= ct1;j++)
{
bl[a[j].id] = W(bl[a[j].id],Q2(a[j].r));
A2(a[j].r,a[j].id);
}
for (int j = 1;j <= ct2;j++)
{
vis[b[j]] = 0;int x = edp[b[j]];
if(Q1(dfn[x]+siz[x]-1)-Q1(dfn[x]-1) || bl[b[j]] <= 0) continue;
ans++;
}
p = 0;
for (int j = 0;j < len[i];j++)
{
int x = s[i][j]-'a';p = tr[p][x];
if(j == len[i]-1) p = fa[p];
auto [p1,p2] = pre[p];
if(p2) A1(dfn[fa[edp[p2]]],-1);
}
// cout << ans << '\n';
}
printf("%d",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 46756kb
input:
8 wwwsoupunetcom wwwsoupunet soupunet punetcom punet pun net n
output:
6
result:
ok 1 number(s): "6"
Test #2:
score: 0
Accepted
time: 3ms
memory: 44776kb
input:
4 a aa aaa aaaa
output:
2
result:
ok 1 number(s): "2"
Test #3:
score: 0
Accepted
time: 3ms
memory: 42644kb
input:
5 bc cbcb cbca cbc c
output:
3
result:
ok 1 number(s): "3"
Test #4:
score: 0
Accepted
time: 0ms
memory: 44972kb
input:
5 c ccaaaa caaa aa ccaaa
output:
3
result:
ok 1 number(s): "3"
Test #5:
score: 0
Accepted
time: 5ms
memory: 44752kb
input:
5 bbabc b bbba abb abbbbab
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 0
Accepted
time: 0ms
memory: 45836kb
input:
100 cccabcacaccbcacbbcaaabacacbbccaacacacacccbcaacaaaaaacbcbcbcaccccaccccbaaaabcaacbacabaccaaccbcccbabbcbbbaaabbcbbabccacbbabaac acacacccbcaacaaaaaacbcbcbcaccccaccccbaaaabca babaaccbbacbcacca acbbcaaabacacbbccaacacacacccbcaacaaaaaacbcbcbcaccccaccccbaaaabcaacbacabaccaaccbcccbabbcbbbaaabbcbbabccacbbab...
output:
202
result:
ok 1 number(s): "202"
Test #7:
score: 0
Accepted
time: 3ms
memory: 44960kb
input:
100 zdbbzzedhbzpphphpzbpbcddb cbhzdbbzzedhbzpphphpzbpbcddbhcbzcphee dbbzzedhbzp dbbzzedhbzpphphpzbpbcddbhcbzcpheeczzzhhabeephbhe bhzdbbzzedhbzpphphpzbpbcddbhcbzcpheeczzzhhabeephbhepczcdhd dhbzpphphpzbpbcddbhcbzcpheeczzzhhabeephbhepczcdhdaczadhad ac dhbzpphphpzbpbcddbh zzedhbzpphphpzbpbcddbhcbzcpheec...
output:
190
result:
ok 1 number(s): "190"
Test #8:
score: 0
Accepted
time: 8ms
memory: 52620kb
input:
100 abhpappzeddpdbezhzdhezpzcpchbhecdebpahacbpebdeppdbpepbeaehphheepeb ebcaacpdhcbcddhbhczahpchbzbzdhzzaddaaebabbphdbbeepdhpeazeaaapbdezhdeeedcpadbzbhheadpezpheddcdcbeedcdbdzbddpacaedazheddhheezzdaebeheeccachcaapazdpchpzdbpzeaahhczcppchazdephdzzeaahzbzzeabhppapezeaahzhdbzhchabazcczpdhhdhedbdbebhecph...
output:
204
result:
ok 1 number(s): "204"
Test #9:
score: 0
Accepted
time: 0ms
memory: 46740kb
input:
19 babaada aabaadabbb ccb aa abaada ccbaadba ababaadac baad db ababaadaccaaa aadb aabaabaadabbba baebcababaadaccaaaababaa abaadab baebcababaadaccaaa adba aababaadacb aadbba baebcababaadaccaaaacbc
output:
13
result:
ok 1 number(s): "13"
Test #10:
score: 0
Accepted
time: 3ms
memory: 48792kb
input:
23 aababa ababaaaadd b aad aaad aaababaaaa aaa aadaacbababaaaadbab acbababaaaadba aba ababa acbab ababaa aaababaaa babaaaad ddaaababaaaadd aadaacbababaaa bababaaa babaaaadaba aacbababaaaa abaaaa dacbababaaaa aaaa
output:
29
result:
ok 1 number(s): "29"
Test #11:
score: -100
Runtime Error
input:
28519 abbbaaaaaaaacabcacabbaadabcbaa cdaaacabbabc addaaaaabbaabcbbaabbccaaaacabaadbacbbbbaaabacacabbaadabcbbaaaabaecaaaaaadbaabdaabbacbaaacbbabbacecbbacbaabcaababbaaabdbaaacbbeaabadaabbcabdaabaacaaaaadabaac bbaadabcdebacbaaabbcaabbbaab bbcabbbaadaabbbaabc accaccbababbaacaaacabcacabcaabababaaaca aaaa...