只能这么说
C++二分查找很好用
就是有一点点绕
而且写起来容易出bug
不过应该是本人自己的问题不是方法不好
#include <iostream>
#include <string>
using namespace std;
const int N = 110;
int t, n, m;
string inputs[N];
int outputs[N];
bool dfs(int depth, string path) {
// 如果到达了最后一个 if 语句,则验证给定的程序是否与实际输出一致
if (depth == m) {
for (int i = 0; i < m; i++) {
int res = 0;
for (int j = 0; j < n; j++) {
if (path[j] == '1' && inputs[i][j] == '1') {
res = 1;
break;
}
}
if (res != outputs[i]) {
return false;
}
}
return true;
}
// 尝试将当前语句的条件设为每一个变量
for (int i = 0; i < n; i++) {
path[i] = '1';
if (dfs(depth + 1, path)) {
return true;
}
path[i] = '0';
}
// 如果没有任何条件为真,则当前语句的条件应该设为 false
if (dfs(depth + 1, path)) {
return true;
}
return false;
}
int main() {
cin >> t;
while (t--) {
cin >> n >> m;
// 读入所有的输入和输出
for (int i = 0; i < m; i++) {
cin >> inputs[i] >> outputs[i];
}
// 使用深度优先搜索来枚举所有可能的程序
string path(n, '0');
if (dfs(0, path)) {
cout << "OK" << endl;
} else {
cout << "LIE" << endl;
}
}
return 0;
}