QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#862965#9980. Boolean Function Reconstructionucup-team4975WA 0ms3712kbC++143.9kb2025-01-19 11:17:312025-01-19 11:17:32

Judging History

你现在查看的是最新测评结果

  • [2025-01-19 11:17:32]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3712kb
  • [2025-01-19 11:17:31]
  • 提交

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;
        for (int i = 0; i < ans.size(); i++){
            if(i == 0 && ans[0] == '(')
                continue;
            if (i == ans.size() - 1 && ans[i] == ')')
                continue;
            cout << ans[i];
        }
        cout << endl;
    }
    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: 0
Wrong Answer
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:

wrong answer invalid expressions 9 (test case 1)