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

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

int Power3(int p) {
  int r = 1;
  for (int i = 0; i < p; i++) {
    r *= 3;
  }
  return r;
}

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

  int n;
  cin >> n;
  string original_command;
  cin >> original_command;
  vector<vector<int>> output_table(n + 1);
  vector<vector<string>> command_table(n + 1);
  for (int k = 1; k <= n; k++) {
    output_table[k].resize(Power3(k));
    command_table[k].resize(Power3(k));
  }

  for (int k = 1; k <= n; k++) {
    for (int x = 0; x < Power3(k); x++) {
      string command;
      if (k == 1) {
        command = original_command;
      } else {
        command = command_table[k - 1][x % Power3(k - 1)];
      }

      int d = x / Power3(k - 1);
      string next_command = "";
      for (char c : command) {
        if (c == 'R') {
          d++;
          if (d == 3) {
            d = 0;
            next_command.append("R");
          }
        } else if (c == 'S') {
          if (d == 1)
            d = 2;
          else if (d == 2)
            d = 1;

          next_command.append("S");
        }
      }

      next_command = std::regex_replace(next_command, std::regex("SS"), "");

      output_table[k][x] = d;
      command_table[k][x] = next_command;
    }
  }

  vector<int> outputs;
  for (int x = 0; x < Power3(n); x++) {
    int output = 0;
    for (int k = 1; k <= n; k++) {
      output += output_table[k][x % Power3(k)] * Power3(k - 1);
    }
    outputs.push_back(output);
  }

  for (int i = 0; i < outputs.size(); i++) {
    if (i > 0)
      cout << " ";
    cout << outputs[i];
  }
  cout << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

int Power3(int p) {
  int r = 1;
  for (int i = 0; i < p; i++) {
    r *= 3;
  }
  return r;
}

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

  int n;
  cin >> n;
  string original_command;
  cin >> original_command;
  vector<vector<int>> output_table(n + 1);
  vector<vector<string>> command_table(n + 1);
  for (int k = 1; k <= n; k++) {
    output_table[k].resize(Power3(k));
    command_table[k].resize(Power3(k));
  }

  for (int k = 1; k <= n; k++) {
    for (int x = 0; x < Power3(k); x++) {
      // cout << "k: " << k << " x: " << x << endl;

      string command;
      if (k == 1) {
        command = original_command;
      } else {
        command = command_table[k - 1][x % Power3(k - 1)];
      }

      int d = x / Power3(k - 1);
      string next_command = "";
      for (char c : command) {
        if (c == 'R') {
          d++;
          if (d == 3) {
            d = 0;
            next_command.append("R");
          }
        } else if (c == 'S') {
          if (d == 1)
            d = 2;
          else if (d == 2)
            d = 1;

          next_command.append("S");
        }
      }

      // next_command = std::regex_replace(next_command, std::regex("SS"), "");

      output_table[k][x] = d;
      command_table[k][x] = next_command;
    }
  }

  vector<int> outputs;
  for (int x = 0; x < Power3(n); x++) {
    int output = 0;
    for (int k = 1; k <= n; k++) {
      output += output_table[k][x % Power3(k)] * Power3(k - 1);
    }
    outputs.push_back(output);
  }

  for (int i = 0; i < outputs.size(); i++) {
    if (i > 0)
      cout << " ";
    cout << outputs[i];
  }
  cout << endl;
}