Problem p01551 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Na, Nt, Ng, Nc;
ll dp[64][64][64][64];
int M;
vector<string> nonterms;
map<string, vector<string>> rules;
vector<string> R;

stringstream ss;

ll dfs(int a, int t, int g, int c) {
  if (a < 0 || t < 0 || g < 0 || c < 0)
    return 0;
  //  cout << a << " " << t << " " << g << " " << c << endl;
  if (a == 0 && t == 0 && g == 0 && c == 0)
    return 1;
  int n = (Na + Nt + Ng + Nc) - (a + t + g + c);
  if (n == R.size())
    return 0;
  ll &ret = dp[a][t][g][c];
  if (ret != -1)
    return ret;
  ret = 0;
  for (int i = 0; i < R[n].size(); i++) {
    char s = R[n][i];
    //    ret = (ret + dfs(a - (s=='A' && a), t - (s=='T' && t), g - (s=='G' &&
    //    g), c- (s=='C' && c))) % MOD; cout << s << " " << ret << endl;
    if (s == 'A' && a) {
      ret += dfs(a - 1, t, g, c);
    } else if (s == 'T' && t) {
      ret += dfs(a, t - 1, g, c);
    } else if (s == 'G' && g) {
      ret += dfs(a, t, g - 1, c);
    } else if (s == 'C' && c) {
      ret += dfs(a, t, g, c - 1);
    }
    ret %= MOD;
  }
  return ret;
}

int dfs1(vector<string> &vs) {
  if (R.size() > Na + Nt + Ng + Nc)
    return -1;
  //  cout << "ok" << endl;
  //  cout << vs.size() << endl;
  //  vector<string> ret;
  for (auto &s : vs) {
    //    cout << s << endl;
    if (s[0] == '[') {
      R.push_back(s.substr(1, s.size() - 2));
    } else {
      //      cout << s << " "  << endl;
      if (dfs1(rules[s]) < 0)
        return -1;
      //      vector<string> tmp = dfs1(rules[s]);
      //      copy(tmp.begin(), tmp.end(), back_inserter(R));
    }
  }
  //  return &ret;
  return 0;
}

int main() {
  cin >> Na >> Nt >> Ng >> Nc;
  cin >> M;
  cin.ignore();

  rep(i, M) {
    string str;
    getline(cin, str);
    ss.clear();
    ss.str(str);
    string nonterm;
    vector<string> terms;
    ss >> nonterm;
    ss.ignore();
    nonterm = nonterm.substr(0, nonterm.size() - 1);
    while (!ss.eof()) {
      string term;
      ss >> term;
      terms.push_back(term);
      ss.ignore();
    }
    rules[nonterm] = terms;
    // cout << nonterm << " " << terms.size() << endl;
    nonterms.push_back(nonterm);
  }

  if (dfs1(rules[nonterms[0]]) < 0) {
    cout << 0 << endl;
  } else {
    //  for(auto &r: R) cout << r << endl;
    memset(dp, -1, sizeof(dp));
    cout << dfs(Na, Nt, Ng, Nc) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Na, Nt, Ng, Nc;
ll dp[64][64][64][64];
int M;
vector<string> nonterms;
map<string, vector<string>> rules;
vector<string> R;

stringstream ss;

ll dfs(int a, int t, int g, int c) {
  if (a < 0 || t < 0 || g < 0 || c < 0)
    return 0;
  //  cout << a << " " << t << " " << g << " " << c << endl;
  if (a == 0 && t == 0 && g == 0 && c == 0)
    return 1;
  int n = (Na + Nt + Ng + Nc) - (a + t + g + c);
  if (n == R.size())
    return 0;
  ll &ret = dp[a][t][g][c];
  if (ret != -1)
    return ret;
  ret = 0;
  for (int i = 0; i < R[n].size(); i++) {
    char s = R[n][i];
    //    ret = (ret + dfs(a - (s=='A' && a), t - (s=='T' && t), g - (s=='G' &&
    //    g), c- (s=='C' && c))) % MOD; cout << s << " " << ret << endl;
    if (s == 'A' && a) {
      ret += dfs(a - 1, t, g, c);
    } else if (s == 'T' && t) {
      ret += dfs(a, t - 1, g, c);
    } else if (s == 'G' && g) {
      ret += dfs(a, t, g - 1, c);
    } else if (s == 'C' && c) {
      ret += dfs(a, t, g, c - 1);
    }
  }
  return ret;
}

vector<string> dfs1(vector<string> vs) {
  //  cout << "ok" << endl;
  //  cout << vs.size() << endl;
  vector<string> ret;
  for (auto &s : vs) {
    //    cout << s << endl;
    if (s[0] == '[') {
      ret.push_back(s.substr(1, s.size() - 2));
    } else {
      //      cout << s << " "  << endl;
      vector<string> tmp = dfs1(rules[s]);
      copy(tmp.begin(), tmp.end(), back_inserter(ret));
    }
  }
  return ret;
}

int main() {
  cin >> Na >> Nt >> Ng >> Nc;
  cin >> M;
  cin.ignore();
  rep(i, M) {
    string str;
    getline(cin, str);
    ss.clear();
    ss.str(str);
    string nonterm;
    vector<string> terms;
    ss >> nonterm;
    ss.ignore();
    nonterm = nonterm.substr(0, nonterm.size() - 1);
    while (!ss.eof()) {
      string term;
      ss >> term;
      terms.push_back(term);
      ss.ignore();
    }
    rules[nonterm] = terms;
    // cout << nonterm << " " << terms.size() << endl;
    nonterms.push_back(nonterm);
  }
  R = dfs1(rules[nonterms[0]]);
  //  for(auto &r: R) cout << r << endl;
  memset(dp, -1, sizeof(dp));
  cout << dfs(Na, Nt, Ng, Nc) << endl;
  return 0;
}