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

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 13, MAX_POW3_N = 531441, MAX_M = 200020;

int N, M, Half, pow3[MAX_N], salsa[MAX_POW3_N], highbits[MAX_POW3_N],
    Ans[MAX_POW3_N];

char S[MAX_M];

int main() {
  scanf("%d", &N);
  Half = N / 2;
  scanf("%s", S + 1);
  M = strlen(S + 1);
  pow3[0] = 1;
  for (int i = 1; i <= N; i++)
    pow3[i] = pow3[i - 1] * 3;
  for (int i = 0; i < pow3[N]; i++) {
    int result = 0;
    for (int j = 0; j < N; j++) {
      int t = i / pow3[j] % 3;
      result += (3 - t) % 3 * pow3[j];
    }
    salsa[i] = result;
  }
  for (int i = 0; i < pow3[Half]; i++) {
    int lowbits = i;
    for (int j = 0; j < pow3[N - Half]; j++)
      highbits[j] = j;
    bool high_flip = false;
    for (int j = 1; j <= M; j++) {
      if (S[j] == 'S') {
        high_flip ^= true;
        lowbits = salsa[lowbits];
      } else {
        if (++lowbits == pow3[Half]) {
          lowbits = 0;
          if (high_flip) {
            for (int j = 0; j < pow3[N - Half]; j++) {
              highbits[j] = salsa[highbits[j]];
            }
            high_flip = false;
          }
          for (int j = 0; j < pow3[N - Half]; j++) {
            if (++highbits[j] == pow3[N - Half])
              highbits[j] = 0;
          }
        }
      }
    }
    if (high_flip) {
      for (int j = 0; j < pow3[N - Half]; j++) {
        highbits[j] = salsa[highbits[j]];
      }
    }
    high_flip = false;
    for (int j = 0; j < pow3[N - Half]; j++) {
      Ans[j * pow3[Half] + i] = highbits[j] * pow3[Half] + lowbits;
    }
  }
  for (int i = 0; i < pow3[N]; i++)
    printf("%d ", Ans[i]);
  printf("\\n");
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 13, MAX_M = 200020;

int N, M, Half, pow3[MAX_N], salsa[1 << MAX_N], highbits[1 << MAX_N],
    Ans[1 << MAX_N];

char S[MAX_M];

int main() {
  scanf("%d", &N);
  Half = N / 2;
  scanf("%s", S + 1);
  M = strlen(S + 1);
  pow3[0] = 1;
  for (int i = 1; i <= N; i++)
    pow3[i] = pow3[i - 1] * 3;
  for (int i = 0; i < pow3[N]; i++) {
    int result = 0;
    for (int j = 0; j < N; j++) {
      int t = i / pow3[j] % 3;
      result += (3 - t) % 3 * pow3[j];
    }
    salsa[i] = result;
  }
  for (int i = 0; i < pow3[Half]; i++) {
    int lowbits = i;
    for (int j = 0; j < pow3[N - Half]; j++)
      highbits[j] = j;
    bool high_flip = false;
    for (int j = 1; j <= M; j++) {
      if (S[j] == 'S') {
        high_flip ^= true;
        lowbits = salsa[lowbits];
      } else {
        if (++lowbits == pow3[Half]) {
          lowbits = 0;
          if (high_flip) {
            for (int j = 0; j < pow3[N - Half]; j++) {
              highbits[j] = salsa[highbits[j]];
            }
            high_flip = false;
          }
          for (int j = 0; j < pow3[N - Half]; j++) {
            if (++highbits[j] == pow3[N - Half])
              highbits[j] = 0;
          }
        }
      }
    }
    if (high_flip) {
      for (int j = 0; j < pow3[N - Half]; j++) {
        highbits[j] = salsa[highbits[j]];
      }
    }
    high_flip = false;
    for (int j = 0; j < pow3[N - Half]; j++) {
      Ans[j * pow3[Half] + i] = highbits[j] * pow3[Half] + lowbits;
    }
  }
  for (int i = 0; i < pow3[N]; i++)
    printf("%d ", Ans[i]);
  printf("\\n");
}