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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int p, q;
  while (cin >> p >> q && p) {
    vector<vector<int>> c(4, vector<int>(p, 0));
    for (int i = 0; i < p; i++) {
      string t;
      cin >> t;
      bool f = true;
      int cnt = 0;
      for (int j = 0; j < t.size(); j++) {
        if (f && t[j] == '.')
          cnt++;
        else
          f = false;
        if (t[j] == '(')
          c[0][i]++;
        if (t[j] == ')')
          c[0][i]--;
        if (t[j] == '{')
          c[1][i]++;
        if (t[j] == '}')
          c[1][i]--;
        if (t[j] == '[')
          c[2][i]++;
        if (t[j] == ']')
          c[2][i]--;
      }
      if (i)
        c[3][i - 1] = cnt;
    }

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p - 1; j++) {
        c[i][j + 1] += c[i][j];
      }
    }

    vector<bool> rcs(3, false);
    int rcs_cnt = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p; j++) {
        if (c[i][j])
          rcs[i] = true;
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p; j++) {
        if (c[i][j]) {
          rcs_cnt++;
          break;
        }
      }
    }
    vector<vector<int>> x(4, vector<int>());
    for (int i = 0; i < p - 1; i++) {
      for (int k = 0; k < 4; k++)
        x[k].push_back(c[k][i]);
      int cnta = 0;
      for (int k = 0; k < 3; k++)
        if (c[k][i])
          cnta++;
      if (cnta < 2)
        continue;
      for (int j = 0; j < p - 1; j++) {
        if (i == j)
          continue;
        for (int k = 0; k < 3; k++)
          if (c[k][j] && c[k][j] == c[k][i]) {
            for (int l = 0; l < 4; l++)
              x[l].push_back(c[l][i] * c[k][j] - c[l][j] * c[k][i]);
          }
      }
    }

    vector<int> R, C, S;
    for (int i = 1; i < 21; i++) {
      if (rcs[0] == false)
        continue;
      for (int j = 1; j < 21; j++) {
        if (rcs[1] == false)
          continue;
        for (int k = 1; k < 21; k++) {
          if (rcs[2] == false)
            continue;
          bool f = true;
          for (int a = 0; a < x[0].size(); a++) {
            if (i * x[0][a] + j * x[1][a] + k * x[2][a] != x[3][a]) {
              f = false;
              break;
            }
          }
          if (f)
            R.push_back(i), C.push_back(j), S.push_back(k);
        }
      }
    }
    for (int i = 0; i < x[0].size(); i++) {
      if (x[0][i] && !x[1][i] && !x[2][i])
        R.push_back(x[3][i] / x[0][i]);
      if (!x[0][i] && x[1][i] && !x[2][i])
        C.push_back(x[3][i] / x[1][i]);
      if (!x[0][i] && !x[1][i] && x[2][i])
        S.push_back(x[3][i] / x[2][i]);
    }
    R.erase(unique(R.begin(), R.end()), R.end());
    C.erase(unique(C.begin(), C.end()), C.end());
    S.erase(unique(S.begin(), S.end()), S.end());

    vector<vector<int>> a(3, vector<int>(q, 0));
    for (int i = 0; i < q; i++) {
      string t;
      cin >> t;

      for (int j = 0; j < t.size(); j++) {
        if (t[j] == '(')
          a[0][i]++;
        if (t[j] == ')')
          a[0][i]--;
        if (t[j] == '{')
          a[1][i]++;
        if (t[j] == '}')
          a[1][i]--;
        if (t[j] == '[')
          a[2][i]++;
        if (t[j] == ']')
          a[2][i]--;
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < q - 1; j++) {
        a[i][j + 1] += a[i][j];
      }
    }

    vector<int> ans;
    ans.push_back(0);

    if (rcs_cnt >= 2 && R.size() == 1 && C.size() == 1 && S.size() == 1) {
      for (int i = 0; i < q - 1; i++)
        ans.push_back(R[0] * a[0][i] + C[0] * a[1][i] + S[0] * a[2][i]);
    } else {
      for (int i = 0; i < q - 1; i++) {
        bool f = false;
        if (!a[0][i] && !a[1][i] && !a[2][i]) {
          ans.push_back(0);
          continue;
        } else if (a[0][i] && R.size() == 1 && !a[1][i] && !a[2][i]) {
          ans.push_back(R[0] * a[0][i]);
          continue;
        } else if (a[1][i] && C.size() == 1 && !a[0][i] && !a[2][i]) {
          ans.push_back(C[0] * a[1][i]);
          continue;
        } else if (a[2][i] && S.size() == 1 && !a[0][i] && !a[1][i]) {
          ans.push_back(S[0] * a[2][i]);
          continue;
        }
        for (int j = 0; j < p - 1; j++) {
          if (!c[0][j] && !c[1][j] && !c[2][j])
            continue;
          if ((!a[0][i] && c[0][j]) || (a[0][i] && !c[0][j]))
            continue;
          if ((!a[1][i] && c[1][j]) || (a[1][i] && !c[1][j]))
            continue;
          if ((!a[2][i] && c[2][j]) || (a[2][i] && !c[2][j]))
            continue;
          double u = -1;
          if (a[0][i] && c[0][j])
            u = a[0][i] / c[0][j];
          if (a[1][i] && c[1][j]) {
            if (u == -1)
              u = a[1][i] / c[1][j];
            else if (u != a[1][i] / c[1][j])
              break;
          }
          if (a[2][i] && c[2][j]) {
            if (u == -1)
              u = a[2][i] / c[2][j];
            else if (u != a[2][i] / c[2][j])
              break;
          }
          f = true;
          c[3][1] *= (int)u;
          ans.push_back(c[3][j]);
          break;
        }
        if (!f)
          ans.push_back(-1);
      }
    }
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << (i == ans.size() - 1 ? "\\n" : " ");
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(void) {
  int p, q;
  while (cin >> p >> q && p) {
    vector<vector<int>> c(4, vector<int>(p, 0));
    for (int i = 0; i < p; i++) {
      string t;
      cin >> t;
      bool f = true;
      int cnt = 0;
      for (int j = 0; j < t.size(); j++) {
        if (f && t[j] == '.')
          cnt++;
        else
          f = false;
        if (t[j] == '(')
          c[0][i]++;
        if (t[j] == ')')
          c[0][i]--;
        if (t[j] == '{')
          c[1][i]++;
        if (t[j] == '}')
          c[1][i]--;
        if (t[j] == '[')
          c[2][i]++;
        if (t[j] == ']')
          c[2][i]--;
      }
      if (i)
        c[3][i - 1] = cnt;
    }

    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p - 1; j++) {
        c[i][j + 1] += c[i][j];
      }
    }

    vector<bool> rcs(3, false);
    int rcs_cnt = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p; j++) {
        if (c[i][j])
          rcs[i] = true;
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < p; j++) {
        if (c[i][j]) {
          rcs_cnt++;
          break;
        }
      }
    }

    vector<int> R, C, S;
    for (int i = 1; i < 21; i++) {
      if (rcs[0] == false)
        continue;
      for (int j = 1; j < 21; j++) {
        if (rcs[1] == false)
          continue;
        for (int k = 1; k < 21; k++) {
          if (rcs[2] == false)
            continue;
          bool f = true;
          for (int a = 0; a < c[0].size(); a++) {
            if (i * c[0][a] + j * c[1][a] + k * c[2][a] != c[3][a]) {
              f = false;
              break;
            }
          }
          if (f)
            R.push_back(i), C.push_back(j), S.push_back(k);
        }
      }
    }
    for (int i = 0; i < p - 1; i++) {
      if (c[0][i] && !c[1][i] && !c[2][i])
        R.push_back(c[3][i] / c[0][i]);
      if (!c[0][i] && c[1][i] && !c[2][i])
        C.push_back(c[3][i] / c[1][i]);
      if (!c[0][i] && !c[1][i] && c[2][i])
        S.push_back(c[3][i] / c[2][i]);
    }
    R.erase(unique(R.begin(), R.end()), R.end());
    C.erase(unique(C.begin(), C.end()), C.end());
    S.erase(unique(S.begin(), S.end()), S.end());

    vector<vector<int>> a(3, vector<int>(q, 0));
    for (int i = 0; i < q; i++) {
      string t;
      cin >> t;

      for (int j = 0; j < t.size(); j++) {
        if (t[j] == '(')
          a[0][i]++;
        if (t[j] == ')')
          a[0][i]--;
        if (t[j] == '{')
          a[1][i]++;
        if (t[j] == '}')
          a[1][i]--;
        if (t[j] == '[')
          a[2][i]++;
        if (t[j] == ']')
          a[2][i]--;
      }
    }
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < q - 1; j++) {
        a[i][j + 1] += a[i][j];
      }
    }

    vector<int> ans;
    ans.push_back(0);

    if (rcs_cnt >= 2 && R.size() == 1 && C.size() == 1 && S.size() == 1) {
      for (int i = 0; i < q - 1; i++)
        ans.push_back(R[0] * a[0][i] + C[0] * a[1][i] + S[0] * a[2][i]);
    } else {
      for (int i = 0; i < q - 1; i++) {
        bool f = false;
        if (!a[0][i] && !a[1][i] && !a[2][i]) {
          ans.push_back(0);
          continue;
        } else if (a[0][i] && R.size() == 1 && !a[1][i] && !a[2][i]) {
          ans.push_back(R[0] * a[0][i]);
          continue;
        } else if (a[1][i] && C.size() == 1 && !a[0][i] && !a[2][i]) {
          ans.push_back(C[0] * a[1][i]);
          continue;
        } else if (a[2][i] && S.size() == 1 && !a[0][i] && !a[1][i]) {
          ans.push_back(R[2] * a[2][i]);
          continue;
        }
        for (int j = 0; j < p - 1; j++) {
          if (!c[0][j] && !c[1][j] && !c[2][j])
            continue;
          if ((!a[0][i] && c[0][j]) || (a[0][i] && !c[0][j]))
            continue;
          if ((!a[1][i] && c[1][j]) || (a[1][i] && !c[1][j]))
            continue;
          if ((!a[2][i] && c[2][j]) || (a[2][i] && !c[2][j]))
            continue;
          double u = -1;
          if (a[0][i] && c[0][j])
            u = a[0][i] / c[0][j];
          if (a[1][i] && c[1][j]) {
            if (u == -1)
              u = a[1][i] / c[1][j];
            else if (u != a[1][i] / c[1][j])
              break;
          }
          if (a[2][i] && c[2][j]) {
            if (u == -1)
              u = a[2][i] / c[2][j];
            else if (u != a[2][i] / c[2][j])
              break;
          }
          f = true;
          c[3][1] *= (int)u;
          ans.push_back(c[3][j]);
          break;
        }
        if (!f)
          ans.push_back(-1);
      }
    }
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << (i == ans.size() - 1 ? "\\n" : " ");
  }
}