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

#include <cstdio>
#include <set>
#include <vector>

using namespace std;

struct H {
  int r, c, s, i;
  H() {}
  H(int r, int c, int s, int i) : r(r), c(c), s(s), i(i) {}
};

vector<H> v;

int main(void) {
  int p, q, R, C, S, d, k;
  char g[88];
  for (;;) {
    v.clear();
    scanf("%d%d", &p, &q);
    if (p + q == 0)
      break;
    R = C = S = 0;
    for (int i = 0; i < p; i++) {
      scanf("%s", g);
      k = d = 0;
      while (g[k] == '.')
        ++k;
      d = k;
      v.push_back(H(R, C, S, d));
      while (g[k]) {
        if (g[k] == '(')
          ++R;
        if (g[k] == ')')
          --R;
        if (g[k] == '{')
          ++C;
        if (g[k] == '}')
          --C;
        if (g[k] == '[')
          ++S;
        if (g[k] == ']')
          --S;
        k++;
      }
    }

    R = C = S = 0;
    for (int i = 0; i < q; i++) {
      scanf("%s", g);
      k = d = 0;
      while (g[k] == '.')
        ++k;
      d = k;
      int id[11111] = {};
      for (int r = 1; r <= 20; r++) {
        for (int c = 1; c <= 20; c++) {
          for (int s = 1; s <= 20; s++) {
            bool ok = true;
            for (int j = 0; j < int(v.size()); j++) {
              if (v[j].r * r + v[j].c * c + v[j].s * s == v[j].i) {

              } else
                ok = false;
            }
            if (ok)
              id[R * r + C * c + S * s]++;
          }
        }
      }
      int aa = 0, res;
      for (int j = 0; j < 11111; j++) {
        if (id[j])
          ++aa, res = j;
      }
      printf("%d%c", aa == 1 ? res : -1, i == q - 1 ? '\\n' : ' ');
      while (g[k]) {
        if (g[k] == '(')
          ++R;
        if (g[k] == ')')
          --R;
        if (g[k] == '{')
          ++C;
        if (g[k] == '}')
          --C;
        if (g[k] == '[')
          ++S;
        if (g[k] == ']')
          --S;
        k++;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <set>
#include <vector>

using namespace std;

struct H {
  int r, c, s, i;
  H() {}
  H(int r, int c, int s, int i) : r(r), c(c), s(s), i(i) {}
};

vector<H> v;

int main(void) {
  int p, q, R, C, S, d, k;
  char g[88];
  for (;;) {
    v.clear();
    scanf("%d%d", &p, &q);
    if (p + q == 0)
      break;
    R = C = S = 0;
    for (int i = 0; i < p; i++) {
      scanf("%s", g);
      k = d = 0;
      while (g[k] == '.')
        ++k;
      d = k;
      v.push_back(H(R, C, S, d));
      while (g[k]) {
        if (g[k] == '(')
          ++R;
        if (g[k] == ')')
          --R;
        if (g[k] == '{')
          ++C;
        if (g[k] == '}')
          --C;
        if (g[k] == '[')
          ++S;
        if (g[k] == ']')
          --S;
        k++;
      }
    }

    R = C = S = 0;
    for (int i = 0; i < q; i++) {
      scanf("%s", g);
      k = d = 0;
      while (g[k] == '.')
        ++k;
      d = k;
      int id[444] = {};
      for (int r = 1; r <= 20; r++) {
        for (int c = 1; c <= 20; c++) {
          for (int s = 1; s <= 20; s++) {
            bool ok = true;
            for (int j = 0; j < int(v.size()); j++) {
              if (v[j].r * r + v[j].c * c + v[j].s * s == v[j].i) {

              } else
                ok = false;
            }
            if (ok)
              id[R * r + C * c + S * s]++;
          }
        }
      }
      int aa = 0, res;
      for (int j = 0; j < 444; j++) {
        if (id[j])
          ++aa, res = j;
      }
      printf("%d%c", aa == 1 ? res : -1, i == q - 1 ? '\\n' : ' ');
      while (g[k]) {
        if (g[k] == '(')
          ++R;
        if (g[k] == ')')
          --R;
        if (g[k] == '{')
          ++C;
        if (g[k] == '}')
          --C;
        if (g[k] == '[')
          ++S;
        if (g[k] == ']')
          --S;
        k++;
      }
    }
  }
  return 0;
}