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

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (long long i = 0; i < (long long)N; i++)
int N, x, y, pos, tri0, tri1, tri2, pw3[13] = {1}, A[1000001], salsa[531441];
char T[200001], sw[1000001];
void OUT(long long x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
char *replace(char *s, const char *before, const char *after) {
  assert(s != (char *)NULL);
  assert(before != (char *)NULL);
  assert(after != (char *)NULL);
  const size_t szb = strlen(before), sza = strlen(after);
  char *pt = s;
  if (szb == 0)
    return s;
  for (;;) {
    pt = strstr(pt, before);
    if (pt == (char *)NULL)
      break;
    const char *remain = pt + szb;
    memmove(pt + sza, remain, strlen(remain) + 1);
    memcpy(pt, after, sza);
    pt += sza;
  }
  return s;
}
signed main(void) {
  x = scanf("%d %s", &N, T + 1);
  replace(T + 1, "SS", "");
  if (T[0] == '\\n')
    return 0;
  rep(i, N) pw3[i + 1] = 3 * pw3[i];
  tri0 = pw3[N], tri1 = pw3[N / 2], tri2 = pw3[N - N / 2];
  rep(i, tri0) salsa[i] = salsa[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, tri1) {
    x = i, pos = 0;
    rep(j, strlen(T + 1)) {
      if (T[j + 1] == 'S') {
        x = salsa[x];
        if (sw[pos] == 's')
          pos--;
        else
          sw[++pos] = 's';
      }
      if (T[j + 1] == 'R') {
        x++;
        if (x == tri1)
          sw[++pos] = 'r', x = 0;
      }
    }
    rep(j, tri2) {
      y = j;
      rep(k, pos) {
        if (sw[k + 1] == 's')
          y = salsa[y];
        else if (sw[k + 1] == 'r') {
          if ((++y) == tri2)
            y = 0;
        }
      }
      A[i + j * tri1] = y * tri1 + x;
    }
  }
  rep(i, tri0) OUT(A[i]), putchar(' ');
}

Error: TLE

Fixed submission:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, N) for (long long i = 0; i < (long long)N; i++)
int N, o, p, x, y, pos, tri0, tri1, tri2, pw3[13] = {1}, A[1000001],
                                          salsa[531441];
char T[200001], sw[1000001];
void OUT(long long x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    OUT(x / 10);
  putchar(x % 10 + 48);
}
char *replace(char *s, const char *before, const char *after) {
  assert(s != (char *)NULL);
  assert(before != (char *)NULL);
  assert(after != (char *)NULL);
  const size_t szb = strlen(before), sza = strlen(after);
  char *pt = s;
  if (szb == 0)
    return s;
  for (;;) {
    pt = strstr(pt, before);
    if (pt == (char *)NULL)
      break;
    const char *remain = pt + szb;
    memmove(pt + sza, remain, strlen(remain) + 1);
    memcpy(pt, after, sza);
    pt += sza;
  }
  return s;
}
void Dance(char c[]) {
  if (c[p] == EOF)
    return;
  if (c[p] == 'S') {
    x = salsa[x];
    if (sw[pos] == 's')
      pos--;
    else
      sw[++pos] = 's';
  }
  if (c[p] == 'R') {
    x++;
    if (x == tri1)
      sw[++pos] = 'r', x = 0;
  }
}
void Update(void) {
  rep(i, tri2) {
    y = i;
    rep(j, pos) {
      if (sw[j + 1] == 's')
        y = salsa[y];
      else if (sw[j + 1] == 'r') {
        if ((++y) == tri2)
          y = 0;
      }
    }
    A[o + i * tri1] = y * tri1 + x;
  }
}
signed main(void) {
  o = scanf("%d %s", &N, T + 1);
  replace(T + 1, "SS", "");
  if (T[0] == '\\n')
    return 0;
  rep(i, N) pw3[i + 1] = 3 * pw3[i];
  tri0 = pw3[N], tri1 = pw3[N / 2], tri2 = pw3[N - N / 2];
  rep(i, tri0) salsa[i] = salsa[i / 3] * 3 + (3 - i % 3) % 3;
  rep(i, tri1) {
    o = i, x = i, pos = 0;
    rep(j, strlen(T + 1)) { p = j + 1, Dance(T); }
    Update();
  }
  rep(i, tri0) OUT(A[i]), putchar(' ');
}