Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;

int main(){
  int N,Q;
  cin>>N>>Q;
  int LR[N+2];
  for(int i=0; i<=N+1; ++i) LR[i]=1;
  
  char S[N+1];
  
  vector<int> Q;
  vector<int> W;
  vector<int> E;
  vector<int> R;
  vector<int> T;
  
  vector<int> Y;
  vector<int> U;
  vector<int> I;
  vector<int> O;
  vector<int> P;
  
  vector<int> A;
  vector<int> S;
  vector<int> D;
  vector<int> F;
  vector<int> G;
  
  vector<int> H;
  vector<int> J;
  vector<int> K;
  vector<int> L
  vector<int> Z;
  
  vector<int> X;
  vector<int> C;
  vector<int> V;
  vector<int> B;
  vector<int> N;
  vector<int> M;
  
  for(int i=1; i<=N; ++i){
    cin>>S[i];
    
    if(S[i]=='Q') Q.push_back(i);
    else if(S[i]=='W') W.push_back(i);
    else if(S[i]=='E') E.push_back(i);
    else if(S[i]=='R') R.push_back(i);
    else if(S[i]=='T') T.push_back(i);
    
    else if(S[i]=='Y') Y.push_back(i);
    else if(S[i]=='U') U.push_back(i);
    else if(S[i]=='I') I.push_back(i);
    else if(S[i]=='O') O.push_back(i);
    else if(S[i]=='P') P.push_back(i);
    
    else if(S[i]=='A') A.push_back(i);
    else if(S[i]=='S') S.push_back(i);
    else if(S[i]=='D') D.push_back(i);
    else if(S[i]=='F') F.push_back(i);
    else if(S[i]=='G') G.push_back(i);
    
    else if(S[i]=='H') H.push_back(i);
    else if(S[i]=='J') J.push_back(i);
    else if(S[i]=='K') K.push_back(i);
    else if(S[i]=='L') L.push_back(i);
    else if(S[i]=='Z') Z.push_back(i);
    
    else if(S[i]=='X') X.push_back(i);
    else if(S[i]=='C') C.push_back(i);
    else if(S[i]=='V') V.push_back(i);
    else if(S[i]=='B') B.push_back(i);
    else if(S[i]=='N') N.push_back(i);
    else if(S[i]=='M') M.push_back(i);
  }
 
  
  
  for(int i=1; i<=Q; ++i){
    char A,B;
    cin>>A>>B;
    
    if(A=='Q'){
      for(int j=0; j<Q.size(); ++j){
        if(LR[Q.at(j)]>0){
          LR[Q.at(j)] -= 1;
          if(B=='L') LR[Q.at(j-1)] += 1;
          else LR[Q.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='W'){
      for(int j=0; j<W.size(); ++j){
        if(LR[W.at(j)]>0){
          LR[W.at(j)] -= 1;
          if(B=='L') LR[W.at(j-1)] += 1;
          else LR[W.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='E'){
      for(int j=0; j<E.size(); ++j){
        if(LR[E.at(j)]>0){
          LR[E.at(j)] -= 1;
          if(B=='L') LR[E.at(j-1)] += 1;
          else LR[E.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='R'){
      for(int j=0; j<R.size(); ++j){
        if(LR[R.at(j)]>0){
          LR[R.at(j)] -= 1;
          if(B=='L') LR[R.at(j-1)] += 1;
          else LR[R.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='T'){
      for(int j=0; j<T.size(); ++j){
        if(LR[T.at(j)]>0){
          LR[T.at(j)] -= 1;
          if(B=='L') LR[T.at(j-1)] += 1;
          else LR[T.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='Y'){
      for(int j=0; j<Y.size(); ++j){
        if(LR[Y.at(j)]>0){
          LR[Y.at(j)] -= 1;
          if(B=='L') LR[Y.at(j-1)] += 1;
          else LR[Y.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='U'){
      for(int j=0; j<U.size(); ++j){
        if(LR[U.at(j)]>0){
          LR[U.at(j)] -= 1;
          if(B=='L') LR[U.at(j-1)] += 1;
          else LR[U.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='I'){
      for(int j=0; j<I.size(); ++j){
        if(LR[I.at(j)]>0){
          LR[I.at(j)] -= 1;
          if(B=='L') LR[I.at(j-1)] += 1;
          else LR[I.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='O'){
      for(int j=0; j<O.size(); ++j){
        if(LR[O.at(j)]>0){
          LR[O.at(j)] -= 1;
          if(B=='L') LR[O.at(j-1)] += 1;
          else LR[O.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='P'){
      for(int j=0; j<P.size(); ++j){
        if(LR[P.at(j)]>0){
          LR[P.at(j)] -= 1;
          if(B=='L') LR[P.at(j-1)] += 1;
          else LR[P.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='A'){
      for(int j=0; j<A.size(); ++j){
        if(LR[A.at(j)]>0){
          LR[A.at(j)] -= 1;
          if(B=='L') LR[A.at(j-1)] += 1;
          else LR[A.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='S'){
      for(int j=0; j<S.size(); ++j){
        if(LR[S.at(j)]>0){
          LR[S.at(j)] -= 1;
          if(B=='L') LR[S.at(j-1)] += 1;
          else LR[S.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='D'){
      for(int j=0; j<D.size(); ++j){
        if(LR[D.at(j)]>0){
          LR[D.at(j)] -= 1;
          if(B=='L') LR[D.at(j-1)] += 1;
          else LR[D.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='F'){
      for(int j=0; j<F.size(); ++j){
        if(LR[F.at(j)]>0){
          LR[F.at(j)] -= 1;
          if(B=='L') LR[F.at(j-1)] += 1;
          else LR[F.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='G'){
      for(int j=0; j<G.size(); ++j){
        if(LR[G.at(j)]>0){
          LR[G.at(j)] -= 1;
          if(B=='L') LR[G.at(j-1)] += 1;
          else LR[G.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='H'){
      for(int j=0; j<H.size(); ++j){
        if(LR[H.at(j)]>0){
          LR[H.at(j)] -= 1;
          if(B=='L') LR[H.at(j-1)] += 1;
          else LR[H.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='J'){
      for(int j=0; j<J.size(); ++j){
        if(LR[J.at(j)]>0){
          LR[J.at(j)] -= 1;
          if(B=='L') LR[J.at(j-1)] += 1;
          else LR[J.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='K'){
      for(int j=0; j<K.size(); ++j){
        if(LR[K.at(j)]>0){
          LR[K.at(j)] -= 1;
          if(B=='L') LR[K.at(j-1)] += 1;
          else LR[K.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='L'){
      for(int j=0; j<L.size(); ++j){
        if(LR[L.at(j)]>0){
          LR[L.at(j)] -= 1;
          if(B=='L') LR[L.at(j-1)] += 1;
          else LR[L.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='Z'){
      for(int j=0; j<Z.size(); ++j){
        if(LR[Z.at(j)]>0){
          LR[Z.at(j)] -= 1;
          if(B=='L') LR[Z.at(j-1)] += 1;
          else LR[Z.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='X'){
      for(int j=0; j<X.size(); ++j){
        if(LR[X.at(j)]>0){
          LR[X.at(j)] -= 1;
          if(B=='L') LR[X.at(j-1)] += 1;
          else LR[X.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='C'){
      for(int j=0; j<C.size(); ++j){
        if(LR[C.at(j)]>0){
          LR[C.at(j)] -= 1;
          if(B=='L') LR[C.at(j-1)] += 1;
          else LR[C.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='V'){
      for(int j=0; j<V.size(); ++j){
        if(LR[V.at(j)]>0){
          LR[V.at(j)] -= 1;
          if(B=='L') LR[V.at(j-1)] += 1;
          else LR[V.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='B'){
      for(int j=0; j<B.size(); ++j){
        if(LR[B.at(j)]>0){
          LR[B.at(j)] -= 1;
          if(B=='L') LR[B.at(j-1)] += 1;
          else LR[B.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='N'){
      for(int j=0; j<N.size(); ++j){
        if(LR[N.at(j)]>0){
          LR[N.at(j)] -= 1;
          if(B=='L') LR[N.at(j-1)] += 1;
          else LR[N.at(j-1)] += 1;
        }
      }
    }
    
    else if(A=='M'){
      for(int j=0; j<M.size(); ++j){
        if(LR[M.at(j)]>0){
          LR[M.at(j)] -= 1;
          if(B=='L') LR[M.at(j-1)] += 1;
          else LR[M.at(j-1)] += 1;
        }
      }
    }
    
  }
  
  int ans=0;
  for(int i=1; i<=N; ++i) ans += LR[i];
  
  cout<< ans <<endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:15:15: error: conflicting declaration 'std::vector<int> Q'
   15 |   vector<int> Q;
      |               ^
a.cc:8:9: note: previous declaration as 'int Q'
    8 |   int N,Q;
      |         ^
a.cc:28:15: error: conflicting declaration 'std::vector<int> S'
   28 |   vector<int> S;
      |               ^
a.cc:13:8: note: previous declaration as 'char S [(N + 1)]'
   13 |   char S[N+1];
      |        ^
a.cc:37:3: error: expected initializer before 'vector'
   37 |   vector<int> Z;
      |   ^~~~~~
a.cc:43:15: error: conflicting declaration 'std::vector<int> N'
   43 |   vector<int> N;
      |               ^
a.cc:8:7: note: previous declaration as 'int N'
    8 |   int N,Q;
      |       ^
a.cc:49:21: error: request for member 'push_back' in 'Q', which is of non-class type 'int'
   49 |     if(S[i]=='Q') Q.push_back(i);
      |                     ^~~~~~~~~
a.cc:62:26: error: request for member 'push_back' in 'S', which is of non-class type 'char [(N + 1)]'
   62 |     else if(S[i]=='S') S.push_back(i);
      |                          ^~~~~~~~~
a.cc:70:24: error: 'L' was not declared in this scope
   70 |     else if(S[i]=='L') L.push_back(i);
      |                        ^
a.cc:71:24: error: 'Z' was not declared in this scope
   71 |     else if(S[i]=='Z') Z.push_back(i);
      |                        ^
a.cc:77:26: error: request for member 'push_back' in 'N', which is of non-class type 'int'
   77 |     else if(S[i]=='N') N.push_back(i);
      |                          ^~~~~~~~~
a.cc:88:24: error: request for member 'size' in 'Q', which is of non-class type 'int'
   88 |       for(int j=0; j<Q.size(); ++j){
      |                        ^~~~
a.cc:89:17: error: request for member 'at' in 'Q', which is of non-class type 'int'
   89 |         if(LR[Q.at(j)]>0){
      |                 ^~
a.cc:90:16: error: request for member 'at' in 'Q', which is of non-class type 'int'
   90 |           LR[Q.at(j)] -= 1;
      |                ^~
a.cc:91:27: error: request for member 'at' in 'Q', which is of non-class type 'int'
   91 |           if(B=='L') LR[Q.at(j-1)] += 1;
      |                           ^~
a.cc:92:21: error: request for member 'at' in 'Q', which is of non-class type 'int'
   92 |           else LR[Q.at(j-1)] += 1;
      |                     ^~
a.cc:188:24: error: request for member 'size' in 'A', which is of non-class type 'char'
  188 |       for(int j=0; j<A.size(); ++j){
      |                        ^~~~
a.cc:189:17: error: request for member 'at' in 'A', which is of non-class type 'char'
  189 |         if(LR[A.at(j)]>0){
      |                 ^~
a.cc:190:16: error: request for member 'at' in 'A', which is of non-class type 'char'
  190 |           LR[A.at(j)] -= 1;
      |                ^~
a.cc:191:27: error: request for member 'at' in 'A', which is of non-class type 'char'
  191 |           if(B=='L') LR[A.at(j-1)] += 1;
      |                           ^~
a.cc:192:21: error: request for member 'at' in 'A', which is of non-class type 'char'
  192 |           else LR[A.at(j-1)] += 1;
      |                     ^~
a.cc:198:24: error: request for member 'size' in 'S', which is of non-class type 'char [(N + 1)]'
  198 |       for(int j=0; j<S.size(); ++j){
      |                        ^~~~
a.cc:199:17: error: request for member 'at' in 'S', which is of non-class type 'char [(N + 1)]'
  199 |         if(LR[S.at(j)]>0){
      |                 ^~
a.cc:200:16: error: request for member 'at' in 'S', which is of non-class type 'char [(N + 1)]'
  200 |           LR[S.at(j)] -= 1;
      |                ^~
a.cc:201:27: error: request for member 'at' in 'S', which is of non-class type 'char [(N + 1)]'
  201 |           if(B=='L') LR[S.at(j-1)] += 1;
      |                           ^~
a.cc:202:21: error: request for member 'at' in 'S', which is of non-class type 'char [(N + 1)]'
  202 |           else LR[S.at(j-1)] += 1;
      |                     ^~
a.cc:268:22: error: 'L' was not declared in this scope
  268 |       for(int j=0; j<L.size(); ++j){
      |                      ^
a.cc:278:22: error: 'Z' was not declared in this scope
  278 |       for(int j=0; j<Z.size(); ++j){
      |                      ^
a.cc:318:24: error: request for member 'size' in 'B', which is of non-class type 'char'
  318 |       for(int j=0; j<B.size(); ++j){
      |                        ^~~~
a.cc:319:17: error: request for member 'at' in 'B', which is of non-class type 'char'
  319 |         if(LR[B.at(j)]>0){
      |                 ^~
a.cc:320:16: error: request for member 'at' in 'B', which is of non-class type 'char'
  320 |           LR[B.at(j)] -= 1;
      |                ^~
a.cc:321:27: error: request for member 'at' in 'B', which is of non-class type 'char'
  321 |           if(B=='L') LR[B.at(j-1)] += 1;
      |                           ^~
a.cc:322:21: error: request for member 'at' in 'B', which is of non-class type 'char'
  322 |           else LR[B.at(j-1)] += 1;
      |                     ^~
a.cc:328:24: error: request for member 'size' in 'N', which is of non-class type 'int'
  328 |       for(int j=0; j<N.size(); ++j){
      |                        ^~~~
a.cc:329:17: error: request for member 'at' in 'N', which is of non-class type 'int'
  329 |         if(LR[N.at(j)]>0){
      |                 ^~
a.cc:330:16: error: request for member 'at' in 'N', which is of non-class type 'int'
  330 |           LR[N.at(j)] -= 1;
      |                ^~
a.cc:331:27: error: request for member 'at' in 'N', which is of non-class type 'int'
  331 |           if(B=='L') LR[N.at(j-1)] += 1;
      |                           ^~
a.cc:332:21: error: request for member 'at' in 'N', which is of non-class type 'int'
  332 |           else LR[N.at(j-1)] += 1;
      |                     ^~