p191.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

char init;
string s;

void read() { cin >> init >> s; }

void solve() {
    // The problem statement is somewhat hard to parse, but it boils down to a
    // context-free grammar. Let S_A and S_B be nonterminals representing empty
    // stands signed by A and B, and let 'A', 'B' be terminals (filled stands).
    // The two operations give us the following productions:
    //     S_A -> 'B'
    //     S_A -> 'A' S_B S_A
    //     S_B -> 'A'
    //     S_B -> 'B' S_A S_B
    //
    // We need to check if the given string can be derived starting from S_A or
    // S_B (given in input). This grammar is LL(1) since the first terminal
    // uniquely determines which production to apply: if c == X we must expand,
    // if c is the opponent we must use the terminal production. So we can use
    // standard predictive parsing with a stack of pending nonterminals. For each
    // character c, pop the top S_X: if c is the opponent of X, it matches a
    // terminal production and we consume it. If c == X, we apply the expand
    // rule, pushing S_X and then S_{opponent} on top. The string is valid iff
    // the stack is empty at the end.

    auto opponent = [](char c) { return c == 'A' ? 'B' : 'A'; };

    vector<char> stack = {init};
    for(char c: s) {
        if(stack.empty()) {
            cout << "NO" << endl;
            return;
        }

        char top = stack.back();
        stack.pop_back();

        if(c == top) {
            stack.push_back(top);
            stack.push_back(opponent(top));
        }
    }

    cout << (stack.empty() ? "YES" : "NO") << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        solve();
    }

    return 0;
}

=================
p191.in1
======================
A
AAB

=================
p191.ans1
======================
YES

=================
statement.txt
======================
191. Exhibition
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



Two companies "A" and "B" decided to hold a joint exhibition of their production. Antitrust committee decided to take an experiment. To have a possibility to show its own production each company must advertise the production of its competitor. The exhibition is built as a straight row of stands. Some stands can be empty, other contain the production of represented companies (each stand can contain the production of only one company). The empty stands are also signed with the names of the companies.
Originally the exhibition consists of just one empty stand, which is signed by the name of one of the companies (by a lot).
The company can choose any of its empty stands and either to fill it with the production of the competitor or to place the empty stand to the left, signed by the name of the competitor, and to the left of that place the stand with its own production. If there are any stands to the left, they are moved to the left. By the beginning of the exhibition all stands must be filled.
Your task is to write a program in accordance with the filled stands before the exhibition to determine if the requirements of the antitrust committee were satisfied or not.

Input
The first line contains the name of the company ("A" or "B"), which was chosen to be the first to fill the empty stand. The second line contains a row of the stands presented to the antitrust committee. The stands are listed from left to right. Letter "A" indicates a stand of company "A", letter "B" - a stand of company "B". Maximum number of stands at the exhibition is 30000.

Output
Output "YES" - if the requirements of the antitrust committee were fulfilled, and "NO" - if not.

Sample test(s)

Input
A
AAB

Output
YES
Author:	Sergey V. Mironov
Resource:	ACM International Collegiate Programming Contest 2003-2004
North-Eastern European Region, Southern Subregion
Date:	2003 October, 9

=================
