题意:

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 mbits. 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:

  1. Binary number of exactly m bits.
  2. 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

思路:

1.由于是位操作,所以各个位之间的运算结果相互独立。因此一位一位割裂来计算,选取每一位取“0”还是取“1”更好一点。对于每一位来讲,n个数的这一位上的“1”的个数的和越多,则所有数的和越大。另外,如果取“0”和取“1”能获得相同的结果,则取“0”即可。

2.学习使用bitset用法。

实现:

 #include <iostream>
#include <cstdio>
#include <bitset>
#include <map>
using namespace std; int n, m, res[][];
bitset<> bs[][];
map<string, int> mp;
int main()
{
mp["?"] = ;
bs[][].set();
cin >> n >> m;
for (int i = ; i < n; i++)
{
string x, tmp, y, op, z;
cin >> x >> tmp >> y;
mp[x] = i + ;
if (y[] == '' || y[] == '')
{
for (int j = ; j < m; j++)
{
bs[i + ][].set(j, y[j] - '');
bs[i + ][].set(j, y[j] - '');
}
}
else
{
cin >> op >> z;
for (int j = ; j <= ; j++)
{
if (op == "AND")
{
bs[i + ][j] = bs[mp[y]][j] & bs[mp[z]][j];
}
else if (op == "OR")
{
bs[i + ][j] = bs[mp[y]][j] | bs[mp[z]][j];
}
else
{
bs[i + ][j] = bs[mp[y]][j] ^ bs[mp[z]][j];
}
}
}
for (int j = ; j < m; j++)
{
for (int k = ; k <= ; k++)
res[j][k] += bs[i + ][k][j];
}
}
for (int i = ; i < m; i++)
{
cout << (res[i][] > res[i][] ? : );
}
puts("");
for (int i = ; i < m; i++)
{
cout << (res[i][] >= res[i][] ? : );
}
return ;
}

CF778B(round 402 div.2 E) Bitwise Formula的更多相关文章

  1. 【推导】【贪心】Codeforces Round #402 (Div. 2) E. Bitwise Formula

    按位考虑,每个变量最终的赋值要么是必为0,要么必为1,要么和所选定的数相同,记为2,要么和所选定的数相反,记为3,一共就这四种情况. 可以预处理出来一个真值表,然后从前往后推导出每个变量的赋值. 然后 ...

  2. Codeforces Round #402 (Div. 2)

    Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...

  3. Codeforces Round #402 (Div. 2) A+B+C+D

    Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...

  4. Codeforces Round #402 (Div. 2) A,B,C,D,E

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. CodeForces Round #402 (Div.2) A-E

    2017.2.26 CF D2 402 这次状态还算能忍吧……一路不紧不慢切了前ABC(不紧不慢已经是在作死了),卡在D,然后跑去看E和F——卧槽怎么还有F,早知道前面做快点了…… F看了看,不会,弃 ...

  6. Codeforces Round #402 (Div. 1)

    A题卡壳了,往离线倒着加那方面想了会儿,后来才发现方向错了,二十多分钟才过掉,过了B后做D,想法好像有点问题,最后只过两题,掉分了,差一点回紫. AC:AB Rank:173 Rating:2227- ...

  7. Codeforces Round#402(Div.1)掉分记+题解

    哎,今天第一次打div1 感觉头脑很不清醒... 看到第一题就蒙了,想了好久,怎么乱dp,倒过来插之类的...突然发现不就是一道sb二分吗.....sb二分看了二十分钟........ 然后第二题看了 ...

  8. Codeforces Round #402 (Div. 2) A B C sort D二分 (水)

    A. Pupils Redistribution time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. 【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

    暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=1 ...

随机推荐

  1. c语言基本函数

    1. 用宏定义写出swap(x,y) #define swap(x, y) x = x + y; y = x - y; x = x - y; 2.数组a[N],存放了1至N-1个数,其中某个数重复一次 ...

  2. 使用pt-query-digest进行日志分析

    使用pt-query-digest sudo apt install percona-toolkit 也可以到官网 https://www.percona.com/downloads/percona- ...

  3. poj 2771 Guardian of Decency 解题报告

    题目链接:http://poj.org/problem?id=2771 题目意思:有一个保守的老师要带他的学生来一次短途旅行,但是他又害怕有些人会变成情侣关系,于是就想出了一个方法: 1.身高差距   ...

  4. html5--6-16 CSS3中的文字与字体

    html5--6-16 CSS3中的文字与字体 中文字体包很大,少量字体的话可以有其它方法. 有字库-首页-全球第一中文web font(在线字体)服务平台.web font.webfont.在线字体 ...

  5. html5--6-23 CSS3中的文字与字体

    html5--6-23 CSS3中的文字与字体 text-overflow 设置是否使用一个省略标记(...)标示对象内文本的溢出 clip: 默认值当对象内文本溢出时不显示省略标记(...),而是将 ...

  6. WAS:修改jsp编译器用JDK5.0

      问题现象: 今天有现场反映,访问应用的个别页面报错,报错内容如下: 于是先查看其他现场,都是好的:根据报错信息,提示的意思是jsp解析不了. 结合上面2个情况,排除代码问题,应该是现场WAS环境问 ...

  7. NSArray是强引用容器

    经常比较疑惑NSArray.NSDictionary.NSSet这几个对象容器管理对象所采用的方式是“强引用”还是“弱引用”. 通过简单的命令行程序得到的结论是“NSArray.NSDictionar ...

  8. robotframework:appium切换webview后,在webview里滑动屏幕

    问题: 在用robot写手机淘宝app的自动化时,打开手机淘宝后,点击天猫国际,跳转到天猫国际页面,天猫国际页面是H5, 需要切换到对应的webview,切换到webview后,点击美妆菜单,跳转到美 ...

  9. DDK编写64位驱动时加入x64汇编的方法

    上篇讲了如何在编写x64应用程序时加入x64汇编,这里来说说如何在编写x64驱动时加入x64汇编. 一.在asm文件中单独编写功能函数 比如要实现一个64位的加法函数,原型如下: ULONG64 my ...

  10. B. Arpa’s obvious problem and Mehrdad’s terrible solution

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...