QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#862954 | #9980. Boolean Function Reconstruction | ucup-team4975 | WA | 1ms | 3712kb | C++14 | 3.7kb | 2025-01-19 11:13:11 | 2025-01-19 11:13:12 |
Judging History
answer
#define LOCAL
#include <bits/stdc++.h>
#define fir first
#define sec second
#define el '\n'
#ifdef LOCAL
#define FINISH cerr << "FINISH" << endl;
#else
#define FINISH ;
#endif
#ifdef LOCAL
#define debug(x) cerr << setw(4) << #x << " == " << x << endl
#else
#define debug(x)
#endif
#ifdef LOCAL
#define debugv(x) \
cerr << setw(4) << #x << ":: "; \
for (auto i : x) \
cerr << i << " "; \
cerr << endl
#else
#define debugv(x)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ostream& operator<<(ostream& out, PII& x)
{
out << x.fir << " " << x.sec << endl;
return out;
}
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
const int N = 200020;
int jud;
string getans(int n, string s, string res)
{
assert(s.size() == (1 << n));
bool f = true;
for (int i = 0; i < (1 << n); i++) {
if (s[i] != s[0])
f = false;
}
if (f) {
if (s[0] == '0')
return "F";
return "T";
}
for (int i = 0; i < (1 << n - 1); i++) {
if (s[i] == '1' && s[i + (1 << n - 1)] == '0')
jud = false;
}
if (n == 1) {
string ans;
if (s == "00") ans = "F";
else if (s == "01") ans = res[0];
else if (s == "11") ans = "T";
else jud = false;
return ans;
}
else if (n == 2) {
string ans;
if (s == "0000") ans = "F";
else if (s == "0001") ans = "(" + string(1, res[0]) + "&" + string(1, res[1]) + ")";
else if (s == "0011") ans = res[0];
else if (s == "0101") ans = res[1];
else if (s == "0111") ans = "(" + string(1, res[0]) + "|" + string(1, res[1]) + ")";
else if (s == "1111") ans = "T";
else jud = false;
return ans;
}
string ans = "(";
string sleft = string(s.begin(), next(s.begin(), (1 << n - 1)));
string newres = string(next(res.begin()), res.end());
string l = getans(n - 1, sleft, newres);
// assert(l != "T");
if (l != "F" && l != "T")
ans += l;
int flag = 1, allz = 1;
for (int i = (1 << n - 1); i < s.size(); i++) {
if (s[i] == '0') {
flag = 0;
break;
}
}
if (l != "F" && l != "T")
ans += "|";
if (flag) {
ans += res[0];
}
else {
if (l != "F" && l != "T")
ans += "(";
ans += res[0];
ans += "&";
string sright = string(next(s.begin(), (1 << n - 1)), s.end());
string r = getans(n - 1, sright, newres);
ans += r;
if (l != "F" && l != "T")
ans += ")";
}
ans += ")";
return ans;
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
string res = "";
for (int i = n - 1; i >= 0; i--) {
res += char('a' + i);
}
jud = true;
string ans = getans(n, s, res);
if (jud) {
int mxcnt = 10 + (1 << n - 1), cnt = 0, p = 0, mxp = 0;
for (int i = 0; i < ans.size(); i++) {
if (ans[i] == '|' || ans[i] == '&')
cnt++;
if (ans[i] == '(')
p++;
if (ans[i] == ')')
p--;
mxp = max(mxp, p);
}
// cout << cnt << " " << mxp << el;
if (cnt > mxcnt) {
cout << cnt << endl;
}
cout << "Yes" << el << ans << el;
}
else
cout << "No" << el;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
7 2 0001 2 0111 2 1111 3 00010111 1 10 2 0101 5 00000000000000000000000000000001
output:
Yes (b&a) Yes (b|a) Yes T Yes ((b&a)|(c&(b|a))) No Yes a Yes (e&(d&(c&(b&a))))
result:
ok 7 lines, tightest: 4 out of 14 (7 test cases)
Test #2:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
4 1 00 1 10 1 01 1 11
output:
Yes F No Yes a Yes T
result:
ok 4 lines, tightest: 0 out of 11 (4 test cases)
Test #3:
score: 0
Accepted
time: 1ms
memory: 3712kb
input:
16 2 0000 2 1000 2 0100 2 1100 2 0010 2 1010 2 0110 2 1110 2 0001 2 1001 2 0101 2 1101 2 0011 2 1011 2 0111 2 1111
output:
Yes F No No No No No No No Yes (b&a) No Yes a No Yes b No Yes (b|a) Yes T
result:
ok 16 lines, tightest: 1 out of 12 (16 test cases)
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3712kb
input:
256 3 00000000 3 10000000 3 01000000 3 11000000 3 00100000 3 10100000 3 01100000 3 11100000 3 00010000 3 10010000 3 01010000 3 11010000 3 00110000 3 10110000 3 01110000 3 11110000 3 00001000 3 10001000 3 01001000 3 11001000 3 00101000 3 10101000 3 01101000 3 11101000 3 00011000 3 10011000 3 01011000...
output:
Yes F No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No ...
result:
wrong answer invalid expressions 5 (test case 241)