Problem p02671 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int res[531441];
int P[14];
int nn;
void dfs(int id, int p, int q, string s) {
  if (id == nn) {
    res[p] = q;
    return;
  }
  int n = s.size();
  string t;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'S') {
      if (t.size() != 0 && t.back() == 'S') {
        t.pop_back();
      } else {
        t.push_back(s[i]);
      }
    } else {
      t.push_back(s[i]);
    }
  }
  for (int pp = 0; pp < 3; pp++) {
    string nxt;
    int k = pp;
    for (int i = 0; i < t.size(); i++) {
      if (t[i] == 'S') {
        if (k == 1 || k == 2) {
          k = 3 - k;
        }
        nxt.push_back('S');
      } else if (t[i] == 'R') {
        k++;
        if (k == 3) {
          nxt.push_back('R');
          k = 0;
        }
      }
    }
    dfs(id + 1, p + P[id] * pp, q + P[id] * k, nxt);
  }
}
int main() {
  string s;
  cin >> nn >> s;
  P[0] = 1;
  for (int i = 1; i <= 12; i++) {
    P[i] = P[i - 1] * 3;
  }
  dfs(0, 0, 0, s);
  int k = 1;
  for (int i = 0; i < nn; i++) {
    k *= 3;
  }
  rep(i, k) { cout << res[i] << " "; }
  cout << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(), (x).end()

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int res[531441];
int P[14];
int nn;
void dfs(int id, int p, int q, string s) {
  if (id == nn) {
    res[p] = q;
    return;
  }
  int n = s.size();
  string t;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'S') {
      if (t.size() != 0 && t.back() == 'S') {
        t.pop_back();
      } else {
        t.push_back(s[i]);
      }
    } else {
      t.push_back(s[i]);
    }
  }
  for (int pp = 0; pp < 3; pp++) {
    string nxt;
    int k = pp;
    for (int i = 0; i < n; i++) {
      if (t[i] == 'S') {
        if (k == 1 || k == 2) {
          k = 3 - k;
        }
        nxt.push_back('S');
      } else if (t[i] == 'R') {
        k++;
        if (k == 3) {
          nxt.push_back('R');
          k = 0;
        }
      }
    }
    dfs(id + 1, p + P[id] * pp, q + P[id] * k, nxt);
  }
}
int main() {
  string s;
  cin >> nn >> s;
  P[0] = 1;
  for (int i = 1; i <= 12; i++) {
    P[i] = P[i - 1] * 3;
  }
  dfs(0, 0, 0, s);
  int k = 1;
  for (int i = 0; i < nn; i++) {
    k *= 3;
  }
  rep(i, k) { cout << res[i] << " "; }
  cout << endl;
  return 0;
}