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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int na, nt, ng, nc;
vector<string> str;
map<string, vector<string>> dict;
map<string, vector<string>> dict0;

int dp[55][55][55][55];
bool used[55][55][55][55];

void fail() {
  cout << 0 << endl;
  exit(0);
}

void solve(const string &sa) {
  if (dict0.count(sa))
    return;
  for (const string &s : dict[sa]) {
    if (s[0] == '[')
      dict0[sa].push_back(s);
    else {
      solve(s);
      auto v = dict0[s];
      dict0[sa].insert(dict0[sa].end(), v.begin(), v.end());
      if ((int)dict0[sa].size() > na + nt + ng + nc)
        fail();
    }
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> na >> nt >> ng >> nc;
  int m;
  cin >> m;
  resz(str, m);
  cin.ignore();
  rep(i, m) {
    string buf;
    getline(cin, buf);
    stringstream ss(buf);
    ss >> str[i];
    str[i].pop_back();
    string tmp;
    while (ss >> tmp)
      dict[str[i]].push_back(tmp);
  }
  solve(str[0]);
  auto v = dict0[str[0]];
  if ((int)v.size() != na + nt + ng + nc)
    fail();
  struct State {
    int a, t, g, c;
    State() {}
    State(int a, int t, int g, int c) : a(a), t(t), g(g), c(c) {}
  };
  str.clear();
  dict.clear();
  dict0.clear();
  queue<State> que;
  que.emplace(0, 0, 0, 0);
  dp[0][0][0][0] = 1;
  while (!que.empty()) {
    State st = que.front();
    que.pop();
    int idx = st.a + st.t + st.g + st.c;
    if (idx >= (int)v.size())
      continue;
    const string &s = v[idx];
    // cout<<s<<endl;
    reps(i, 1, (int)s.size() - 1) {
      State nxt = st;
      if (s[i] == 'A')
        ++nxt.a;
      else if (s[i] == 'T')
        ++nxt.t;
      else if (s[i] == 'G')
        ++nxt.g;
      else if (s[i] == 'C')
        ++nxt.c;
      else
        continue;
      if (nxt.a > na || nxt.t > nt || nxt.g > ng || nxt.c > nc)
        continue;
      (dp[nxt.a][nxt.t][nxt.g][nxt.c] += dp[st.a][st.t][st.g][st.c]) %= mod;
      if (used[nxt.a][nxt.t][nxt.g][nxt.c])
        continue;
      used[nxt.a][nxt.t][nxt.g][nxt.c] = true;
      que.push(nxt);
    }
  }
  cout << dp[na][nt][ng][nc] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

int na, nt, ng, nc;
vector<string> str;
map<string, vector<string>> dict;
map<string, vector<string>> dict0;

int dp[55][55][55][55];
bool used[55][55][55][55];

void fail() {
  cout << 0 << endl;
  exit(0);
}

void solve(const string &sa) {
  if (dict0.count(sa))
    return;
  for (const string &s : dict[sa]) {
    if (s[0] == '[')
      dict0[sa].push_back(s);
    else {
      solve(s);
      auto v = dict0[s];
      dict0[sa].insert(dict0[sa].end(), v.begin(), v.end());
      if ((int)dict0[sa].size() > na + nt + ng + nc)
        fail();
    }
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> na >> nt >> ng >> nc;
  int m;
  cin >> m;
  resz(str, m);
  cin.ignore();
  rep(i, m) {
    string buf;
    getline(cin, buf);
    stringstream ss(buf);
    ss >> str[i];
    str[i].pop_back();
    string tmp;
    while (ss >> tmp)
      dict[str[i]].push_back(tmp);
  }
  solve(str[0]);
  auto v = dict0[str[0]];
  struct State {
    int a, t, g, c;
    State() {}
    State(int a, int t, int g, int c) : a(a), t(t), g(g), c(c) {}
  };
  str.clear();
  dict.clear();
  dict0.clear();
  queue<State> que;
  que.emplace(0, 0, 0, 0);
  dp[0][0][0][0] = 1;
  while (!que.empty()) {
    State st = que.front();
    que.pop();
    int idx = st.a + st.t + st.g + st.c;
    if (st.a + st.t + st.g + st.c >= na + nt + ng + nc)
      continue;
    const string &s = v[idx];
    reps(i, 1, (int)s.size() - 1) {
      State nxt = st;
      if (s[i] == 'A')
        ++nxt.a;
      else if (s[i] == 'T')
        ++nxt.t;
      else if (s[i] == 'G')
        ++nxt.g;
      else if (s[i] == 'C')
        ++nxt.c;
      else
        continue;
      if (nxt.a > na || nxt.t > nt || nxt.g > ng || nxt.c > nc)
        continue;
      (dp[nxt.a][nxt.t][nxt.g][nxt.c] += dp[st.a][st.t][st.g][st.c]) %= mod;
      if (used[nxt.a][nxt.t][nxt.g][nxt.c])
        continue;
      used[nxt.a][nxt.t][nxt.g][nxt.c] = true;
      que.push(nxt);
    }
  }
  cout << dp[na][nt][ng][nc] << endl;

  return 0;
}