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

#include <iostream>
using namespace std;

struct N {
  int R, C, S;
};

N n[80000000];

int main() {
  int p, q;
  int Ind[20], nr[20], nc[20], ns[20];
  string s1[20], s2[20], s;
  while (1) {
    cin >> p >> q;
    if (p + q == 0)
      break;
    nr[0] = nc[0] = ns[0] = 0;
    for (int i = 0; i < p; i++) {
      cin >> s1[i];
      Ind[i] = 0;
      if (i != 0) {
        nr[i] = nr[i - 1];
        nc[i] = nc[i - 1];
        ns[i] = ns[i - 1];
      }
      bool flag = true;
      for (int j = 0; j < s1[i].length(); j++) {
        if (s1[i][j] != '.')
          flag = false;
        if (s1[i][j] == '.' && flag)
          Ind[i]++;
        if (s1[i][j] == '(')
          nr[i]++;
        if (s1[i][j] == ')')
          nr[i]--;
        if (s1[i][j] == '{')
          nc[i]++;
        if (s1[i][j] == '}')
          nc[i]--;
        if (s1[i][j] == '[')
          ns[i]++;
        if (s1[i][j] == ']')
          ns[i]--;
      }
    }
    int a = 0;

    for (int R = 1; R <= 20; R++) {
      for (int C = 1; C <= 20; C++) {
        for (int S = 1; S <= 20; S++) {
          bool flag = true;
          for (int i = 1; i < p; i++) {
            if (Ind[i] != (R * (nr[i - 1]) + C * (nc[i - 1]) + S * (ns[i - 1])))
              flag = false;
          }
          if (flag) {
            n[a].R = R;
            n[a].C = C;
            n[a].S = S;
            a++;
          }
        }
      }
    }
    nr[0] = nc[0] = ns[0] = 0;
    for (int i = 1; i < q + 1; i++) {
      cin >> s2[i];
      if (i != 0) {
        nr[i] = nr[i - 1];
        nc[i] = nc[i - 1];
        ns[i] = ns[i - 1];
      }
      for (int j = 0; j < s2[i].length(); j++) {
        if (s2[i][j] == '(')
          nr[i]++;
        if (s2[i][j] == ')')
          nr[i]--;
        if (s2[i][j] == '{')
          nc[i]++;
        if (s2[i][j] == '}')
          nc[i]--;
        if (s2[i][j] == '[')
          ns[i]++;
        if (s2[i][j] == ']')
          ns[i]--;
      }
    }
    for (int i = 0; i < q; i++) {
      int b = -1;
      for (int j = 0; j < a; j++) {
        if (b != -1 && b != n[j].R * nr[i] + n[j].C * nc[i] + n[j].S * ns[i]) {
          b = -1;
          break;
        } else
          b = n[j].R * nr[i] + n[j].C * nc[i] + n[j].S * ns[i];
      }
      cout << b;
      if (i != q - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

struct N {
  int R, C, S;
};

int main() {
  int p, q;
  int Ind[20], nr[20], nc[20], ns[20];
  string s1[20], s2[20], s;
  N n[8000000];
  while (1) {
    cin >> p >> q;
    if (p + q == 0)
      break;
    nr[0] = nc[0] = ns[0] = 0;
    for (int i = 0; i < p; i++) {
      cin >> s1[i];
      Ind[i] = 0;
      if (i != 0) {
        nr[i] = nr[i - 1];
        nc[i] = nc[i - 1];
        ns[i] = ns[i - 1];
      }
      bool flag = true;
      for (int j = 0; j < s1[i].length(); j++) {
        if (s1[i][j] != '.')
          flag = false;
        if (s1[i][j] == '.' && flag)
          Ind[i]++;
        if (s1[i][j] == '(')
          nr[i]++;
        if (s1[i][j] == ')')
          nr[i]--;
        if (s1[i][j] == '{')
          nc[i]++;
        if (s1[i][j] == '}')
          nc[i]--;
        if (s1[i][j] == '[')
          ns[i]++;
        if (s1[i][j] == ']')
          ns[i]--;
      }
    }
    int a = 0;

    for (int R = 1; R <= 20; R++) {
      for (int C = 1; C <= 20; C++) {
        for (int S = 1; S <= 20; S++) {
          bool flag = true;
          for (int i = 1; i < p; i++) {
            if (Ind[i] != (R * (nr[i - 1]) + C * (nc[i - 1]) + S * (ns[i - 1])))
              flag = false;
          }
          if (flag) {
            n[a].R = R;
            n[a].C = C;
            n[a].S = S;
            a++;
          }
        }
      }
    }
    nr[0] = nc[0] = ns[0] = 0;
    for (int i = 1; i < q + 1; i++) {
      cin >> s2[i];
      if (i != 0) {
        nr[i] = nr[i - 1];
        nc[i] = nc[i - 1];
        ns[i] = ns[i - 1];
      }
      for (int j = 0; j < s2[i].length(); j++) {
        if (s2[i][j] == '(')
          nr[i]++;
        if (s2[i][j] == ')')
          nr[i]--;
        if (s2[i][j] == '{')
          nc[i]++;
        if (s2[i][j] == '}')
          nc[i]--;
        if (s2[i][j] == '[')
          ns[i]++;
        if (s2[i][j] == ']')
          ns[i]--;
      }
    }
    for (int i = 0; i < q; i++) {
      int b = -1;
      for (int j = 0; j < a; j++) {
        if (b != -1 && b != n[j].R * nr[i] + n[j].C * nc[i] + n[j].S * ns[i]) {
          b = -1;
          break;
        } else
          b = n[j].R * nr[i] + n[j].C * nc[i] + n[j].S * ns[i];
      }
      cout << b;
      if (i != q - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}