Bob recently read about bitwise operations used in computers: AND, OR
and XOR. He have studied their properties and invented a new game.
Initially, Bob chooses integer m, bit depth of the game, which means
that all numbers in the game will consist of m bits. Then he asks
Peter to choose some m-bit number. After that, Bob computes the values
of n variables. Each variable is assigned either a constant m-bit
number or result of bitwise operation. Operands of the operation may
be either variables defined before, or the number, chosen by Peter.
After that, Peter’s score equals to the sum of all variable values.
Bob wants to know, what number Peter needs to choose to get the
minimum possible score, and what number he needs to choose to get the
maximum possible score. In both cases, if there are several ways to
get the same score, find the minimum number, which he can choose.
Input The first line contains two integers n and m, the number of
variables and bit depth, respectively (1 ≤ n ≤ 5000; 1 ≤ m ≤ 1000).
The following n lines contain descriptions of the variables. Each line
describes exactly one variable. Description has the following format:
name of a new variable, space, sign “:=”, space, followed by one of:
Binary number of exactly m bits. The first operand, space, bitwise
operation (“AND”, “OR” or “XOR”), space, the second operand. Each
operand is either the name of variable defined before or symbol ‘?’,
indicating the number chosen by Peter. Variable names are strings
consisting of lowercase Latin letters with length at most 10. All
variable names are different. Output In the first line output the
minimum number that should be chosen by Peter, to make the sum of all
variable values minimum possible, in the second line output the
minimum number that should be chosen by Peter, to make the sum of all
variable values maximum possible. Both numbers should be printed as
m-bit binary numbers.
位运算的各位是独立的,对每一位枚举是
0
还是
#include<cstdio>
#include<string>
#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> id;
int n,m,kind[5010],val[5010][1010],opt[5010],opta[5010],optb[5010],mxa[1010],mna[1010];
int calc(int p)
{
int ret=0;
for (int i=1;i<=n;i++)
{
if (kind[i]==1) val[i][p]=(val[opta[i]][p]&val[optb[i]][p]);
if (kind[i]==2) val[i][p]=(val[opta[i]][p]|val[optb[i]][p]);
if (kind[i]==3) val[i][p]=(val[opta[i]][p]^val[optb[i]][p]);
ret+=val[i][p];
}
return ret;
}
int main()
{
int x0,x1;
string s;
ios::sync_with_stdio(0);
cin>>n>>m;
id["?"]=0;
for (int i=1;i<=n;i++)
{
cin>>s;
id[s]=i;
cin>>s;
cin>>s;
if (s[0]=='0'||s[0]=='1')
{
kind[i]=0;
for (int j=1;j<=m;j++)
val[i][j]=s[j-1]-'0';
}
else
{
opta[i]=id[s];
cin>>s;
if (s[0]=='A') kind[i]=1;
else
{
if (s[0]=='O') kind[i]=2;
else kind[i]=3;
}
cin>>s;
optb[i]=id[s];
}
}
for (int j=1;j<=m;j++)
{
val[0][j]=0;
x0=calc(j);
val[0][j]=1;
x1=calc(j);
if (x0==x1) mxa[j]=mna[j]=0;
else
{
if (x0<x1)
{
mna[j]=0;
mxa[j]=1;
}
else
{
mna[j]=1;
mxa[j]=0;
}
}
}
for (int j=1;j<=m;j++) cout<<mna[j];
cout<<endl;
for (int j=1;j<=m;j++) cout<<mxa[j];
cout<<endl;
}