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

/*input
3
SRSRRSRRRSRRRR
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
  REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
  return out;
}
//}}}
const ll maxn = 300005;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

const int N = 531441;

int n;
string s;
vector<int> a;

string op[13][N + 5];

int res[13][N + 5];

int main() {
  IOS;
  cin >> n >> s;
  string tmp;
  REP(i, SZ(s)) {
    if (s[i] == 'R')
      tmp.pb(s[i]);
    else {
      if (SZ(tmp) && tmp.back() == 'S')
        tmp.pop_back();
      else
        tmp.pb(s[i]);
    }
  }

  op[0][0] = tmp;
  res[0][0] = 0;

  int mx = 1;
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < mx; j++) {
        int msb = i;
        op[len][i * mx + j].clear();
        for (char ch : op[len - 1][j]) {
          if (ch == 'S') {
            if (msb)
              msb = 3 - msb;
            if (SZ(op[len][i * mx + j]) && op[len][i * mx + j].back() == 'S') {
              op[len][i * mx + j].pop_back();
            } else {
              op[len][i * mx + j].pb('S');
            }
          } else {
            msb++;
            if (msb == 3) {
              msb = 0;
              op[len][i * mx + j].pb('R');
            }
          }
        }
        res[len][i * mx + j] = msb * mx + res[len - 1][j];
      }
    mx *= 3;
  }
  REP(i, mx) { cout << res[n][i] << " \\n"[i == mx - 1]; }
  return 0;
}


Error: -11

Fixed submission:

/*input
3
SRSRRSRRRSRRRR
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
  REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
  return out;
}
//}}}
const ll maxn = 300005;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

const int N = 531441;

inline int flip(int n, int len) {
  int bs = 1;
  int ret = 0;
  REP(i, len) {
    int cur = n % 3;
    n /= 3;
    if (cur)
      cur = 3 - cur;
    ret += bs * cur;
    bs *= 3;
  }
  return ret;
}

int n;
string s;
vector<int> a;

string op[12][N];

int res[12][N];

int main() {
  IOS;
  cin >> n >> s;
  string tmp;
  REP(i, SZ(s)) {
    if (s[i] == 'R')
      tmp.pb(s[i]);
    else {
      if (SZ(tmp) && tmp.back() == 'S')
        tmp.pop_back();
      else
        tmp.pb(s[i]);
    }
  }

  op[0][0] = tmp;
  res[0][0] = 0;

  int mx = 1;
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < mx; j++) {
        int msb = i;
        op[len][i * mx + j].clear();
        for (char ch : op[len - 1][j]) {
          if (ch == 'S') {
            if (msb)
              msb = 3 - msb;
            if (SZ(op[len][i * mx + j]) && op[len][i * mx + j].back() == 'S') {
              op[len][i * mx + j].pop_back();
            } else {
              op[len][i * mx + j].pb('S');
            }
          } else {
            msb++;
            if (msb == 3) {
              msb = 0;
              op[len][i * mx + j].pb('R');
            }
          }
        }
        res[len][i * mx + j] = msb * mx + res[len - 1][j];
      }
    mx *= 3;
  }
  REP(i, mx) { cout << res[n][i] << " \\n"[i == mx - 1]; }
  return 0;
}