Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}

using piii = pair<int,pair<int,int>>;

bool issafe(const piii& a, const piii& b){
  if(a.first+b.first < 0) return false;
  if(a.second.first+b.second.first < 0) return false;
  if(a.second.second+b.second.second < 0) return false;
  return true;
}

piii pls(const piii& a, const piii& b){
  pisol.pb(pls(past, pa));ii res;
  res.fi
  e{
  }rst = a.first+b sol.pb(pls(past,pb));
  res.second.f{
    irst = a.second.first
  } + b.second.first;
  res.second.second = a.second.second + b.second.second;
  return res;
}

int main(){
  ll N,A,B,C; cin >> N >> A >> B >> C;
  vector<string> S(N);
  rep(i,N){
    cin >> S[i];
  }
  piii start = {A,{B,C}};
  vector<unordered_set<pair<piii,piii>>> dp(N+1);
  piii aa = {0,{0,0}};
  dp[0].insert(make_pair(aa,aa));
  rep(i,N){
    for(auto a: dp[i]){
      piii now = a.second;
      if(S[i] == "AB"){
        piii tmp1 = now;
        piii tmp2 = now;
        tmp1.first++; tmp1.second.first--;
        tmp2.first--; tmp2.second.first++;
        if(issafe(start,tmp1)) dp[i+1].insert(make_pair(now, tmp1));
        if(issafe(start,tmp2)) dp[i+1].insert(make_pair(now, tmp2));
      }
      else if(S[i] == "BC"){
          piii tmp1 = now;
          piii tmp2 = now;
          tmp1.second.first++; tmp1.second.second--;
          tmp2.second.first--; tmp2.second.second++;
          if(issafe(start,tmp1)) dp[i+1].insert(make_pair(now, tmp1));
          if(issafe(start,tmp2)) dp[i+1].insert(make_pair(now, tmp2));
      }
      else if(S[i] == "AC"){
          piii tmp1 = now;
          piii tmp2 = now;
          tmp1.first++; tmp1.second.second--;
          tmp2.first--; tmp2.second.second++;
          if(issafe(start,tmp1)) dp[i+1].insert(make_pair(now, tmp1));
          if(issafe(start,tmp2)) dp[i+1].insert(make_pair(now, tmp2));
      }
    }
  }
  if(sz(dp[0])==0){
    cout << "No" << endl;
    return 0;
  }


  vector<piii> sol;

  for(auto a: dp[N]){
    auto now = a;
    sol.pb(now.second);
    for(int i=1; N-1>=0; i++){
      piii past = now.first;
      piii pa, pb;
      if(S[N-i] == "AB"){
        pa = {1, {-1, 0}};
        pb = {-1, {1, 0}};
      }
      if(S[N-i] == "BC"){
        pa = {0, {1, -1}};
        pb = {0, {-1, 1}};
      }
      if(S[N-i] == "AC"){
        pa = {1, {0, -1}};
        pb = {-1, {0, 1}};
      }
      if(dp[N-1].find(make_pair(pls(past,pa), past)) != dp[N-1].end()){
        sol.pb(pls(past, pa));
        now = make_pair(pls(past,pa), past);
      }
      else {
        sol.pb(pls(past,pb));
        now = make_pair(pls(past,pb), past);
      }
    }
    break;
  }
  reverse(all(sol));
  rep(i,N){
    piii past = sol[i];
    piii now = sol[i+1];
    if(past.first+1==now.first) cout << 'A' << endl;
    else if(past.second.first+1==now.second.first) cout << 'B' << endl;
    else if(past.second.second+1==now.second.second) cout << 'C' << endl;
  }
  return 0;
}


Compiler output:
a.cc: In function 'piii pls(const piii&, const piii&)':
a.cc:41:3: error: 'pisol' was not declared in this scope
   41 |   pisol.pb(pls(past, pa));ii res;
      |   ^~~~~
a.cc:41:16: error: 'past' was not declared in this scope
   41 |   pisol.pb(pls(past, pa));ii res;
      |                ^~~~
a.cc:41:22: error: 'pa' was not declared in this scope; did you mean 'a'?
   41 |   pisol.pb(pls(past, pa));ii res;
      |                      ^~
      |                      a
a.cc:41:27: error: 'ii' was not declared in this scope
   41 |   pisol.pb(pls(past, pa));ii res;
      |                           ^~
a.cc:42:3: error: 'res' was not declared in this scope; did you mean 'rep'?
   42 |   res.fi
      |   ^~~
      |   rep
a.cc:44:4: error: 'rst' was not declared in this scope
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |    ^~~
a.cc:44:17: error: no match for 'operator+' (operand types are 'const int' and 'const piii' {aka 'const std::pair<int, std::pair<int, int> >'})
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |          ~~~~~~~^~
      |            |     |
      |            |     const piii {aka const std::pair<int, std::pair<int, int> >}
      |            const int
In file included from /usr/include/c++/14/string:48,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator.h:627:5: note: candidate: 'template<class _Iterator> constexpr std::reverse_iterator<_Iterator> std::operator+(typename reverse_iterator<_Iterator>::difference_type, const reverse_iterator<_Iterator>&)'
  627 |     operator+(typename reverse_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:627:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   'const piii' {aka 'const std::pair<int, std::pair<int, int> >'} is not derived from 'const std::reverse_iterator<_Iterator>'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note: candidate: 'template<class _Iterator> constexpr std::move_iterator<_IteratorL> std::operator+(typename move_iterator<_IteratorL>::difference_type, const move_iterator<_IteratorL>&)'
 1798 |     operator+(typename move_iterator<_Iterator>::difference_type __n,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1798:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   'const piii' {aka 'const std::pair<int, std::pair<int, int> >'} is not derived from 'const std::move_iterator<_IteratorL>'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:3598:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3598 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3598:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3616:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3616 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3616:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const _CharT*' and 'int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3635:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3635 |     operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3635:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   'const piii' {aka 'const std::pair<int, std::pair<int, int> >'} is not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3652:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, const _CharT*)'
 3652 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3652:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3670:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, _CharT)'
 3670 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3670:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3682:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
 3682 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3682:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3689:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const __cxx11::basic_string<_CharT, _Traits, _Allocator>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3689 |     operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3689:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3696:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3696 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3696:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3719:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3719 |     operator+(const _CharT* __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3719:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'const _CharT*' and 'int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3726:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(_CharT, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&)'
 3726 |     operator+(_CharT __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3726:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const piii' {aka 'const std::pair<int, std::pair<int, int> >'} have incompatible cv-qualifiers
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/basic_string.h:3733:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(__cxx11::basic_string<_CharT, _Traits, _Allocator>&&, const _CharT*)'
 3733 |     operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.h:3733:5: note:   template argument deduction/substitution failed:
a.cc:44:18: note:   mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Allocator>' and 'const int'
   44 |   }rst = a.first+b sol.pb(pls(past,pb));
      |                  ^
/usr/include/c++/14/bits/ba