Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)

/* REPmacro */
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) for(int i = 0; i < (n); i++)
#define EACH(begin, end) for(auto itr = (begin); itr != (end); itr++)

/* function */
#define IN(x) cin >> x
#define DEBUG(x) cerr << (x) << " "
#define PRINT(x) cout << (x) << endl
#define BR cout << endl

/* const */
const int INF = 1001001; // 10^6
const ll LINF = 1001001001001001001; // 10^18
const int MOD = 1e9 + 7;



int N = 0;
int Q = 0;
string str;
char cell[INF];
int ret = 0;
int dp[INF][INF];
vector<char> t;
vector<char> d;

int canStay(int n, int q){
  if(dp[n][q] != -1) {
    return dp[n][q];
  }else if(q == Q) {
    return 1;
  }else{
    int now = n;
    if(t[q+1] == cell[n]){
      //呪文を受ける
      if(d[q+1] == 'L'){
        now--;
      }else if(d[q+1] == 'R'){
        now++;
      }else{
        DEBUG("WRONG");
      }

      if(now == 0 || now == N+1){
        dp[n][q] = 0;
        return 0;
      } else {
        int ans = canStay(now, q+1);
        dp[n][q] = ans;
        return ans;
      }
    }else{
      //呪文を受けない
      int ans = canStay(n, q+1);
      dp[n][q] = ans;
      return ans;
    }
  }
}

void input() {
  IN(N);
  IN(Q);
  IN(str);
  REP(i,N){
    cell[i+1] = str[i];
  }
  t.push_back('b');
  d.push_back('b');
  REP(i,Q){
    char c;
    char dir;
    IN(c);
    IN(dir);
    t.push_back(c);
    d.push_back(dir);
  }
  REP(i, N+3){
    REP(j, Q+3){
      dp[i][j] = -1; //初期化
    }
  }
}

void solve() {
  REP(i,N){
    int ans = canStay(i+1,0);
    if(ans == 1){
      ret++;
    }
  }
  // REP(i, N){
  //   REP(j, Q){
  //     DEBUG(dp[i+1][j]);
  //   }
  //   DEBUG("\n");
  // }
}

int main(void){
  input();
  solve();
  PRINT(ret);
}


Compiler output:
/tmp/ccgA0IyP.o: in function `canStay(int, int)':
a.cc:(.text+0x8f): relocation truncated to fit: R_X86_64_PC32 against symbol `t' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0xc9): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0xf5): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccgA0IyP.o
/tmp/ccgA0IyP.o: in function `input()':
a.cc:(.text+0x2b8): relocation truncated to fit: R_X86_64_PC32 against symbol `t' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x2d2): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x31d): relocation truncated to fit: R_X86_64_PC32 against symbol `t' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x333): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccgA0IyP.o
/tmp/ccgA0IyP.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x478): relocation truncated to fit: R_X86_64_PC32 against symbol `t' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x491): relocation truncated to fit: R_X86_64_PC32 against symbol `t' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x4aa): relocation truncated to fit: R_X86_64_PC32 against symbol `d' defined in .bss section in /tmp/ccgA0IyP.o
a.cc:(.text+0x4c3): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status