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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
#define int long long
int N[4];
int sum;
int M;
map<string, int> mp;
vector<vector<string>> A;
vector<vector<string>> B;
vector<vector<int>> C;
bool ok = true;
const int MOD = 1000000007;
void rec(int i) {
  for (int j = 0; j < A[i].size(); j++) {
    if (!ok)
      break;
    if (A[i][j][0] <= 'Z') {
      B[i].push_back(A[i][j]);
    } else {
      int k = mp[A[i][j]];
      if ((int)B[k].size() == 0) {
        rec(k);
      }
      if (!ok)
        break;
      for (int l = 0; l < B[k].size(); l++) {
        B[i].push_back(B[k][l]);
      }
    }
    if (!ok)
      break;
  }
  if ((int)B[i].size() > 200) {
    ok = false;
  }
}
int dp[51][51][51][51];
int solve(const int n[4]) {
  if (dp[n[0]][n[1]][n[2]][n[3]] >= 0) {
    return dp[n[0]][n[1]][n[2]][n[3]];
  }
  int T[4];
  int s = sum;
  int res = 0;
  for (int i = 0; i < 4; i++) {
    T[i] = n[i];
    s -= n[i];
  }

  for (int i = 0; i < 4; i++) {
    if (T[i] > 0 && C[s][i] == 1) {
      T[i]--;
      res = (res + solve(T)) % MOD;
      T[i]++;
    }
  }
  // res = 0;
  // cerr << n[0] << " " << n[1] << " " << N[2] << " " << N[3] << " " << res <<
  // endl;
  return dp[n[0]][n[1]][n[2]][n[3]] = res;
}
signed main() {

  cin >> N[0] >> N[1] >> N[2] >> N[3];
  sum = N[0] + N[1] + N[2] + N[3];
  cin >> M;
  cin.ignore();
  vector<string> S(M);
  A.resize(M);
  B.resize(M);
  string X;
  for (int i = 0; i < M; i++) {
    getline(cin, S[i]);
    stringstream ss(S[i]);
    ss >> X;
    X.pop_back();
    mp[X] = i;
    while (ss >> X) {
      // cerr << X << endl;
      if (X[0] == '[') {
        // cerr << X << endl;
        X.erase(X.begin());
        X.pop_back();
        sort(X.begin(), X.end());
        X.erase(unique(X.begin(), X.end()), X.end());
      }
      // cerr << i << " " << X << endl;
      A[i].push_back(X);
    }
  }

  rec(0);
  if (ok) {
    ok = (B[0].size() == sum);
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }
  /*for (int i = 0; i < B[0].size(); i++) {
          cerr << B[0][i] << " ";
  }
  cerr << endl;*/
  // cerr << (ok ? "ok" : "ng") << endl;
  for (int i0 = 0; i0 <= N[0]; i0++) {
    for (int i1 = 0; i1 <= N[1]; i1++) {
      for (int i2 = 0; i2 <= N[2]; i2++) {
        for (int i3 = 0; i3 <= N[3]; i3++) {
          dp[i0][i1][i2][i3] = -1;
        }
      }
    }
  }
  dp[0][0][0][0] = 1;
  C.resize(sum, vector<int>(4, 0));
  for (int i = 0; i < sum; i++) {
    for (int j = 0; j < B[0][i].size(); j++) {
      if (B[0][i][j] == 'A')
        C[i][0] = 1;
      else if (B[0][i][j] == 'T')
        C[i][1] = 1;
      else if (B[0][i][j] == 'G')
        C[i][2] = 1;
      else if (B[0][i][j] == 'C')
        C[i][3] = 1;
    }
  }

  /*for (int i = 0; i < sum; i++) {
          for (int j = 0; j < 4; j++) {
                  cerr << C[i][j] << " ";
          }
          cerr << endl;
  }*/
  cout << solve(N) << endl;
}


Error: 0
AT CG 


Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
using namespace std;
#define int long long
int N[4];
int sum;
int M;
map<string, int> mp;
vector<vector<string>> A;
vector<vector<string>> B;
vector<vector<int>> C;
bool ok = true;
const int MOD = 1000000007;
void rec(int i) {
  for (int j = 0; j < A[i].size(); j++) {
    if (!ok)
      break;
    if (A[i][j][0] <= 'Z') {
      B[i].push_back(A[i][j]);
    } else {
      int k = mp[A[i][j]];
      if ((int)B[k].size() == 0) {
        rec(k);
      }
      if (!ok)
        break;
      for (int l = 0; l < B[k].size(); l++) {
        B[i].push_back(B[k][l]);
      }
    }
    if (!ok)
      break;
  }
  if ((int)B[i].size() > 200) {
    ok = false;
  }
}
int dp[51][51][51][51];
int solve(const int n[4]) {
  if (dp[n[0]][n[1]][n[2]][n[3]] >= 0) {
    return dp[n[0]][n[1]][n[2]][n[3]];
  }
  int T[4];
  int s = sum;
  int res = 0;
  for (int i = 0; i < 4; i++) {
    T[i] = n[i];
    s -= n[i];
  }
  if (s == sum)
    return 1;

  for (int i = 0; i < 4; i++) {
    if (T[i] > 0 && C[s][i] == 1) {
      T[i]--;
      res = (res + solve(T)) % MOD;
      T[i]++;
    }
  }
  // cerr << n[0] << " " << n[1] << " " << N[2] << " " << N[3] << " " << res <<
  // endl;
  return dp[n[0]][n[1]][n[2]][n[3]] = res;
}
signed main() {

  cin >> N[0] >> N[1] >> N[2] >> N[3];
  sum = N[0] + N[1] + N[2] + N[3];
  cin >> M;
  cin.ignore();
  vector<string> S(M);
  A.resize(M);
  B.resize(M);
  string X;
  for (int i = 0; i < M; i++) {
    getline(cin, S[i]);
    stringstream ss(S[i]);
    ss >> X;
    X.pop_back();
    mp[X] = i;
    while (ss >> X) {
      // cerr << X << endl;
      if (X[0] == '[') {
        // cerr << X << endl;
        X.erase(X.begin());
        X.pop_back();
        sort(X.begin(), X.end());
        X.erase(unique(X.begin(), X.end()), X.end());
      }
      // cerr << i << " " << X << endl;
      A[i].push_back(X);
    }
  }
  rec(0);
  if (ok) {
    ok = (B[0].size() == sum);
  }
  if (!ok) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < B[0].size(); i++) {
    cerr << B[0][i] << " ";
  }
  cerr << endl;
  // cerr << (ok ? "ok" : "ng") << endl;
  for (int i0 = 0; i0 <= N[0]; i0++) {
    for (int i1 = 0; i1 <= N[1]; i1++) {
      for (int i2 = 0; i2 <= N[2]; i2++) {
        for (int i3 = 0; i3 <= N[3]; i3++) {
          dp[i0][i1][i2][i3] = -1;
        }
      }
    }
  }
  C.resize(sum, vector<int>(4, 0));
  for (int i = 0; i < sum; i++) {
    for (int j = 0; j < B[0][i].size(); j++) {
      if (B[0][i][j] == 'A')
        C[i][0] = 1;
      else if (B[0][i][j] == 'T')
        C[i][1] = 1;
      else if (B[0][i][j] == 'G')
        C[i][2] = 1;
      else if (B[0][i][j] == 'C')
        C[i][3] = 1;
    }
  }
  /*for (int i = 0; i < sum; i++) {
          for (int j = 0; j < 4; j++) {
                  cerr << C[i][j] << " ";
          }
          cerr << endl;
  }*/
  cout << solve(N) << endl;
}