题意:
选择一个 m 位的二进制数字,总分为 n 个算式的答案之和。问得到最低分和最高分分别应该取哪个二进制数字
分析:
因为所有数字都是m位的,高位的权重大于低位 ,我们就从高到低考虑 ans 的每一位是取 0 还是取 1,统计该位的权重(即n个式子该位结果之和)即可。
#include <bits/stdc++.h>
using namespace std;
int n, m;
map<string, int> mp;
struct query{
string f;
int num;
int a, op, b;
}q[];
void init()
{
cin >> n >> m;
string s;
mp["?"] = ;
for (int i = ; i <= n; i++)
{
cin >> s;
mp[s] = i;
cin >> s;
cin >> s;
if (s[] >= '' && s[] <= '')
{
q[i].op = ;
q[i].f = s;
}
else
{
q[i].a = mp[s];
cin >> s;
if (s[] == 'A') q[i].op = ;
if (s[] == 'O') q[i].op = ;
if (s[] == 'X') q[i].op = ;
cin >> s;
q[i].b = mp[s];
}
}
}
int find(int x, int p)
{
int ret = ;
q[].num = p;
for (int i = ; i <= n; i++)
{
if (q[i].op == ) q[i].num = q[i].f[x]-'';
else
{
int a = q[q[i].a].num;
int b = q[q[i].b].num;
if (q[i].op == ) q[i].num = a&b;
if (q[i].op == ) q[i].num = a|b;
if (q[i].op == ) q[i].num = a^b;
}
ret += q[i].num;
}
return ret;
}
void solve()
{
string ans1, ans2;
for (int i = ; i < m; i++)
{
int x0 = find(i, );
int x1 = find(i, );
x0 <= x1 ? ans1 += '' : ans1 += '';
x0 >= x1 ? ans2 += '' : ans2 += '';
}
cout << ans1 << endl << ans2 << endl;
}
int main()
{
init();
solve();
}