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

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

int H, N, Q, p[30][30];
vector<int> r[30], J;
string c[30];
string S;

vector<int> amida_add(vector<int> a, vector<int> b) {
  vector<int> c = a, p = a, q = a;
  for (int i = 0; i < a.size(); i++)
    p[a[i]] = i;
  for (int i = 0; i < b.size(); i++)
    q[b[i]] = i;
  for (int i = 0; i < a.size(); i++)
    c[q[p[i]]] = i;
  return c;
}

vector<int> amida_rep(vector<int> a, int rep) {
  vector<int> dp[32], res = J;
  for (int i = 0; i < 32; i++) {
    dp[i].resize(a.size(), 0);
  }
  dp[0] = a;
  for (int i = 0; i < 31; i++)
    dp[i + 1] = amida_add(dp[i], dp[i]);
  for (int i = 0; i < 31; i++) {
    if ((rep / (1 << i)) % 2 == 1)
      res = amida_add(res, dp[i]);
  }
  return res;
}

vector<int> solve(int L, int R) {
  if (L == R) {
    for (int i = 0; i < N; i++) {
      if (S[L] == c[i][0])
        return r[i];
    }
  }
  vector<int> D;
  D.push_back(L - 1);
  int depth = 0;
  for (int i = L; i <= R; i++) {
    if (S[i] == '(')
      depth++;
    if (S[i] == ')')
      depth--;
    if (S[i] == '+' && depth == 0)
      D.push_back(i);
  }
  D.push_back(R + 1);
  if (D.size() >= 3) {
    vector<int> T = J;
    for (int i = 0; i < D.size() - 1; i++) {
      vector<int> U = solve(D[i] + 1, D[i + 1] - 1);
      T = amida_add(T, U);
    }
    return T;
  }
  int cl = -1, cr = -1;
  for (int i = L; i <= R; i++) {
    if (S[i] == '(') {
      cl = i;
      break;
    }
  }
  for (int i = R; i >= L; i--) {
    if (S[i] == ')') {
      cr = i;
      break;
    }
  }
  if (cl != -1) {
    int P = stoi(S.substr(L, cl - L));
    vector<int> U = solve(cl + 1, cr - 1);
    U = amida_rep(U, P);
    return U;
  }
  int PP = stoi(S.substr(L, R - L));
  vector<int> UU = solve(R, R);
  UU = amida_rep(UU, PP);
  return UU;
}

int main() {
  cin >> H >> N;
  for (int j = 0; j < H; j++)
    J.push_back(j);
  for (int i = 0; i < N; i++) {
    int w = 0;
    cin >> c[i] >> w;
    r[i] = J;
    for (int j = 0; j < w - 1; j++) {
      for (int k = 0; k < H - 1; k++) {
        cin >> p[j][k];
        if (p[j][k] == 1)
          swap(r[i][k], r[i][k + 1]);
      }
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> S;
    vector<int> ans = solve(0, S.size() - 1);
    for (int j = 0; j < ans.size(); j++) {
      if (j)
        cout << " ";
      cout << ans[j] + 1;
    }
    cout << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int H, N, Q, p[30][30];
vector<int> r[30], J;
string c[30];
string S;

vector<int> amida_add(vector<int> a, vector<int> b) {
  vector<int> c = a, p = a, q = a;
  for (int i = 0; i < a.size(); i++)
    p[a[i]] = i;
  for (int i = 0; i < b.size(); i++)
    q[b[i]] = i;
  for (int i = 0; i < a.size(); i++)
    c[q[p[i]]] = i;
  return c;
}

vector<int> amida_rep(vector<int> a, int rep) {
  vector<int> dp[32], res = J;
  for (int i = 0; i < 32; i++) {
    dp[i].resize(a.size(), 0);
  }
  dp[0] = a;
  for (int i = 0; i < 31; i++)
    dp[i + 1] = amida_add(dp[i], dp[i]);
  for (int i = 0; i < 31; i++) {
    if ((rep / (1 << i)) % 2 == 1)
      res = amida_add(res, dp[i]);
  }
  return res;
}

vector<int> solve(int L, int R) {
  if (L == R) {
    for (int i = 0; i < N; i++) {
      if (S[L] == c[i][0])
        return r[i];
    }
  }
  vector<int> D;
  D.push_back(L - 1);
  int depth = 0;
  for (int i = L; i <= R; i++) {
    if (S[i] == '(')
      depth++;
    if (S[i] == ')')
      depth--;
    if (S[i] == '+' && depth == 0)
      D.push_back(i);
  }
  D.push_back(R + 1);
  if (D.size() >= 3) {
    vector<int> T = J;
    for (int i = 0; i < D.size() - 1; i++) {
      vector<int> U = solve(D[i] + 1, D[i + 1] - 1);
      T = amida_add(T, U);
    }
    return T;
  }
  int cl = -1, cr = -1;
  for (int i = L; i <= R; i++) {
    if (S[i] == '(') {
      cl = i;
      break;
    }
  }
  for (int i = R; i >= L; i--) {
    if (S[i] == ')') {
      cr = i;
      break;
    }
  }
  if (cl != -1) {
    int P = stoi(S.substr(L, cl - L));
    vector<int> U = solve(cl + 1, cr - 1);
    U = amida_rep(U, P);
    return U;
  }
  int PP = stoi(S.substr(L, R - 1));
  vector<int> UU = solve(R, R);
  UU = amida_rep(UU, PP);
  return UU;
}

int main() {
  cin >> H >> N;
  for (int j = 0; j < H; j++)
    J.push_back(j);
  for (int i = 0; i < N; i++) {
    int w = 0;
    cin >> c[i] >> w;
    r[i] = J;
    for (int j = 0; j < w - 1; j++) {
      for (int k = 0; k < H - 1; k++) {
        cin >> p[j][k];
        if (p[j][k] == 1)
          swap(r[i][k], r[i][k + 1]);
      }
    }
  }
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> S;
    vector<int> ans = solve(0, S.size() - 1);
    for (int j = 0; j < ans.size(); j++) {
      if (j)
        cout << " ";
      cout << ans[j] + 1;
    }
    cout << endl;
  }
  return 0;
}