01串状态压缩
2025-09-20 20:57:15
发布于:湖北
5阅读
0回复
0点赞
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 将手牌字符串转换为花色掩码
int get_color_mask(const string& cards) {
int mask = 0;
for (char c : cards) {
switch (c) {
case 'a': mask |= 1; break; // 0001
case 'b': mask |= 2; break; // 0010
case 'c': mask |= 4; break; // 0100
case 'd': mask |= 8; break; // 1000
}
}
return mask;
}
void solve() {
int n, m;
cin >> n >> m;
string s, t;
cin >> s >> t;
// 获取自己和对手的花色掩码
int self_mask = get_color_mask(s);
int opp_mask = get_color_mask(t);
bool case1 = false, case2 = false, case3 = false, case4 = false;
// Case1:对手必须有所有四种花色
case1 = (opp_mask == 15);
// Case2:对手选3种花色,自己选剩下的1种
for (int opp_submask = 1; opp_submask < 16; opp_submask++) {
// 只需要考虑3种花色的组合
if (__builtin_popcount(opp_submask) != 3) continue;
// 检查对手是否有这3种花色
if ((opp_mask & opp_submask) != opp_submask) continue;
// 计算缺失的第4种花色
int needed_color = 15 & (~opp_submask);
// 检查自己是否有这种花色
if (self_mask & needed_color) {
case2 = true;
break;
}
}
// Case3:对手选1种花色,自己选剩下的3种
for (int opp_submask = 1; opp_submask < 16; opp_submask++) {
// 只需要考虑1种花色的组合
if (__builtin_popcount(opp_submask) != 1) continue;
// 检查对手是否有这种花色
if ((opp_mask & opp_submask) != opp_submask) continue;
// 计算缺失的3种花色
int needed_color = 15 & (~opp_submask);
// 检查自己是否有这3种花色
if ((self_mask & needed_color) == needed_color) {
case3 = true;
break;
}
}
// Case4:自己必须有所有四种花色
case4 = (self_mask == 15);
// 输出结果
cout << (case1 ? '1' : '0');
cout << (case2 ? '1' : '0');
cout << (case3 ? '1' : '0');
cout << (case4 ? '1' : '0');
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
这里空空如也
有帮助,赞一个