{$apptype console}
{$q+,r+,o-}

uses
    TestLib, SysUtils;

const
    max_ops = 1000;
    max_vars = 11;

var
    formula: string;
    pos: longint;
    token: char;
    outcome: tResult;
    op: array [1..3 * max_ops] of char;
    op1, op2: array [1..3 * max_ops] of longint;
    values: array [1..3 * max_ops] of boolean;
    ops: longint;
    vars: array [1..max_vars] of char;
    varValues: array [1..max_vars] of boolean;
    u: array [1..max_vars] of longint;
    cvars: longint;

function add(_op: char; _op1, _op2: longint): longint;
begin
    inc(ops);
    op[ops] := _op;
    op1[ops] := _op1;
    op2[ops] := _op2;
    add := ops;
end;

function addVariable(v: char): longint;
var
    i: longint;
begin
    for i := 1 to cvars do begin
        if (vars[i] = v) then begin
            if (outcome <> _fail) and (u[i] <> 0) then begin
               quit(outcome, format('Variable "%s" used twice: at %d and %d', [v, u[i], pos - 1]));
            end;
            u[i] := pos - 1;
            addVariable := i;
            exit;
        end;
    end;
    if (outcome <> _fail) then begin
        quit(outcome, format('Unknown variable "%s" at %d', [v, pos - 1]));
    end;
    inc(cvars);
    vars[cvars] := v;
    addVariable := cvars;
end;


procedure nextToken();
begin
    while (formula[pos] = ' ') do begin
        inc(pos);
    end;
    token := formula[pos];
    inc(pos);
end;

function test(t: char): boolean;
begin
    result := token = t;
    if (result) then begin
        nextToken();
    end;
end;

procedure expect(t: char);
begin
    if (not test(t)) then begin
        quit(outcome, format('"%s" expected, "%s" found at %d', [t, token, pos - 1]));
    end;
end;

function parseOr(): longint; forward;

function parseTerm(): longint;
begin
    if (test('~')) then begin
        result := add('~', parseTerm(), 0);
    end else if (test('(')) then begin
        result := parseOr();
        expect(')');
    end else if token in ['a'..'k'] then begin
        result := add('v', addVariable(token), 0);
        expect(token);
    end else begin
        result := 0;
        quit(outcome, format('Term expected, "%s" found at %d', [token, pos - 1]));
    end;
end;

function parseAnd(): longint;
begin
    result := parseTerm();
    while (test('&')) do begin
        result := add('&', result, parseTerm());
    end;
end;

function parseOr(): longint;
begin
    result := parseAnd();
    while (test('|')) do begin
        result := add('|', result, parseAnd());
    end;
end;

function parse(var inf: InStream): longint;
begin
    pos := 1;
    formula := inf.readString() + '!!';
    nextToken();
    result := parseOr();
    expect('!');
end;

var
    ja, pa: string;
    i, j: longint;
    iroot, oroot: longint;
    s: string;
begin
    ja := ans.readstring();
    pa := ouf.readstring();

    if (pa <> 'Yes') and (pa <> 'No') then begin
        quit(_wa, format('"Yes" or "No" expected, "%s" found', [pa]));
    end;
    if (pa = 'No') then begin
        if (ja = 'Yes') then begin
            quit(_wa, 'Answer exists, but was not found');
        end;
        quit(_ok, 'No');
    end;

    outcome := _fail;
    iroot := parse(inf);

    fillchar(u, sizeof(u), 0);

    outcome := _wa;
    oroot := parse(ouf);

    for i := 0 to (1 shl cvars) - 1 do begin
        for j := 1 to cvars do begin
            varValues[j] := ((i shr (j - 1)) and 1) = 1;
        end;
        for j := 1 to ops do begin
            case op[j] of
                '|': values[j] := values[op1[j]] or values[op2[j]];
                '&': values[j] := values[op1[j]] and values[op2[j]];
                '~': values[j] := not values[op1[j]];
                'v': values[j] := varValues[op1[j]];
                else quit(_fail, 'Unknown op');
            end;
        end;

        if values[iroot] <> values[oroot] then begin
            s := '';
            for j := 1 to cvars do begin                              
                s := s + vars[j] + '=' + intToStr(ord(varValues[j])) + ' ';
            end;
            quit(_wa, format('%s: input=%d, output=%d', [s, ord(values[iroot]), ord(values[oroot])]));
        end;
    end;

    if (ja = 'No') then begin
        quit(_fail, 'Answer not found by the jury');
    end;
    quit(_ok, 'Yes');
end.
