Problem p02671 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
inline int min(int x, int y, int z) { return min(x, min(y, z)); }
inline int max(int x, int y, int z) { return max(x, max(y, z)); }
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
#define N 200005
int a[N], t[15][N];
char s[N];
int id[N], n;
void upd(int &x) {
  if (x == 1)
    x = 2;
  else if (x == 2)
    x = 1;
}
signed main() {
  cin >> n;
  scanf("%s", s + 1);
  int m = strlen(s + 1);
  int cur = 1;
  for (int i = 0; i < n; i++) {
    cur *= 3;
    for (int j = 0; j < cur; j++) {
      int x = j;
      for (int k = 0; k < i; k++)
        x /= 3;
      a[j] = x;
    }
    bool op = 0;
    for (int j = 1; j <= m; j++) {
      if (s[j] == 'S')
        op ^= 1;
      else {
        int u = id[j], tmp = 0;
        for (int k = 0; k < 3; k++) {
          int v = k * (cur / 3) + u;
          if (op)
            upd(a[v]);
          if (a[v] == 2)
            tmp = k, a[v] = 0;
          else
            a[v]++;
          if (op)
            upd(a[v]);
        }
        id[j] += tmp * (cur / 3);
      }
    }
    if (op)
      for (int j = 0; j < cur; j++)
        upd(a[j]);
    for (int j = 0; j < cur; j++)
      t[i][j] = a[j];
    // for(int j=1;j<=cur;j++) printf("%d ",t[i][j]);
    // cout<<"\n";
  }
  for (int i = 0; i < cur; i++) {
    int ans = 0, tmp = 1;
    for (int j = 0; j < n; j++) {
      tmp *= 3;
      ans += t[j][i % tmp] * (tmp / 3);
    }
    printf("%d ", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define Fast_IO ios::sync_with_stdio(false);
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
// mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define fir first
#define sec second
#define mod 998244353
#define ll long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
inline int read() {
  char ch = getchar();
  int nega = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      nega = -1;
    ch = getchar();
  }
  int ans = 0;
  while (isdigit(ch)) {
    ans = ans * 10 + ch - 48;
    ch = getchar();
  }
  if (nega == -1)
    return -ans;
  return ans;
}
typedef pair<int, int> pii;
inline int min(int x, int y, int z) { return min(x, min(y, z)); }
inline int max(int x, int y, int z) { return max(x, max(y, z)); }
inline int add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int add(int x, int y, int z) { return add(add(x, y), z); }
inline int sub(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int mul(int x, int y) { return 1LL * x * y % mod; }
inline int mul(int x, int y, int z) { return mul(mul(x, y), z); }
#define N 600005
int a[N], t[15][N];
char s[N];
int id[N], n;
void upd(int &x) {
  if (x == 1)
    x = 2;
  else if (x == 2)
    x = 1;
}
signed main() {
  cin >> n;
  scanf("%s", s + 1);
  int m = strlen(s + 1);
  int cur = 1;
  for (int i = 0; i < n; i++) {
    cur *= 3;
    for (int j = 0; j < cur; j++) {
      int x = j;
      for (int k = 0; k < i; k++)
        x /= 3;
      a[j] = x;
    }
    bool op = 0;
    for (int j = 1; j <= m; j++) {
      if (s[j] == 'S')
        op ^= 1;
      else {
        int u = id[j], tmp = 0;
        for (int k = 0; k < 3; k++) {
          int v = k * (cur / 3) + u;
          if (op)
            upd(a[v]);
          if (a[v] == 2)
            tmp = k, a[v] = 0;
          else
            a[v]++;
          if (op)
            upd(a[v]);
        }
        id[j] += tmp * (cur / 3);
      }
    }
    if (op)
      for (int j = 0; j < cur; j++)
        upd(a[j]);
    for (int j = 0; j < cur; j++)
      t[i][j] = a[j];
    // for(int j=1;j<=cur;j++) printf("%d ",t[i][j]);
    // cout<<"\n";
  }
  for (int i = 0; i < cur; i++) {
    int ans = 0, tmp = 1;
    for (int j = 0; j < n; j++) {
      tmp *= 3;
      ans += t[j][i % tmp] * (tmp / 3);
    }
    printf("%d ", ans);
  }
  return 0;
}