Codeforces Problem 778B 拆位做法
来源:互联网 发布:c语言中的float 编辑:程序博客网 时间:2024/06/06 12:40
题目: http://codeforces.com/problemset/problem/778/B
简述题意
有n条语句,每一句描述一个变量名,以及其值(二进制),或表达式(仅由前面的变量名、"?"、运算符号组成)我们的任务是:求出是所有变量最大 || 最小的“?”
分析
二进制。。。我们可以使用拆位,即每一位分开处理先读入,预处理循环一次所有语句,同时循环每一位,尝试该位为0或1,然后按要求得出答案
另外
这道题的难点应该在于字符串处理。。。
贴代码
#include<cstdio>#include<iostream>#include<string>#include<map>using namespace std;const int maxn = 5050;const int maxm = 1050;map<string, int> name;bool val[maxn][maxm];int oper[maxn];string x[maxn], y[maxn];int n, m;string s, ans_min="", ans_max="";int cal(int a, int b) { int g[maxn], x0, y0, ans = 0; for(int i = 1; i <= n; i++) { if(oper[i] == 0) ans += g[i] = val[i][a]; else { x0 = x[i] == "?" ? b : g[name[x[i]]]; y0 = y[i] == "?" ? b : g[name[y[i]]]; if(oper[i] == 1) ans += g[i] = x0 & y0; if(oper[i] == 2) ans += g[i] = x0 | y0; if(oper[i] == 3) ans += g[i] = x0 ^ y0; } } return ans;}int main() { cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> s; name[s] = i; cin >> s >> s; if(s[0] == '0' || s[0] == '1') { oper[i] = 0; for(int j = 0; j < m; j++) val[i][j] = s[j] - '0'; } else { x[i] = s; cin >> s; oper[i] = s == "AND" ? 1 : (s == "OR" ? 2 : 3); cin >> s; y[i] = s; } } for(int i = 0; i < m; i++) { int k0, k1; k0 = cal(i, 0); k1 = cal(i, 1); if(k0 <= k1) ans_min += '0'; else ans_min += '1'; if(k0 >= k1) ans_max += '0'; else ans_max += '1'; } cout << ans_min << endl << ans_max << endl; return 0;}
0 0
- Codeforces Problem 778B 拆位做法
- codeforces 778B 拆位运算
- Problem - 216B - Codeforces
- Problem - 218B - Codeforces
- codeforces problem/408/B
- Codeforces Problem - 50B
- Problem - 376B - Codeforces
- problem - 616B -codeforces
- Codeforces Problem 333B
- Codeforces Problem 332B
- 位操作 A + B Problem
- codeforces-Problem 193B - Xor
- Codeforces Problem 499B. Lecture
- codeforces 278B New Problem
- Codeforces Problem - 617B Chocolate
- Problem--158B--Codeforces--Taxi
- codeforces 782/problem/B [二分]
- codeforces/contest/797/problem/B
- Maven组件开发
- Ibatis 框架的基本介绍及基础操作
- 清华 2012 求1和2的个数
- HDU1175
- AOJ803魔方
- Codeforces Problem 778B 拆位做法
- spring知识七------对JDBC的支持
- 树莓派 开始的配置
- JVM加载class文件的原理机制
- 打造氚云PaaS智慧服务 社区+智能客服同步上线
- Cucumber测试框架应用介绍--黄瓜框架
- 一步一步学MySQL----4 表的创建与删除
- apple 产品鉴定
- 《快学Scala》习题详解 第3章 数组