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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string dir;
vector<string> ope;

string solve(string s, int i) {
  int n = s.size();
  if (n == 1)
    return s;
  if (s[0] == '(' && s[n - 1] == ')') {
    int m = 0;
    for (int j = 0; j < n - 1; ++j) {
      if (s[j] == '(')
        ++m;
      else if (s[j] == ')') {
        --m;
        if (m == 0)
          break;
      }
    }
    if (m == 1)
      return solve(s.substr(1, n - 2), 0);
  }

  string ret;
  if (dir[i] == 'L') {
    int j = 0;
    int k = 0;
    int m = 0;
    while (k <= n) {
      if (k < n && s[k] == '(')
        ++m;
      if (k < n && s[k] == ')')
        --m;
      if (m == 0 && (k == n || find(ope[i].begin(), ope[i].end(), s[k]) !=
                                   ope[i].end())) {
        if (j > 0)
          ret += s[j - 1];
        ret += solve(s.substr(j, k - j), i + 1);
        if (j > 0)
          ret = '(' + ret + ')';
        j = k + 1;
      }
      ++k;
    }
  } else {
    int j = n - 1;
    int k = n - 1;
    int m = 0;
    while (k >= -1) {
      if (k >= 0 && s[k] == '(')
        ++m;
      if (k >= 0 && s[k] == ')')
        --m;
      if (m == 0 && (k == -1 || find(ope[i].begin(), ope[i].end(), s[k]) !=
                                    ope[i].end())) {
        if (j < n - 1)
          ret = s[j + 1] + ret;
        ret = solve(s.substr(k + 1, j - k), i + 1) + ret;
        if (j < n - 1)
          ret = '(' + ret + ')';
        j = k - 1;
      }
      --k;
    }
  }

  return ret;
}

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

  while (--d >= 0) {
    int g;
    cin >> g;
    dir.resize(g);
    ope.resize(g);
    for (int i = 0; i < g; ++i) {
      int m;
      cin >> dir[i] >> m;
      ope[i].resize(m);
      for (int j = 0; j < m; ++j)
        cin >> ope[i][j];
    }

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      cout << solve(s, 0) << endl;
    }

    if (d > 0)
      cout << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int g;
string dir;
vector<string> ope;

string solve(string s, int i) {
  int n = s.size();
  if (n == 1)
    return s;
  if (s[0] == '(' && s[n - 1] == ')')
    return solve(s.substr(1, n - 2), 0);

  string ret;
  if (dir[i] == 'L') {
    int j = 0;
    int k = 0;
    int m = 0;
    while (k <= n) {
      if (k < n && s[k] == '(')
        ++m;
      if (k < n && s[k] == ')')
        --m;
      if (m == 0 && (k == n || find(ope[i].begin(), ope[i].end(), s[k]) !=
                                   ope[i].end())) {
        if (j > 0)
          ret += s[j - 1];
        ret += solve(s.substr(j, k - j), i + 1);
        if (j > 0)
          ret = '(' + ret + ')';
        j = k + 1;
      }
      ++k;
    }
  } else {
    int j = n - 1;
    int k = n - 1;
    int m = 0;
    while (k >= -1) {
      if (k >= 0 && s[k] == '(')
        ++m;
      if (k >= 0 && s[k] == ')')
        --m;
      if (m == 0 && (k == -1 || find(ope[i].begin(), ope[i].end(), s[k]) !=
                                    ope[i].end())) {
        if (j < n - 1)
          ret = s[j + 1] + ret;
        ret = solve(s.substr(k + 1, j - k), i + 1) + ret;
        if (j < n - 1)
          ret = '(' + ret + ')';
        j = k - 1;
      }
      --k;
    }
  }

  return ret;
}

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

  while (--d >= 0) {
    cin >> g;
    dir.resize(g);
    ope.resize(g);
    for (int i = 0; i < g; ++i) {
      int m;
      cin >> dir[i] >> m;
      ope[i].resize(m);
      for (int j = 0; j < m; ++j)
        cin >> ope[i][j];
    }

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      cout << solve(s, 0) << endl;
    }

    if (d > 0)
      cout << endl;
  }

  return 0;
}