Problem p00308 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, string> P;
struct S {
  int t, a, b;
};

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int o = 0, x = 0, cnt = 1, os = -1, tmp = 0;
    string s;
    cin >> s;
    if (s.length() <= 2) {
      cout << 0 << endl;
      continue;
    }
    queue<P> que;
    vector<S> vec;
    que.push(P(0, s));
    bool f = true;
    while (!que.empty()) {
      if (tmp++ >= 200) {
        cout << -1 << endl;
        f = false;
        break;
      }
      P p = que.front();
      que.pop();
      string t = p.second;
      int v = p.first;
      // cout << v << " " << t << endl;
      if (t == "ox" || t == "xo")
        continue;
      if (t == "o") {
        os = v;
        continue;
      }
      if (t == "oo") {
        vec.push_back({0, v, 1});
        os = cnt;
        cnt += 2;
        continue;
      }
      if (t.substr(0, 2) == "ox") {
        vec.push_back({0, v, 2});
        cnt++;
        que.push(P(cnt++, t.substr(2, t.length() - 2)));
        continue;
      }
      int ao = 0, ax = 0;
      for (int j = 0; j < t.length(); j++) {
        if (t[j] == 'o')
          ao++;
        else
          ax++;
      }
      o = 0;
      x = 0;
      bool flag = false;
      for (int j = 0; j < t.length() - 1; j++) {
        if (t[j] == 'o')
          o++;
        else
          x++;
        if (o >= x && ao - o >= ax - x) {
          vec.push_back({0, v, j + 1});
          que.push(P(cnt++, t.substr(0, j + 1)));
          que.push(P(cnt++, t.substr(j + 1, t.length() - j - 1)));
          tmp = 0;
          flag = true;
          break;
        }
      }
      if (!flag) {
        if (os == -1)
          que.push(P(v, t));
        else if (t[0] == 'x') {
          vec.push_back({1, os, v});
          que.push(P(cnt++, "o" + t));
          tmp = 0;
        } else {
          vec.push_back({1, v, os});
          que.push(P(cnt++, t + "o"));
          tmp = 0;
        }
        os = -1;
      }
    }
    if (!f)
      continue;
    cout << vec.size() << endl;
    for (S p : vec) {
      if (!p.t)
        cout << "split ";
      else
        cout << "join ";
      cout << p.a << " " << p.b << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, string> P;
struct S {
  int t, a, b;
};

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int o = 0, x = 0, cnt = 1, os = -1, tmp = 0;
    string s;
    cin >> s;
    if (s.length() <= 2) {
      cout << 0 << endl;
      continue;
    }
    queue<P> que;
    vector<S> vec;
    que.push(P(0, s));
    bool f = true;
    while (!que.empty()) {
      if (tmp++ >= 200) {
        cout << -1 << endl;
        f = false;
        break;
      }
      P p = que.front();
      que.pop();
      string t = p.second;
      int v = p.first;
      if (t == "ox" || t == "xo")
        continue;
      if (t == "o") {
        os = v;
        continue;
      }
      if (t == "oo") {
        vec.push_back({0, v, 1});
        os = cnt;
        cnt += 2;
        continue;
      }
      int ao = 0, ax = 0;
      for (int j = 0; j < t.length(); j++) {
        if (t[j] == 'o')
          ao++;
        else
          ax++;
      }
      o = 0;
      x = 0;
      bool flag = false;
      for (int j = 0; j < t.length() - 1; j++) {
        if (t[j] == 'o')
          o++;
        else
          x++;
        if (o >= x && ao - o >= ax - x) {
          vec.push_back({0, v, j + 1});
          que.push(P(cnt++, t.substr(0, j + 1)));
          que.push(P(cnt++, t.substr(j + 1, t.length() - j - 1)));
          tmp = 0;
          flag = true;
          break;
        }
      }
      if (!flag) {
        if (os == -1)
          que.push(P(v, t));
        else if (t[0] == 'x') {
          vec.push_back({1, os, v});
          que.push(P(cnt++, "o" + t));
          tmp = 0;
        } else {
          vec.push_back({1, v, os});
          que.push(P(cnt++, t + "o"));
          tmp = 0;
        }
        os = -1;
      }
    }
    if (!f)
      continue;
    cout << vec.size() << endl;
    for (S p : vec) {
      if (!p.t)
        cout << "split ";
      else
        cout << "join ";
      cout << p.a << " " << p.b << endl;
    }
  }
  return 0;
}