Language: C++

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vec;

vec Add(vec,vec);
vec Power(vec,int);
vec Formula();
vec Ar();
vec v[27];
string s;
int n,p;

int main(){
  int k,h,a;
  char c;
  cin>>n>>k;
  while(k--){
    cin>>c>>h;
    for(int i=0;i<n;i++) v[c-'A'].push_back(i);
    for(int i=0;i<h-1;i++)
      for(int j=0;j<n-1;j++){
	cin>>a;
	if(a) swap(v[c-'A'][j],v[c-'A'][j+1]);
      }
  }
  cin>>k;
  while(k--){
    cin>>s;
    s+='$';
    p=0;
    vec ans=Formula();
    for(int i=0;i<n;i++){
      if(i) cout<<' ';
      cout<<ans[i]+1;
    }
    cout<<endl;
  }
  return 0;
}

vec Add(vec A,vec B){
  vec res=vec(n);
  for(int i=0;i<n;i++) res[i]=A[B[i]];
  return res;
}

vec Power(vec A,int x){
  vec res;
  for(int i=0;i<n;i++) res.push_back(i);
  while(x){
    if(x%2) res=Add(res,A);
    x/=2;
    A=Add(A,A);
  }
  return res;
}

vec Formula(){
  vec res=Ar();
  if(s[p]=='+') p++,res=Add(res,Ar());
  return res;
}

vec Ar(){
  if(s[p]=='('){
    p++;
    vec res=Formula();
    p++;
    return res;
  }
  if('0'<=s[p]&&s[p]<='9'){
    int num=0;
    while('0'<=s[p]&&s[p]<='9') num=num*10+s[p]-'0',p++;
    vec res=Ar();
    return Power(res,num);
  }
  if('A'<=s[p]&&s[p]<='Z') return v[s[p++]-'A'];
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> vec;

vec Add(vec,vec);
vec Power(vec,int);
vec Formula();
vec Ar();
vec v[27];
string s;
int n,p;

int main(){
  int k,h,a;
  char c;
  cin>>n>>k;
  while(k--){
    cin>>c>>h;
    for(int i=0;i<n;i++) v[c-'A'].push_back(i);
    for(int i=0;i<h-1;i++)
      for(int j=0;j<n-1;j++){
	cin>>a;
	if(a) swap(v[c-'A'][j],v[c-'A'][j+1]);
      }
  }
  cin>>k;
  while(k--){
    cin>>s;
    s+='$';
    p=0;
    vec ans=Formula();
    for(int i=0;i<n;i++){
      if(i) cout<<' ';
      cout<<ans[i]+1;
    }
    cout<<endl;
  }
  return 0;
}

vec Add(vec A,vec B){
  vec res=vec(n);
  for(int i=0;i<n;i++) res[i]=A[B[i]];
  return res;
}

vec Power(vec A,int x){
  vec res;
  for(int i=0;i<n;i++) res.push_back(i);
  while(x){
    if(x%2) res=Add(res,A);
    x/=2;
    A=Add(A,A);
  }
  return res;
}

vec Formula(){
  vec res=Ar();
  if(s[p]=='+') p++,res=Add(res,Ar());
  return res;
}

vec Ar(){
  if(s[p]=='('){
    p++;
    vec res=Formula();
    p++;
    return res;
  }
  if('0'<=s[p]&&s[p]<='9'){
    int num=0;
    while('0'<=s[p]&&s[p]<='9') num=num*10+s[p]-'0',p++;
    vec res=Ar();
    return Power(res,num);
  }
  if('A'<=s[p]&&s[p]<='Z') return v[s[p++]-'A'];
}

Compiler output:
a.cc:93:5: error: redefinition of 'vec v [27]'
   93 | vec v[27];
      |     ^
a.cc:12:5: note: 'vec v [27]' previously declared here
   12 | vec v[27];
      |     ^
a.cc:94:8: error: redefinition of 'std::string s'
   94 | string s;
      |        ^
a.cc:13:8: note: 'std::string s' previously declared here
   13 | string s;
      |        ^
a.cc:95:5: error: redefinition of 'int n'
   95 | int n,p;
      |     ^
a.cc:14:5: note: 'int n' previously declared here
   14 | int n,p;
      |     ^
a.cc:95:7: error: redefinition of 'int p'
   95 | int n,p;
      |       ^
a.cc:14:7: note: 'int p' previously declared here
   14 | int n,p;
      |       ^
a.cc:97:5: error: redefinition of 'int main()'
   97 | int main(){
      |     ^~~~
a.cc:16:5: note: 'int main()' previously defined here
   16 | int main(){
      |     ^~~~
a.cc:125:5: error: redefinition of 'vec Add(vec, vec)'
  125 | vec Add(vec A,vec B){
      |     ^~~
a.cc:44:5: note: 'vec Add(vec, vec)' previously defined here
   44 | vec Add(vec A,vec B){
      |     ^~~
a.cc:131:5: error: redefinition of 'vec Power(vec, int)'
  131 | vec Power(vec A,int x){
      |     ^~~~~
a.cc:50:5: note: 'vec Power(vec, int)' previously defined here
   50 | vec Power(vec A,int x){
      |     ^~~~~
a.cc:142:5: error: redefinition of 'vec Formula()'
  142 | vec Formula(){
      |     ^~~~~~~
a.cc:61:5: note: 'vec Formula()' previously defined here
   61 | vec Formula(){
      |     ^~~~~~~
a.cc:148:5: error: redefinition of 'vec Ar()'
  148 | vec Ar(){
      |     ^~
a.cc:67:5: note: 'vec Ar()' previously defined here
   67 | vec Ar(){
      |     ^~
a.cc: In function 'vec Ar()':
a.cc:81:1: warning: control reaches end of non-void function [-Wreturn-type]
   81 | }
      | ^
a.cc: In function 'vec Ar()':
a.cc:162:1: warning: control reaches end of non-void function [-Wreturn-type]
  162 | }
      | ^