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

#include <bits/stdc++.h>
using namespace std;

void dif(const string &s) {
  struct Chain {
    int id;
    string s;
  };
  int m = 0;

  vector<string> res;

  int carry = -1;
  queue<Chain> q;
  q.push({m++, s});
  while (!q.empty()) {
    auto chain = q.front();
    q.pop();

    if (~carry) {
      stringstream ss;
      ss << "join " << carry << " " << chain.id;
      res.push_back(ss.str());
      chain.s.insert(begin(chain.s), 'o');
      chain.id = m++;
      carry = -1;
    }

    auto x = count(begin(chain.s), end(chain.s), 'x');
    auto o = count(begin(chain.s), end(chain.s), 'o');
    if (x == 0) {
      while (2 < o && 2 < chain.s.size()) {
        stringstream ss;
        ss << "split " << chain.id << " " << 0;
        res.push_back(ss.str());
        chain.s.erase(0, 1);
        chain.id = (++m)++;
        --o;
      }
      continue;
    }

    while (x + 1 < o && chain.s.front() == 'o') {
      stringstream ss;
      ss << "split " << chain.id << " " << 0;
      res.push_back(ss.str());
      chain.s.erase(0, 1);
      chain.id = (++m)++;
      --o;
    }

    x = o = 0;
    for (int i = 0; i < chain.s.size() - 1; ++i) {
      if (chain.s[i] == 'o')
        ++o;
      if (chain.s[i] == 'x')
        ++x;
      if (!x || x + 1 != o)
        continue;
      stringstream ss;
      ss << "split " << chain.id << " " << i;
      res.push_back(ss.str());
      q.push({m + 1, chain.s.substr(i + 1)});
      chain.s.erase(i + 1);
      chain.id = m;
      m += 2;
      break;
    }

    while (chain.s.back() == 'o') {
      stringstream ss;
      ss << "split " << chain.id << " " << chain.s.size() - 2;
      res.push_back(ss.str());
      chain.s.pop_back();
      ss.str("");
      ss << "join " << m + 1 << " " << m;
      res.push_back(ss.str());
      chain.s.insert(begin(chain.s), 'o');
      chain.id = m + 2;
      m += 3;
    }

    while (chain.s.front() == 'o' && chain.s.back() == 'x') {
      stringstream ss;
      ss << "split " << chain.id << " " << 0;
      res.push_back(ss.str());
      ss.str("");
      ss << "join " << m + 1 << " " << m;
      res.push_back(ss.str());
      ss.str("");
      ss << "split " << m + 2 << " " << chain.s.size() - 3;
      res.push_back(ss.str());
      chain.s.erase(0, 1);
      chain.s.pop_back();
      chain.id = m + 3;
      m += 5;
    }

    carry = chain.id;
  }

  cout << res.size() << endl;
  for (const auto &i : res)
    cout << i << endl;
}

void same(string s) {
  vector<string> res;
  int id = 0;
  while (2 < s.size()) {
    if (s[0] == s[1])
      break;
    stringstream ss;
    ss << "split " << id << " " << 1;
    res.push_back(ss.str());
    id += 2;
    s.erase(0, 2);
  }
  stringstream ss;
  ss << res.size();
  res.insert(begin(res), ss.str());

  if (2 < s.size())
    cout << -1 << endl;
  else
    for (auto &i : res)
      cout << i << endl;
}

void solve(const string &s) {
  auto x = count(begin(s), end(s), 'x');
  auto o = count(begin(s), end(s), 'o');

  if (x > o)
    cout << -1 << endl;
  if (x == o)
    same(s);
  if (x < o)
    dif(s);
}

int main() {
  int Q;
  cin >> Q;

  for (int i = 0; i < Q; ++i) {
    string s;
    cin >> s;

    solve(s);
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

void dif(const string &s) {
  struct Chain {
    int id;
    string s;
  };
  int m = 0;

  vector<string> res;

  int carry = -1;
  queue<Chain> q;
  q.push({m++, s});
  while (!q.empty()) {
    auto &chain = q.front();
    q.pop();

    if (~carry) {
      stringstream ss;
      ss << "join " << carry << " " << chain.id;
      res.push_back(ss.str());
      chain.s.insert(begin(chain.s), 'o');
      chain.id = m++;
      carry = -1;
    }

    auto x = count(begin(chain.s), end(chain.s), 'x');
    auto o = count(begin(chain.s), end(chain.s), 'o');
    if (x == 0) {
      while (2 < o && 2 < chain.s.size()) {
        stringstream ss;
        ss << "split " << chain.id << " " << 0;
        res.push_back(ss.str());
        chain.s.erase(0, 1);
        chain.id = (++m)++;
        --o;
      }
      continue;
    }

    while (x + 1 < o && chain.s.front() == 'o') {
      stringstream ss;
      ss << "split " << chain.id << " " << 0;
      res.push_back(ss.str());
      chain.s.erase(0, 1);
      chain.id = (++m)++;
      --o;
    }

    x = o = 0;
    for (int i = 0; i < chain.s.size() - 1; ++i) {
      if (chain.s[i] == 'o')
        ++o;
      if (chain.s[i] == 'x')
        ++x;
      if (!x || x + 1 != o)
        continue;
      stringstream ss;
      ss << "split " << chain.id << " " << i;
      res.push_back(ss.str());
      q.push({m + 1, chain.s.substr(i + 1)});
      chain.s.erase(i + 1);
      chain.id = (++m)++;
      break;
    }

    while (chain.s.back() == 'o') {
      stringstream ss;
      ss << "split " << chain.id << " " << chain.s.size() - 2;
      res.push_back(ss.str());
      chain.s.pop_back();
      ss.str("");
      ss << "join " << m + 1 << " " << m;
      res.push_back(ss.str());
      chain.s.insert(begin(chain.s), 'o');
      chain.id = m + 2;
      m += 3;
    }

    while (chain.s.front() == 'o' && chain.s.back() == 'x') {
      stringstream ss;
      ss << "split " << chain.id << " " << 0;
      res.push_back(ss.str());
      ss.str("");
      ss << "join " << m + 1 << " " << m;
      res.push_back(ss.str());
      ss.str("");
      ss << "split " << m + 2 << " " << chain.s.size() - 3;
      res.push_back(ss.str());
      chain.s.erase(0, 1);
      chain.s.pop_back();
      chain.id = m + 3;
      m += 5;
    }

    carry = chain.id;
  }

  cout << res.size() << endl;
  for (const auto &i : res)
    cout << i << endl;
}

void same(string s) {
  vector<string> res;
  int id = 0;
  while (2 < s.size()) {
    if (s[0] == s[1])
      break;
    stringstream ss;
    ss << "split " << id << " " << 1;
    res.push_back(ss.str());
    id += 2;
    s.erase(0, 2);
  }
  stringstream ss;
  ss << res.size();
  res.insert(begin(res), ss.str());

  if (2 < s.size())
    cout << -1 << endl;
  else
    for (auto &i : res)
      cout << i << endl;
}

void solve(const string &s) {
  auto x = count(begin(s), end(s), 'x');
  auto o = count(begin(s), end(s), 'o');

  if (x > o)
    cout << -1 << endl;
  if (x == o)
    same(s);
  if (x < o)
    dif(s);
}

int main() {
  int Q;
  cin >> Q;

  for (int i = 0; i < Q; ++i) {
    string s;
    cin >> s;

    solve(s);
  }
}