QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244336 | #7697. Impartial Strings | SolitaryDream | AC ✓ | 91ms | 3732kb | C++17 | 3.1kb | 2023-11-08 23:14:03 | 2023-11-08 23:14:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,s,t) for(int i=(s),_t=(t); i<=_t; ++i)
#define DOR(i,s,t) for(int i=(s),_t=(t); i>=_t; --i)
const int N=1005;
int tag[26];
int ch[N][26];
int fail[N];
int tid;
int ins() {
string s;
cin >> s;
int p=0;
for(auto c: s) {
c-='a';
if(!ch[p][c]) ch[p][c]=++tid;
p=ch[p][c];
}
return p;
}
int Q[N*N*2];
int dis[N];
int mark[N];
int cnt[N];
int val[N];
vector<int> g[N];
void dfs(int x)
{
for(auto v:g[x])
{
val[v]+=val[x];
dfs(v);
}
}
void solve() {
string cc;
cin >> cc;
for(int i=0;i<=tid;i++)
fail[i]=0,memset(ch,0,sizeof(ch));
memset(tag,0,sizeof(tag));
for(auto c: cc) tag[c-'a']=1;
tid=0;
int p[2];
p[0]=ins();
p[1]=ins();
int l=1,r=0;
FOR(i,0,25) if(tag[i] && ch[0][i]) Q[++r]=ch[0][i];
while(l<=r) {
int p=Q[l++];
FOR(i,0,25) if(tag[i]) {
if(ch[p][i]) {
Q[++r]=ch[p][i];
fail[ch[p][i]]=ch[fail[p]][i];
} else ch[p][i]=ch[fail[p]][i];
}
}
FOR(i,0,tid)
g[i].clear(),val[i]=(i==p[0])-(i==p[1]);
FOR(i,1,tid)
g[fail[i]].push_back(i);
dfs(0);
// FOR(i,0,tid) FOR(j,0,25) if(tag[j]) cerr << i << ' ' << ch[i][j] << '\n';
// cerr << p[0] << ' ' << p[1] << endl;
int res=0,flag=0;
vector<vector<int> >key;
key.resize(2);
FOR(_,0,1) {
FOR(i,0,tid) dis[i]=-1e9,mark[i]=0,cnt[i]=0;
// memset(mark,0,sizeof(mark));
// memset(cnt,0,sizeof(cnt));
l=1,r=0;
dis[0]=0;
Q[++r]=0;
mark[0]=1;
while(l<=r) {
int x=Q[l++];
mark[x]=0;
++cnt[x];
if(cnt[x]>10) {
continue;
}
FOR(i,0,25) if(tag[i]) {
int y=ch[x][i];
if(dis[x]+(_==0?1:-1)*val[y]>dis[y]) {
// cerr << dis[y] << endl;
dis[y]=dis[x]+(_==0?1:-1)*val[y];
if(!mark[y]) {
Q[++r]=y;
mark[y]=1;
}
}
}
}
FOR(i,0,tid) if(cnt[i]>10) {
key[_].push_back(i);
}
if(key[_].size())
res++;
}
for(int _=0;_<2;_++)
{
l=1,r=0;
for(int i=0;i<=tid;i++)
cnt[i]=0;
for(auto x:key[_])
Q[++r]=x,cnt[x]=1;
while(l<=r)
{
int x=Q[l++];
for(int i=0;i<26;i++)
if(tag[i]&&ch[x][i])
{
if(cnt[ch[x][i]])
continue;
cnt[ch[x][i]]=1;
Q[++r]=ch[x][i];
}
}
for(auto x:key[_^1])
if(cnt[x])
flag=1;
}
cout << (res<2 || !flag) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while(T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3552kb
input:
3 ab ab ba abc ab ba cz cczz zzcc
output:
1 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 3580kb
input:
7 d d d d dd d d d dd z zzzzzzzzzzzz zzz a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1 1 1 1 1 1 1
result:
ok 7 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
10 ab aaaaaabbabbbbb bbbbba ab aaaaaabbabbbbb baaaaaa ab aaaaaabbabbbbb bbbba ab aaaaaabbabbbbb bbba ab aaaaaabbabbbbb baaa ab aaaaaabbabbbbb baaaaa ab aaaaaabbabbbbb baa ab aaaaaabbabbbbb baaaa ab aaaaaabbabbbbb baaaaaaa ab aaaaaabbabbbbb bbbbbba
output:
1 1 1 1 1 1 1 1 0 0
result:
ok 10 lines
Test #4:
score: 0
Accepted
time: 87ms
memory: 3724kb
input:
50 az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 50 lines
Test #5:
score: 0
Accepted
time: 42ms
memory: 3600kb
input:
50 az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 50 lines
Test #8:
score: 0
Accepted
time: 42ms
memory: 3612kb
input:
50 az zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
ok 50 lines
Test #9:
score: 0
Accepted
time: 87ms
memory: 3732kb
input:
50 az zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 50 lines
Test #10:
score: 0
Accepted
time: 44ms
memory: 3628kb
input:
50 az aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa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bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
ok 50 lines
Test #13:
score: 0
Accepted
time: 41ms
memory: 3656kb
input:
50 icp cppcpccpiccccccppiiiipicpcipicciciciccipcpcccpipcpppcipicipiipppipccppppiiippcpcpiciipcpipiipcccpiciiiicpipipcpcpicccpicppcciicippipcpicippcipppcppciiccicipccppccpccpipciipiippciippppicccciiicpciicpcppcipcippcppccipipcppcccppppciiccpippcipiipciccciccccccppipcccccicicpcppippciiccpiccppppccippc...
output:
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 50 lines
Test #14:
score: 0
Accepted
time: 51ms
memory: 3716kb
input:
50 oks kksksoskssksskssksoksoskkkoooskkskskkkkkkokssksokoskkksooskkkokkkksosssoksskokokoksooosksskkkosssosoooskssokkooossskssssosossksoookkss koskskkkksssskkoossosskkskoskokokooksokossssokookkokosssoosoosskksokskskokkokokosookksokksokokooskokokkkkookosokooosoosksksoskkoookoskksksoskskssskoskskkkkskk...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 50 lines
Test #15:
score: 0
Accepted
time: 57ms
memory: 3708kb
input:
50 swxyrcjgp jjxypwcpgjxxcwccgwpsyxgjpwrgwpxwywwwxsxsyyygypwjrwpgcxgcgpgrrjwgjgpjsswjwpsjyxxyxscwcsxysywwyspgrspjsxypyyxsgyssswrcrpjgrygxwgjcyppysycpxyjjjgrgyxcwyywsrpyccrryprcjwgswrcjjgjxwyrpsgjpccrjwsjwxgjpsjssjycxxjprycxjxrpwgxcpjycwxyrgpppwwjjjcjrjxssxwyryxjyjjyjcjcsjsccpxxgscsspsryrjswpcwjgspsx...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
result:
ok 50 lines
Test #16:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
1 az aaaaza azaaaa
output:
0
result:
ok single line: '0'
Test #17:
score: 0
Accepted
time: 81ms
memory: 3648kb
input:
50 ab aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
result:
ok 50 lines