为什么这段代码只有 #3 #9 #12 AC,其余的全 WA?
#include<bits/stdc++.h>
using namespace std;
vector<string>split(string x)
{
string tmp="";
vector<string>ret;
for(int i=0;i<x.length();++i)
{
if(x[i]==' ')
{
if(tmp.length())
ret.push_back(tmp);
tmp="";
}
else tmp+=x[i];
}
if(tmp.length())ret.push_back(tmp);
return ret;
}
//& -> -1 ; | -> -2 ; ! -> -3
vector<int>trans(vector<string>str)
{
vector<int>ret;
for(int i=0;i<str.size();++i)
{
if(str[i]==string("&"))ret.push_back(-1);
else if(str[i]==string("|"))ret.push_back(-2);
else if(str[i]==string("!"))ret.push_back(-3);
else
{
int x=0;
for(int j=1;j<str[i].length();++j)
x=x*10+str[i][j]-48;
ret.push_back(x);
}
}
return ret;
}
struct var
{
int val;
int lc,rc;
bool qf;//qv fan
var(){qf=0;}
var(int v,int l,int r)
{val=v;lc=l;rc=r;qf=0;}
};var tree[500005];
int cnt;int reg[500005];
bool nuse[5000005];//not useful
//& -> -1 ; | -> -2
int build(vector<int>post)
{
stack<int>son;
for(int i=0;i<post.size();++i)
{
if(post[i]>0)
{
tree[++cnt]=var(post[i],0,0);
son.push(cnt);
reg[post[i]]=cnt;
}
else if(post[i]==-3)
tree[son.top()].qf=true;
else
{
int rch=son.top();son.pop();
int lch=son.top();son.pop();
tree[++cnt]=var(post[i],lch,rch);
son.push(cnt);
}
}
return son.top();
}
bool value[500005];
void mark(int bt)
{
if(tree[bt].val>0)
{
nuse[tree[bt].val]=true;
return;
}
mark(tree[bt].lc);
mark(tree[bt].rc);
}
bool handle(int bt)
{
if(tree[bt].val>0)
return value[tree[bt].val];
bool lres=handle(tree[bt].lc);
bool rres=handle(tree[bt].rc);
if(tree[bt].val==-1)//&
{
if(!lres)mark(tree[bt].rc);
if(!rres)mark(tree[bt].lc);
if(tree[bt].qf)return !(lres and rres);
else return lres and rres;
}
else if(tree[bt].val==-2)//|
{
if(lres)mark(tree[bt].rc);
if(rres)mark(tree[bt].lc);
if(tree[bt].qf)return !(lres or rres);
else return lres or rres;
}
}
int read()
{
int x=0;char ch=' ';
while(!isdigit(ch))
ch=getchar();
while(isdigit(ch))
{
x=x*10+ch-48;
ch=getchar();
}
return x;
}
bool reab()
{
char ch=' ';
while(!isdigit(ch))
ch=getchar();
return ch-48;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
string expr;getline(cin,expr);
vector<string>suf=split(expr);
vector<int>suffix=trans(suf);
int root=build(suffix);
int n=read();
for(int i=1;i<=n;++i)
value[i]=reab();
bool result=handle(root);
int q=read();while(q--)
{
int node=read();
putchar(nuse[node]+48);
putchar('\n');
}
return 0;
}