QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#417539 | #8713. 不要玩弄字符串 | Afterlife | WA | 1011ms | 157424kb | C++17 | 3.1kb | 2024-05-22 19:37:31 | 2024-05-22 19:37:31 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=18,M=8*18+10;
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;
vector<int> done(tot+1);
for(int i=0;i<=tot;i++)
g[i].clear(),deg[i]=0,done[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)
{
if(!deg[i])
Q.push({LLONG_MAX,i});
else
Q.push({-f[i][S],i});
}
}
// cerr<<"?"<<endl;
while(!Q.empty())
{
auto [dp,x]=Q.top();
Q.pop();
if(done[x])
continue;
done[x]=1;
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])
Q.push({LLONG_MAX,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];
}
// for(int i=0;i<n;i++)
// if(!(S>>i&1))
// cout<<i<<endl;
cout<<f[x][S]<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14132kb
input:
3 11 1 0 2 000 3 2 0 1
output:
-1 3
result:
ok 2 number(s): "-1 3"
Test #2:
score: -100
Wrong Answer
time: 1011ms
memory: 157424kb
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 0 0 0 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 0 0 0 0 0 0 89827 89827 0 96830 771...
result:
wrong answer 48th numbers differ - expected: '26277', found: '0'