QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#603193 | #8713. 不要玩弄字符串 | Celebrate | WA | 326ms | 168544kb | C++14 | 3.7kb | 2024-10-01 15:10:14 | 2024-10-01 15:10:15 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,x,y) for(int i=x;i<=y;i++)
#define dwn(i,x,y) for(int i=x;i>=y;i--)
#define ll long long
using namespace std;
template<typename T>inline void qr(T &x){
x=0;int f=0;char s=getchar();
while(!isdigit(s))f|=s=='-',s=getchar();
while(isdigit(s))x=x*10+s-48,s=getchar();
x=f?-x:x;
}
int cc=0,buf[31];
template<typename T>inline void qw(T x){
if(x<0)putchar('-'),x=-x;
do{buf[++cc]=int(x%10);x/=10;}while(x);
while(cc)putchar(buf[cc--]+'0');
}
const int N=160,inf=1e9;
int n,len;char s[N];
int cnt=1,ch[N][2],fail[N],pos[N],statu[N],statu2[N];
int dp[1<<18][N];
vector<int>e[N];int deg[N];
int d[N];
int value[1<<18];
bool v[N];
void add(int id){
int p=1;
rep(i,1,len){
int w=s[i]-'0';
if(!ch[p][w])ch[p][w]=++cnt;
p=ch[p][w];
}
pos[id]=p;
statu[p]|=1<<(id-1);
statu2[p]=statu[p];
}
void getfail(){
rep(i,0,1)ch[0][i]=1;
queue<int>q;q.push(1);
while(q.size()){
int x=q.front();q.pop();
int fa=fail[x];
statu2[x]|=statu2[fa];
rep(i,0,1){
int y=ch[x][i];
if(!y){ch[x][i]=ch[fa][i];continue;}
fail[y]=ch[fa][i];
q.push(y);
}
}
}
void solve(){
qr(n);
int lim=(1<<n)-1;
rep(i,1,n){
scanf("%s",s+1);
len=strlen(s+1);
add(i);
int v;qr(v);
rep(j,0,lim)if(j>>(i-1)&1)value[j]+=v;
}
getfail();
rep(i,1,cnt){
rep(w,0,1){
if(ch[i][w]){
e[ch[i][w]].push_back(i);
}
}
}
dwn(ki,lim-1,0){
rep(i,1,cnt){
d[i]=-inf;
deg[i]=2;
v[i]=0;
}
queue<int>q;
priority_queue<pair<int,int> >Q;
rep(x,1,cnt){
if((statu[x]|ki)!=ki&&((statu[x]^statu2[x])|ki)==ki){
for(int y:e[x])if((statu2[y]|ki)==ki){
if(d[y]<value[statu[x]^(statu[x]&ki)]-dp[ki|statu[x]][x]){
d[y]=value[statu[x]^(statu[x]&ki)]-dp[ki|statu[x]][x];
Q.push({d[y],y});
}
if(!--deg[y])q.push(y);
}
}
}
while(q.size()||Q.size()){
if(q.size()){
while(q.size()){
int x=q.front();q.pop();
if(v[x])continue;
v[x]=1;
dp[ki][x]=d[x];
for(int y:e[x])if((statu2[y]|ki)==ki&&!v[y]){
if(d[y]<-d[x]){
d[y]=-d[x];
Q.push({d[y],y});
}
if(!--deg[y])q.push(y);
}
}
}
else{
if(Q.size()){
int x=Q.top().second;Q.pop();
if(v[x])continue;
if(d[x]<=0)break;
q.push(x);
}
}
}
}
// cout<<cnt<<endl;
// rep(i,1,cnt){
// cout<<ch[i][0]<<" "<<ch[i][1]<<" "<<statu[i]<<" "<<statu2[i]<<endl;
// }
// rep(i,0,lim){
// rep(j,1,cnt){
// qw(dp[i][j]);putchar(' ');
// }
// puts("");
// }
int q;qr(q);
while(q--){
scanf("%s",s+1);
len=strlen(s+1);
int p=1,now=statu2[1];
rep(i,1,len){
int w=s[i]-'0';
p=ch[p][w];
now|=statu2[p];
}
qw(dp[now][p]),puts("");
}
}
int main(){
int tt;tt=1;
while(tt--)solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 5764kb
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: 326ms
memory: 168544kb
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 45298 0 84183 0 0 0 0 0 0 0 0 0 0 77190 0 0 0 31681 0 0 0 0 0 0 45298 71575 0 0 89827 84183 0 0 -45298 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 0 0 0 0 0 45298 0 0 53902 0 0 0 0 45298 45298 71575 71575 0 0 0 0 89827 89827 0 ...
result:
wrong answer 23rd numbers differ - expected: '0', found: '45298'