作业CF778B(hsz_dalao)

7 篇文章 0 订阅

B. Bitwise Formula
time limit per test3 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

Examples
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.

拆位后一位一位的令每一位分别为0和1,然后扫一遍取较大值……

#include <cstdio>
#include <map>
#include <iostream>
#include <string>
#define maxn 5000+10
using namespace std;
map <string , int> id;
int g[maxn],n,m,bo[maxn],op[maxn],val[maxn][maxn],ans[maxn][2];
string s[maxn],tmp,x[maxn],y[maxn];

int cal(int p,int q)
{
    int tmp=0;
    for (int i=1;i<=n;i++)
    {
        int k1,k2;
        if (!op[i]) 
        {
            tmp+=g[i]=val[i][p];
            continue;
        }
        k1=x[i][0]=='?'?q:g[id[x[i]]];
        k2=y[i][0]=='?'?q:g[id[y[i]]];
        if (op[i]==1) tmp+=g[i]=k1&k2;
        if (op[i]==2) tmp+=g[i]=k1|k2;
        if (op[i]==3) tmp+=g[i]=k1^k2;
    }
    return tmp;
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
      {
        cin>>s[i]>>tmp>>tmp;
        id[ s[i] ]=i;
        if (tmp[0]>='0'&&tmp[0]<='9') 
            for (int j=0;j<m;j++) 
              val[i][j]=tmp[j]-'0';
        else
        {
            x[i]=tmp;
            cin>>tmp>>y[i];
            if (tmp[0]=='A') op[i]=1;
            if (tmp[0]=='O') op[i]=2;
            if (tmp[0]=='X') op[i]=3;
        }
      }
    for (int i=0;i<m;i++)
    {
        int t1=cal(i,0);
        int t2=cal(i,1);
        if (t1>t2)ans[i][1]=1;
        else if (t1<t2)ans[i][0]=1;
    }
    for (int k=1;k>=0;k--)
    {
        for (int i=0;i<m;i++) printf("%d",ans[i][k]);
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值