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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define inf 1e18
#define rep(x, s, t) for (int(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (int(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<int, int> P;

int n;
string s;
int beki[15];
int carry[200005];
int dest[600005], ndest[600005];
bitset<600005> bs[3];
int pt[3];

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  cin >> s;

  beki[0] = 1;
  for (int i = 1; i < 15; i++)
    beki[i] = beki[i - 1] * 3;

  for (int i = 0; i < 3; i++) {
    int p = i;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'R') {
        if (p == 2)
          carry[j] = i;
        p = (p + 1) % 3;
      } else
        p = (3 - p) % 3;
    }
    dest[i] = p;
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < beki[i + 1]; k++)
        bs[j][k] = false;
      for (int k = 0; k < beki[i]; k++)
        bs[j][j * beki[i] + k] = true;
    }
    for (int j = 0; j < 3; j++)
      pt[j] = j;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'S')
        swap(pt[1], pt[2]);
      else {
        int tmp[3];
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 3; l++) {
            int x = l * beki[i] + carry[j];
            if (bs[pt[k]][x]) {
              tmp[k] = x;
              if (k == 2)
                carry[j] = x;
              break;
            }
          }
        }
        for (int k = 0; k < 3; k++) {
          bs[pt[k]][tmp[k]] = false;
          bs[pt[(k + 1) % 3]][tmp[k]] = true;
        }
      }
    }
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < beki[i + 1]; k++) {
        if (bs[pt[j]][k])
          ndest[k] = dest[k % beki[i]] + j * beki[i];
      }
    }
    for (int j = 0; j < beki[i + 1]; j++)
      dest[j] = ndest[j];
  }

  for (int i = 0; i < beki[n]; i++)
    cout << dest[i] << " ";
  cout << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define inf 1e18
#define rep(x, s, t) for (int(x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for (int(x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<int, int> P;

int n;
string s;
int beki[15];
int carry[200005];
int dest[600005], ndest[600005];
bitset<600005> bs[3];

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  cin >> s;

  beki[0] = 1;
  for (int i = 1; i < 15; i++)
    beki[i] = beki[i - 1] * 3;

  for (int i = 0; i < 3; i++) {
    int p = i;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'R') {
        if (p == 2)
          carry[j] = i;
        p = (p + 1) % 3;
      } else
        p = (3 - p) % 3;
    }
    dest[i] = p;
  }

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < beki[i + 1]; k++)
        bs[j][k] = false;
      for (int k = 0; k < beki[i]; k++)
        bs[j][j * beki[i] + k] = true;
    }
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == 'S')
        swap(bs[1], bs[2]);
      else {
        int tmp[3];
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 3; l++) {
            int x = l * beki[i] + carry[j];
            if (bs[k][x]) {
              tmp[k] = x;
              if (k == 2)
                carry[j] = x;
              break;
            }
          }
        }
        for (int k = 0; k < 3; k++) {
          bs[k][tmp[k]] = false;
          bs[(k + 1) % 3][tmp[k]] = true;
        }
      }
    }
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < beki[i + 1]; k++) {
        if (bs[j][k])
          ndest[k] = dest[k % beki[i]] + j * beki[i];
      }
    }
    for (int j = 0; j < beki[i + 1]; j++)
      dest[j] = ndest[j];
  }

  for (int i = 0; i < beki[n]; i++)
    cout << dest[i] << " ";
  cout << endl;

  return 0;
}