Source Code
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define x           first
#define y           second
#define all(v)      v.begin(),v.end()
#define clr(v,d)    memset(v,d,sizeof(v));

const int N = 1e5 + 10;

const ll mod = 1e9 + 7;

int main()
{
    cin.tie(0);
    cin.sync_with_stdio(0);

    int n , an , o , xo ;

    cin >> n >> an >> o >> xo ;

    string s ,ss , rs ;

    cin >> s >> ss >> rs ;

    string ans = s ;

    for (int i = 0; i < n; ++i) {
        if(s[i] == '0' && ss[i] == '1' && rs[i] == '0')
        {
            if(an == 0 ) return cout << "NO" , 0;
            an--;
            ans[i] = '&' ;
        }
        if(s[i] == '1' && ss[i] == '0' && rs[i] == '0')
        {
            if(an == 0 ) return cout << "NO" , 0;
            an--;
            ans[i] = '&' ;
        }
        if(s[i] == '1' && ss[i] == '1' && rs[i] == '0')
        {
            if(xo == 0) return cout << "NO" , 0;
            xo--;
            ans[i] = '^' ;
        }
        if(s[i] == '0' && ss[i] == '0' && rs[i] == '1' )return cout << "NO" , 0 ;
    }
    for (int i = 0; i < n && xo; ++i) {
        if(s[i] == '0' && ss[i] == '1' && rs[i] == '1')
        {
            xo--;
            ans[i] = '^' ;
        }
        if(s[i] == '1' && ss[i] == '0' && rs[i] == '1' && xo)
        {
            xo--;
            ans[i] = '^' ;
        }
    }
    for (int i = 0; i < n && an; ++i) {
        if(s[i] == '1' && ss[i] == '1' && rs[i] == '1')
        {
            an--;
            ans[i] = '&' ;
        }
    }
    //0 0 0 -> or / xor / and
    //0 1 1 -> or / xor
    //1 1 1 -> and/or
    for (int i = 0; i < n; ++i) {
        if(s[i] == '0' && ss[i] == '0' && rs[i] == '0')
        {
            if(xo) xo-- , ans[i] = '^' ;
            else if(an) an-- , ans[i] = '&' ;
            else if(o) o--, ans[i] = '|' ;
            else return cout << "NO" , 0 ;
        }
        if(s[i] == '1' && ss[i] == '0' && rs[i] == '1' && ans[i] != '^')
        {
            if(o) o--, ans[i] = '|' ;
            else return cout << "NO" , 0 ;
        }
        if(s[i] == '0' && ss[i] == '1' && rs[i] == '1' && ans[i] != '^')
        {
            if(o) o--, ans[i] = '|' ;
            else return cout << "NO" , 0 ;
        }
        if(s[i] == '1' && ss[i] == '1' && rs[i] == '1' && ans[i] != '&')
        {
            if(o) o--, ans[i] = '|' ;
            else return cout << "NO" , 0 ;
        }
        if(ans[i] == '0' || ans[i] == '1') return cout << "NO",0 ;
    }

    cout << "YES\n" ;
    cout << ans ;

    return 0;
}
Copy
Binary String MuhammadHassan
GNU G++17
18 ms
1.1 MB
Accepted