Problem p02671 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 13;
const int siz = 55e4 + 100;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define nfr(i, a, b) for (int i = a; i >= b; --i)

int d[N][siz], a[N][siz], w[N][siz];
int tag[N][siz];
int n, m;
char ch[siz];
int main() {
  scanf("%d", &n);
  scanf("%s", ch + 1);
  m = strlen(ch + 1);

  a[0][0] = 0;
  fr(i, 0, m) w[0][i] = 0;

  int cur = 1;
  fr(i, 1, n) {
    cur *= 3;
    w[i][0] = cur - 1;
    fr(j, 0, cur - 1) d[i][j] = j / (cur / 3);

    int tg = 0;
    fr(j, 1, m) {
      if (ch[j] == 'S') {
        tg ^= 1;
        int p = w[i - 1][j - 1];
        fr(k, 0, 2) {
          if (d[i][p + k * (cur / 3)] == 0) {
            tag[i][p + k * (cur / 3)] = tg;
            continue;
          }
          d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
          tag[i][p + k * (cur / 3)] = tg;
        }
      } else {
        int p = w[i - 1][j - 1];
        fr(k, 0, 2) {
          if (d[i][p + k * (cur / 3)] != 0)
            d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
          tag[i][p + k * (cur / 3)] = tg;

          d[i][p + k * (cur / 3)]++;
          d[i][p + k * (cur / 3)] %= 3;
        }
      }

      int p = w[i - 1][j];
      fr(k, 0, 2) {
        if (d[i][p + k * (cur / 3)] != 0)
          d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
        tag[i][p + k * (cur / 3)] = tg;

        if (d[i][p + k * (cur / 3)] == 2)
          w[i][j] = p + k * (cur / 3);
      }
    }

    fr(j, 0, cur - 1) {
      if (d[i][j] == 0)
        continue;
      d[i][j] ^= 3 * (tag[i][j] ^ tg);
    }
    fr(j, 0, cur - 1) a[i][j] = a[i - 1][j % (cur / 3)] + (cur / 3) * d[i][j];
  }

  fr(i, 0, cur - 1) { printf("%d ", a[n][i]); }
  puts("");
  return 0;
}
/*
2
SRSRRSRRRSRRRR
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 21;
const int siz = 55e4 + 100;
#define fr(i, a, b) for (int i = a; i <= b; ++i)
#define nfr(i, a, b) for (int i = a; i >= b; --i)

int d[N][siz], a[N][siz], w[N][siz];
int tag[N][siz];
int n, m;
char ch[siz];
int main() {
  scanf("%d", &n);
  scanf("%s", ch + 1);
  m = strlen(ch + 1);

  a[0][0] = 0;
  fr(i, 0, m) w[0][i] = 0;

  int cur = 1;
  fr(i, 1, n) {
    cur *= 3;
    w[i][0] = cur - 1;
    fr(j, 0, cur - 1) d[i][j] = j / (cur / 3);

    int tg = 0;
    fr(j, 1, m) {
      if (ch[j] == 'S') {
        tg ^= 1;
        int p = w[i - 1][j - 1];
        fr(k, 0, 2) {
          if (d[i][p + k * (cur / 3)] == 0) {
            tag[i][p + k * (cur / 3)] = tg;
            continue;
          }
          d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
          tag[i][p + k * (cur / 3)] = tg;
        }
      } else {
        int p = w[i - 1][j - 1];
        fr(k, 0, 2) {
          if (d[i][p + k * (cur / 3)] != 0)
            d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
          tag[i][p + k * (cur / 3)] = tg;

          d[i][p + k * (cur / 3)]++;
          d[i][p + k * (cur / 3)] %= 3;
        }
      }

      int p = w[i - 1][j];
      fr(k, 0, 2) {
        if (d[i][p + k * (cur / 3)] != 0)
          d[i][p + k * (cur / 3)] ^= 3 * (tag[i][p + k * (cur / 3)] ^ tg);
        tag[i][p + k * (cur / 3)] = tg;

        if (d[i][p + k * (cur / 3)] == 2)
          w[i][j] = p + k * (cur / 3);
      }
      cerr << "wij" << ' ' << i << ' ' << j << ' ' << w[i][j] << endl;
    }

    fr(j, 0, cur - 1) {
      if (d[i][j] == 0)
        continue;
      d[i][j] ^= 3 * (tag[i][j] ^ tg);
    }
    fr(j, 0, cur - 1) a[i][j] = a[i - 1][j % (cur / 3)] + (cur / 3) * d[i][j],
                      cerr << a[i][j] << ' ';
    cerr << endl;
  }

  fr(i, 0, cur - 1) { printf("%d ", a[n][i]); }
  puts("");
  return 0;
}
/*
2
SRSRRSRRRSRRRR
*/