Language: C++

#include <iostream>
#include <map>

using namespace std;

map<pair<int,int>,char> mem[100001];
char str[100001];
int N,A,B,C;
long sum;


char Search(int A,int B,int step);
char memSearch(int A,int B,int step);

char Search(int A,int B,int step){
  int C = sum -A-B;
  if(step == N){
   switch(str[step]){
     case 'A':
       if(B>0)
         return 'C';
       if(C>0)
         return 'B';
       return 0;
       break;
       
     case 'B':
       if(A>0)
         return 'C';
       if(C>0)
         return 'A';
       return 0;
       break;
       
       case 'C':
       if(B>0)
         return 'A';
       if(A>0)
         return 'B';
       return 0;
       break;
   }
  }
  
  switch(str[step]){
     case 'A':
       if(B>0&&memSearch(A,B-1,step+1)!=0)
         return 'C';
       if(C>0&&memSearch(A,B+1,step+1)!=0)
         return 'B';
       return 0;
       break;
       
     case 'B':
       if(A>0&&memSearch(A-1,B,step+1)!=0)
         return 'C';
       if(C>0&&memSearch(A+1,B,step+1)!=0)
         return 'A';
       return 0;
       break;
       
       case 'C':
       if(B>0&&memSearch(A+1,B-1,step+1)!=0)
         return 'A';
       if(A>0&&memSearch(A-1,B+1,step+1)!=0)
         return 'B';
       return 0;
       break;
   }
  	
  return -1;  
}

char memSearch(int A,int B,int step){
 if(mem[step].find(make_pair(A,B))!=mem[step].end())
      return mem[step][make_pair(A,B)];
 else{
   mem[step][make_pair(A,B)] = Search(A,B,step)
  return mem[step][make_pair(A,B)];
 }
}
  



int main(){
  
  
  cin >> N >> A >> B >> C;
  sum = (long)A+B+C;

  for(int i=1; i<=N; i++){
   char buf[3];
   cin >> buf;
   if(buf[0]=='A'){
	if(buf[1] == 'B')
      str[i] = 'C';
     else
       str[i] = 'B';
   }else
     str[i] = 'A';
  }
  
  if(memSearch(A,B,0)==1){
	cout << "No" << endl;
   return 0; 
  }else{
  	cout << "Yes" << endl;
    for(int step = 1; step<=N; step++){
    	cout << memSearch(A,B,step)) << endl; 
      	switch(memSearch(A,B,step)){
          case 'A':
            A++;
            if(str[step]=='B')
              C--;
            else
              B--;
            break;
          case 'B':
            B++;
            if(str[step]=='A')
              C--;
            else
              A--;
            break;
          case 'C':
            C++;
            if(str[step]=='B')
              A--;
            else
              B--;
            break;
            
        }
    }
  
  }

  
  
  return 0;
}




Compiler output:
a.cc: In function 'char memSearch(int, int, int)':
a.cc:78:48: error: expected ';' before 'return'
   78 |    mem[step][make_pair(A,B)] = Search(A,B,step)
      |                                                ^
      |                                                ;
   79 |   return mem[step][make_pair(A,B)];
      |   ~~~~~~                                        
a.cc: In function 'int main()':
a.cc:110:36: error: expected ';' before ')' token
  110 |         cout << memSearch(A,B,step)) << endl;
      |                                    ^
      |                                    ;
a.cc: In function 'char memSearch(int, int, int)':
a.cc:78:30: warning: control reaches end of non-void function [-Wreturn-type]
   78 |    mem[step][make_pair(A,B)] = Search(A,B,step)