Problem p02671 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}

vector<vector<int>> v1[15];
vector<vector<int>> vs;

int main() {
  int n, m;
  int i, j, k;
  int a, b, c;
  a = 0;
  string sa;
  cin >> n;
  cin >> sa;
  v1[0].push_back(vector<int>());
  for (i = 0; i < sa.size(); i++) {
    if (sa[i] == 'R')
      a++;
    else if (i + 1 < sa.size() && sa[i + 1] == 'S')
      i++;
    else {
      v1[0][0].push_back(a);
      a = 0;
    }
  }
  v1[0][0].push_back(a);
  vs.push_back(vector<int>(1, 0));

  for (i = 0; i < n; i++) {
    c = pow(3, i);
    vs.push_back(vector<int>());
    for (j = 0; j < v1[i].size(); j++) {
      for (k = 0; k < 3; k++) {
        v1[i + 1].push_back(vector<int>());
        a = k;
        int cnt = 0;
        for (auto node : v1[i][j]) {
          if (cnt && a > 0)
            a = 3 - a;
          a += node, cnt++;
          b = a / 3;
          a %= 3;
          v1[i + 1].back().push_back(b);
          /*
          if(b==0 && !v1[i+1].back().empty() && v1[i+1].back().back()==0){
                  v1[i+1].back().pop_back();
          }else v1[i+1].back().push_back(b);*/
        }
        vs[i + 1].push_back(vs[i][j] + c * a);
      }
    }
  }

  c = pow(3, n);
  vector<int> v2(c, -1);
  vector<int> vt(n, 0);
  for (i = 0; i < c; i++) {
    a = 0;
    for (j = 0; j < n; j++) {
      if (vt[j] >= 3)
        vt[j] -= 3, vt[j + 1]++;
      a *= 3;
      a += vt[j];
    }
    v2[a] = vs[n][i];
    vt[0]++;
  }
  array_show(v2);
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int LL;
typedef pair<int, int> P;
typedef pair<LL, int> LP;
const int INF = 1 << 30;
const LL MAX = 1e9 + 7;

void array_show(int *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(LL *array, int array_n, char middle = ' ') {
  for (int i = 0; i < array_n; i++)
    printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n'));
}
void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}
void array_show(vector<LL> &vec_s, int vec_n = -1, char middle = ' ') {
  if (vec_n == -1)
    vec_n = vec_s.size();
  for (int i = 0; i < vec_n; i++)
    printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n'));
}

vector<vector<int>> v1[15];
vector<vector<int>> vs;

int main() {
  int n, m;
  int i, j, k;
  int a, b, c;
  a = 0;
  string sa;
  cin >> n;
  cin >> sa;
  v1[0].push_back(vector<int>());
  for (i = 0; i < sa.size(); i++) {
    if (sa[i] == 'R')
      a++;
    else if (i + 1 < sa.size() && sa[i + 1] == 'S')
      i++;
    else {
      v1[0][0].push_back(a);
      a = 0;
    }
  }
  v1[0][0].push_back(a);
  vs.push_back(vector<int>(1, 0));

  for (i = 0; i < n; i++) {
    c = pow(3, i);
    vs.push_back(vector<int>());
    for (j = 0; j < v1[i].size(); j++) {
      for (k = 0; k < 3; k++) {
        v1[i + 1].push_back(vector<int>());
        a = k;
        int cnt = 0;
        for (auto node : v1[i][j]) {
          if (cnt && a > 0)
            a = 3 - a;
          a += node, cnt++;
          b = a / 3;
          a %= 3;
          if (b == 0 && !v1[i + 1].back().empty() &&
              v1[i + 1].back().back() == 0 && cnt != v1[i][j].size()) {
            v1[i + 1].back().pop_back();
          } else
            v1[i + 1].back().push_back(b);
        }
        vs[i + 1].push_back(vs[i][j] + c * a);
      }
    }
  }

  c = pow(3, n);
  vector<int> v2(c, -1);
  vector<int> vt(n, 0);
  for (i = 0; i < c; i++) {
    a = 0;
    for (j = 0; j < n; j++) {
      if (vt[j] >= 3)
        vt[j] -= 3, vt[j + 1]++;
      a *= 3;
      a += vt[j];
    }
    v2[a] = vs[n][i];
    vt[0]++;
  }
  array_show(v2);
}