Language: C++

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<set>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(C)     (C).begin(),(C).end()

const int N = 255;
int order[N];
bool isleft[N];
void op(string &in,int &p,int cur,bool left,int maxlevel);

void exp(string &in,int &p,int cur,bool left,int maxlevel){
  //cout << in <<" " << p <<" " << in.size() << " " <<  cur <<" " << maxlevel << endl;
  int tmp=p;
  if (cur == 0){
    op(in,p,cur,left,maxlevel);
  }else {
    exp(in,p,cur-1,left,maxlevel);
  }
  while(p < in.size()){
    if (order[in[p]] != -1 && order[in[p]] == cur){
      //cout << in[p] <<" " << endl;
      p++;
      exp(in,p,cur,left,maxlevel);

      if (isleft[cur]== left){
	in.insert(in.begin()+tmp,'(');
	in.insert(in.begin()+p+1,')');
	p+=2;
      }

    }else {
      break;
    }
  }
}

void op(string &in,int &p,int cur,bool left,int maxlevel){
  if (in[p] == '('){
    //cout << "( "<<in[p] << " " << cur <<" "<< left << endl;
    p++;
    //cout <<"( "<< p<<" " << in[p] << endl;
    exp(in,p,maxlevel,left,maxlevel);
    //cout << ") "<<in[p] <<" " << cur <<" " << left <<  endl;
    p++;
  }else if (isalpha(in[p])){
    p++;
  }else {
    //cout << in[p] << endl;
    //assert(false);
  }
}

void test(){
  rep(i,N)order[i]=-1;
  order['>']=2;
  order['+']=order['-']=1;
  order['*']=0;
  isleft[1]=isleft[0]=true;
  string in="a+b*c-d>f";
  int p=0;
  reverse(ALL(in));
  while(p < in.size()){
    exp(in,p,2,true,2);    
  }
  cout << in << endl;
}

void reverse(string &in){
  reverse(ALL(in));
  rep(i,in.size())
    if (in[i] == '(')in[i]=')';
    else if (in[i] == ')')in[i]='(';
}

void erase_bl(string &in){
  int n=in.size();
  bool isneed[n];
  rep(i,in.size())isneed[i]=false;

  rep(i,in.size()){
    if (order[in[i]] != -1){
      int l=i-1,r=i+1;
      while(l>=0){
	if (in[l]=='('&&!isneed[l]){
	  isneed[l]=true;
	  break;
	}
	l--;
      }
      while(r<in.size()){
	if (in[r] == ')' && !isneed[r]){
	  isneed[r]=true;
	  break;
	}
	r++;
      }
    }
  }

  rep(i,in.size())
    if ((in[i]=='('||in[i]==')') &&!isneed[i])
      in[i]=' ';

  in.erase(remove(in.begin(),in.end(),' '),in.end());
}


void solve(string &in,int level){
  int p=0;
  reverse(in);
  exp(in,p,level-1,true,level-1);

  //cout <<"mid "<< endl;
  p=0;
  reverse(in);
  //cout << "tar " << in << endl;
  exp(in,p,level-1,false,level-1);

  erase_bl(in);

  cout << in << endl;
}


main(){
  int te;
  cin>>te;
  while(te--){
    rep(i,N)order[i]=-1;
    int m;
    cin>>m;
    rep(i,m){
      char lr;
      cin>>lr;
      if (lr == 'L'){
	isleft[m-i-1]=true;
      }else if (lr == 'R'){
	isleft[m-i-1]=false;
      }
      int num;
      cin>>num;
      rep(j,num){
	char tmp;
	cin>>tmp;
	order[tmp]=m-i-1;
      }
    }

    int q;
    cin>>q;
    rep(i,q){
      string in;
      cin>>in;
      solve(in,m);
    }
    if (te)cout << endl;
  }
  return false;
}

Compiler output:
a.cc: In function 'void test()':
a.cc:67:3: error: 'reverse' was not declared in this scope
   67 |   reverse(ALL(in));
      |   ^~~~~~~
a.cc: In function 'void reverse(std::string&)':
a.cc:10:29: error: invalid initialization of non-const reference of type 'std::string&' {aka 'std::__cxx11::basic_string<char>&'} from an rvalue of type 'std::__cxx11::basic_string<char>::iterator'
   10 | #define ALL(C)     (C).begin(),(C).end()
      |                    ~~~~~~~~~^~
a.cc:75:11: note: in expansion of macro 'ALL'
   75 |   reverse(ALL(in));
      |           ^~~
a.cc:74:22: note: in passing argument 1 of 'void reverse(std::string&)'
   74 | void reverse(string &in){
      |              ~~~~~~~~^~
a.cc: In function 'void erase_bl(std::string&)':
a.cc:110:27: error: cannot convert 'std::__cxx11::basic_string<char>::iterator' to 'const char*'
  110 |   in.erase(remove(in.begin(),in.end(),' '),in.end());
      |                   ~~~~~~~~^~
      |                           |
      |                           std::__cxx11::basic_string<char>::iterator
In file included from /usr/include/c++/14/cstdio:42,
                 from /usr/include/c++/14/ext/string_conversions.h:45,
                 from /usr/include/c++/14/bits/basic_string.h:4154,
                 from /usr/include/c++/14/string:54,
                 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/stdio.h:158:32: note:   initializing argument 1 of 'int remove(const char*)'
  158 | extern int remove (const char *__filename) __THROW;
      |                    ~~~~~~~~~~~~^~~~~~~~~~
a.cc: At global scope:
a.cc:131:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  131 | main(){
      | ^~~~