#include<iostream>#include<cstring>#include<algorithm>usingnamespace std;typedef pair<string,int> PII;constint N =100010;
PII str[N];int n, m;intcalc(int bits,int now){for(int i =1; i <= n; i ++){int x = str[i].second >> bits &1;//把第bits位取出来if(str[i].first =="AND") now &= x;elseif(str[i].first =="OR") now |= x;else now ^= x;}return now;}intmain(){
cin >> n >> m;//存储每扇门的信息。for(int i =1; i <= n; i ++){
string a;int op;
cin >> a >> op;
str[i]={a, op};}int val =0, ans =0;//val更新当前的值,ans存储当前答案。for(int bits =29; bits >=0; bits --){int res0 =calc(bits,0);int res1 =calc(bits,1);if(val +(1<< bits)<= m && res0 < res1)
val +=(1<< bits), ans += res1 << bits;else ans += res0 << bits;}
cout << ans;return0;}
题目链接:起床困难综合征题目分析:0.知识点:位运算1.思路参考蓝书第八页。code:#include <iostream>#include <cstring>#include <algorithm>using namespace std;typedef pair<string, int> PII;const int N = 100010;PII str[N];int n, m;int calc(int bits, int