QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417467 | #8713. 不要玩弄字符串 | Afterlife | WA | 953ms | 155116kb | C++17 | 2.9kb | 2024-05-22 19:06:57 | 2024-05-22 19:06:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=18,M=8*18;
int n;
int v[1<<N];
int son[M][2],fail[M],ed[M],tot;
int f[M][1<<N];
int deg[M];
vector<int> g[M];
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
son[0][0]=son[0][1]=-1;
cin>>n;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
cin>>v[1<<i];
int x=0;
for(auto ch:s)
{
ch-='0';
if(son[x][ch]==-1)
{
son[x][ch]=++tot;
son[tot][0]=son[tot][1]=-1;
}
x=son[x][ch];
}
ed[x]|=1<<i;
}
for(int i=0;i<n;i++)
for(int S=0;S<(1<<n);S++)
if(S>>i&1)
v[S]+=v[S^(1<<i)];
queue<int> q;
for(int i=0;i<2;i++)
if(son[0][i]!=-1)
q.push(son[0][i]);
else
son[0][i]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
ed[x]|=ed[fail[x]];
for(int i=0;i<2;i++)
if(son[x][i]==-1)
son[x][i]=son[fail[x]][i];
else
fail[son[x][i]]=son[fail[x]][i],q.push(son[x][i]);
}
for(int S=(1<<n)-2;S>=0;S--)
{
priority_queue<pair<int,int> >Q;
for(int i=0;i<=tot;i++)
g[i].clear(),deg[i]=0;
for(int i=0;i<=tot;i++)
{
f[i][S]=-1e18;
for(int j=0;j<2;j++)
{
int ni=son[i][j];
int nS=S|ed[ni];
if(nS>S)
f[i][S]=max(f[i][S],v[nS^S]-f[ni][nS]);
else
g[ni].push_back(i),deg[i]++;
}
if(f[i][S]>0||!deg[i])
Q.push({-f[i][S],i});
}
// cerr<<"?"<<endl;
while(!Q.empty())
{
auto [dp,x]=Q.top();
Q.pop();
if(-dp!=f[x][S])
continue;
for(auto v:g[x])
{
if(-f[x][S]>0)
{
if(-f[x][S]>f[v][S])
f[v][S]=-f[x][S],Q.push({-f[v][S],v});
}
else
{
if(-f[x][S]>f[v][S])
f[v][S]=-f[x][S];
if(!--deg[v])
{
if(f[v][S]<=0)
Q.push({-f[v][S],v});
}
}
}
}
for(int i=0;i<=tot;i++)
if(deg[i])
f[i][S]=max(f[i][S],0ll);
}
int T;
cin>>T;
while(T--)
{
string s;
cin>>s;
int x=0,S=0;
for(auto ch:s)
{
ch-='0';
x=son[x][ch];
S|=ed[x];
}
cout<<f[x][S]<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3784kb
input:
3 11 1 0 2 000 3 2 0 1
output:
-1 3
result:
ok 2 number(s): "-1 3"
Test #2:
score: 0
Accepted
time: 363ms
memory: 155116kb
input:
18 111101 -31301 1101101 53902 101001 77190 1000110 -26277 01010 84183 0111001 -89769 100100 31681 00101 -33612 00101000 -25238 00111 -93542 0001101 45298 01100010 63521 11001001 84789 001011 89827 11010101 -12647 01011100 18677 010100 174 10011111 -73155 524286 0 1 00 10 01 11 000 100 010 110 001 1...
output:
0 0 0 0 0 0 0 0 0 0 0 77190 0 0 0 0 0 0 0 -77190 0 0 0 0 84183 77190 0 0 0 0 0 0 0 0 0 77190 0 0 0 31681 0 -77190 0 0 0 0 0 26277 0 53108 89827 84183 77190 77190 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 77190 77190 0 0 0 0 0 0 31681 -53108 0 0 -77190 -77190 0 0 0 0 0 0 0 0 0 0 26277 26277 0 0 53108 53108...
result:
ok 524286 numbers
Test #3:
score: 0
Accepted
time: 772ms
memory: 97740kb
input:
18 01 3 000 13 100 9 011 1 0101 6 0011 16 0100 12 0111 10 00100 15 1110 2 011111 18 100100 6 1010001 8 10101100 3 1001000 9 00100001 12 001100 8 10111111 2 524286 0 1 00 10 01 11 000 100 010 110 001 101 011 111 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 00000 100...
output:
10 0 10 10 -7 0 3 13 13 10 9 -7 8 0 3 0 13 13 20 13 8 12 0 9 -7 -7 8 8 2 0 3 0 0 0 16 13 13 13 33 8 8 13 9 8 0 12 0 9 9 9 -7 -7 -8 -9 17 8 8 8 2 2 16 0 3 0 0 0 -3 0 0 0 3 25 13 13 8 13 13 13 33 24 6 8 8 8 14 15 17 8 8 8 0 0 0 12 0 9 9 9 9 9 8 7 -16 0 -7 -7 -8 -8 0 -9 17 8 8 8 8 8 8 10 -7 2 2 2 16 16...
result:
ok 524286 numbers
Test #4:
score: -100
Wrong Answer
time: 953ms
memory: 99804kb
input:
18 01 12 100 19 110 17 011 8 0010 10 1101 20 1110 4 1011 19 00101 16 1000 10 001110 1 110010 7 0011111 11 01101011 8 0011111 9 10011110 11 011101 4 01011000 1 524286 0 1 00 10 01 11 000 100 010 110 001 101 011 111 0000 1000 0100 1100 0010 1010 0110 1110 0001 1001 0101 1101 0011 1011 0111 1111 00000 ...
output:
12 -6 11 11 0 6 11 8 9 11 1 26 8 10 11 2 10 8 9 9 9 11 1 16 26 27 8 1 12 10 11 2 0 2 10 10 10 8 9 -6 9 9 9 20 9 11 1 10 16 27 26 26 19 27 8 8 1 0 13 -1 12 10 11 2 0 2 0 0 0 2 10 10 10 10 10 4 10 8 9 0 -6 -10 9 9 9 9 9 9 20 9 9 24 9 11 1 10 10 10 6 16 20 27 26 22 26 27 19 0 19 27 8 16 8 8 1 1 0 0 13 ...
result:
wrong answer 440259th numbers differ - expected: '8', found: '7'