codeforces 778B 拆位运算

来源:互联网 发布:公安局网络通缉人员 编辑:程序博客网 时间:2024/06/03 20:07

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.

Example
Input
3 3
a := 101
b := 011
c := ? XOR b
Output
011
100
Input
5 1
a := 1
bb := 0
cx := ? OR a
d := ? XOR ?
e := d AND bb
Output
0
0
Note
In the first sample if Peter chooses a number 0112, then a = 1012, b = 0112, c = 0002, the sum of their values is 8. If he chooses the number 1002, then a = 1012, b = 0112, c = 1112, the sum of their values is 15.

For the second test, the minimum and maximum sum of variables a, bb, cx, d and e is 2, and this sum doesn’t depend on the number chosen by Peter, so the minimum Peter can choose is 0.

?是你选择的一个数,给你n个变量和变量的得到方式,让你构造出?,使得所有的变量和最大和所有变量和最小的 ?,输出最小的?
暴力拆位就行,每一位假设为0或者1,套进去得到结果比较就好

#include <bits/stdc++.h>using namespace std;string minn,maxx;int bit[5010][1010];int tot,tot1,n,m;map<string,int> mp;struct node{    int f[1010];    int a,op,b;}no[5010];int check(int id,int x){    no[0].f[id]=x;    int res=0;    for(int i=1;i<=n;i++)    {        int ff=0;        int zuo=no[i].a,you=no[i].b;        if(no[i].op==1) if(no[zuo].f[id]!=no[you].f[id]) no[i].f[id]=1,ff=1;        if(no[i].op==2) if(no[zuo].f[id]||no[you].f[id]) no[i].f[id]=1,ff=1;        if(no[i].op==3) if(no[zuo].f[id]&&no[you].f[id]) no[i].f[id]=1,ff=1;        if(!ff) no[i].f[id]=0;        if(no[i].f[id]) res++;    }    return res;}int main(){    cin>>n>>m;    mp["?"]=0;    for(int i=1;i<=n;i++)    {        string s,s1,s2;        cin>>s>>s1>>s2;        mp[s]=i;        if(s2[0]>='0'&&s2[0]<='9')            for(int j=0;j<m;j++)                no[i].f[j]=s2[j]-'0';        else         {            string s3,s4;            cin>>s3>>s4;            no[i].a=mp[s2];            if(s3[0]=='X') no[i].op=1;            if(s3[0]=='O') no[i].op=2;            if(s3[0]=='A') no[i].op=3;            no[i].b=mp[s4];        }    }    for(int i=0;i<m;i++)    {        if(check(i,1)>check(i,0)) maxx+='1',minn+='0';        else maxx+='0',minn+='1';    }    cout<<minn<<endl;    cout<<maxx<<endl;}
原创粉丝点击