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

#include <bits/stdc++.h>
using namespace std;
int n, i, s[105], j, k, l;
int d[1000005], td[1000005];
char c[1000005];
string p[1000005], tmp[1000005];
int main() {
  scanf("%d", &n);
  s[0] = 1;
  for (i = 1; i <= n; ++i)
    s[i] = s[i - 1] * 3;
  cin >> p[0];
  for (i = 0; i < n; ++i) {
    for (k = 0; k < s[i]; ++k)
      tmp[k].clear();
    for (j = 0; j < 3; ++j) {
      for (k = 0; k < s[i]; ++k) {
        int u = j, w = 0;
        for (l = 0; l < p[k].size(); ++l)
          if (p[k][l] == 'S') {
            if (u == 1)
              u = 2;
            else if (u == 2)
              u = 1;
            w ^= 1;
          } else {
            ++u;
            if (u >= 3) {
              if (w) {
                tmp[j * s[i] + k] += 'S';
                w = 0;
              }
              u = 0;
              tmp[j * s[i] + k] += 'R';
            }
          }
        if (w)
          tmp[j * s[i] + k] += 'S';
        d[j * s[i] + k] = td[k] + u * s[i];
      }
    }
    for (k = 0; k < s[i + 1]; ++k) {
      td[k] = d[k];
      p[k] = tmp[k];
    }
  }
  for (i = 0; i < s[n]; ++i)
    printf("%d ", d[i]);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int M = 998244353;
const int N = 500005, E = 524288;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int n, i, j, k, a[15], su[N], sy[N], vis[N], e, ans[N];
char c[N];
vector<int> g[N];
string p[6666];
int main() {
  scanf("%d", &n);
  assert(n & 1);
  scanf("%s", c);
  int m = min(n / 2, 6);
  for (i = 1; c[i]; ++i)
    if (c[i] == 'S' && c[i - 1] == 'S') {
      vis[i] = vis[i - 1] = 1;
      ++i;
    }
  k = 0;
  for (i = 0; c[i]; ++i)
    if (!vis[i])
      c[k++] = c[i];
  c[k] = 0;
  int s = 1;
  for (i = 1; i <= m; ++i)
    s *= 3;
  for (i = 0; i < s; ++i) {
    int tmp = i;
    k = 0;
    while (tmp) {
      a[k++] = tmp % 3;
      tmp /= 3;
    }
    for (j = 0; c[j]; ++j)
      if (c[j] == 'S') {
        for (k = 0; k < m; ++k)
          a[k] = a[k] * 2 % 3;
      } else {
        ++a[k = 0];
        while (k < m && a[k] > 2) {
          a[k] = 0;
          ++a[++k];
        }
        if (k == m)
          g[i].push_back(j);
      }
    for (j = m - 1; j >= 0; --j)
      sy[i] = sy[i] * 3 + a[j];
    // cout<<sy[i]<<' ';
  }
  // cout<<endl;
  int s2 = 1;
  for (i = 1; i <= n - m; ++i)
    s2 *= 3;
  su[0] = 1;
  for (i = 1; c[i - 1]; ++i)
    if (c[i - 1] == 'S')
      su[i] = su[i - 1] * 2 % 3;
    else
      su[i] = su[i - 1];
  int len = strlen(c);
  for (j = 0; j < s; ++j) {
    int y = 0, las = -1;
    for (auto k : g[j]) {
      if (su[k + 1] * su[las + 1] % 3 == 2)
        p[j] += 'S';
      p[j] += 'R';
      las = k;
    }
    if (su[len] * su[las + 1] % 3 == 2)
      p[j] += 'S';
  }
  for (i = 0; i < s2; ++i) {
    for (e = 0; e < s; ++e) {
      int tmp = i;
      k = 0;
      while (tmp) {
        a[k++] = tmp % 3;
        tmp /= 3;
      }
      for (j = k; j <= n - m; ++j)
        a[j] = 0;
      for (j = 0; j < p[e].size(); ++j)
        if (p[e][j] == 'S') {
          for (k = 0; k < n - m; ++k)
            a[k] = a[k] * 2 % 3;
        } else {
          // cout<<a[0]<<endl;
          ++a[k = 0];
          while (k < n - m && a[k] > 2) {
            a[k] = 0;
            ++a[++k];
          }
          // cout<<a[0]<<' '<<a[1]<<endl;
        }
      int t = 0;
      for (k = n - m - 1; k >= 0; --k)
        t = t * 3 + a[k];
      // cout<<i<<' '<<t<<' '<<p[e]<<endl;
      ans[i * s + e] = t * s + sy[e];
    }
  }
  for (i = 0; i < s * s2; ++i)
    printf("%d ", ans[i]);
}