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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector<unsigned int>> formulae;
int ans[(1 << 17)];

void init() {
  for (unsigned int i = 0; i < (1 << 17); i++) {
    ans[i] = -1;
  }

  for (int n = 1; n < 17; n++) {
    if (n == 1) {
      formulae[1].push_back(0b0000000000000000);
      formulae[1].push_back(0b1111111111111111);
      formulae[1].push_back(0b0000000011111111);
      formulae[1].push_back(0b0000111100001111);
      formulae[1].push_back(0b0011001100110011);
      formulae[1].push_back(0b0101010101010101);
    } else {
      for (int i = 0; i < formulae[n - 1].size(); i++) {
        formulae[n].push_back((~formulae[n - 1][i]) % (1 << 16));
      }
      if (n > 4) {
        for (int k = 1; k < n - 3; k++) {
          for (int i = 0; i < formulae[k].size(); i++) {
            for (int j = 0; j < formulae[n - 3 - k].size(); j++) {
              formulae[n].push_back(formulae[k][i] ^ formulae[n - 3 - k][j]);
              formulae[n].push_back(formulae[k][i] & formulae[n - 3 - k][j]);
            }
          }
        }
      }
    }
  }
}

unsigned int eval(string s, int ptr) {
  if (s[ptr] == '0') {
    return 0b0000000000000000;
  } else if (s[ptr] == '1') {
    return 0b1111111111111111;
  } else if (s[ptr] == 'a') {
    return 0b0000000011111111;
  } else if (s[ptr] == 'b') {
    return 0b0000111100001111;
  } else if (s[ptr] == 'c') {
    return 0b0011001100110011;
  } else if (s[ptr] == 'd') {
    return 0b0101010101010101;
  } else if (s[ptr] == '-') {
    unsigned int e = eval(s, ptr + 1);
    return ((~e) % (1 << 16));
  } else {
    int op = ptr;
    int kakko = 0;
    bool done = false;
    while (!done) {
      op++;
      if (kakko == 0) {
        if ((s[op] == '*' || s[op] == '^')) {
          done = true;
        }
      }
      if (s[op] == '(') {
        kakko++;
      } else if (s[op] == ')') {
        kakko--;
      }
    }
    unsigned int e1 = eval(s, ptr + 1);
    unsigned int e2 = eval(s, op + 1);
    if (s[op] == '^') {
      return e1 ^ e2;
    } else {
      return e1 & e2;
    }
  }
}

int main(void) {
  formulae.resize(17);
  init();
  for (int i = 1; i < 17; i++) {
    for (int j = 0; j < (formulae[i].size()); j++) {
      unsigned int e = formulae[i][j];
      //      cout << ":" << e << endl;
      if (ans[e] == -1) {
        ans[e] = i;
      }
    }
  }
  for (;;) {
    string s;
    cin >> s;
    if (s == ".") {
      return 0;
    }
    unsigned int e = 0;
    e += eval(s, 0);
    cout << ans[e] << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<vector<string>> formulae;
int ans[1 << 17];

void init() {
  for (unsigned int i = 0; i < (1 << 17); i++) {
    ans[i] = -1;
  }

  for (int n = 1; n < 17; n++) {
    if (n == 1) {
      formulae[1].push_back("0");
      formulae[1].push_back("1");
      formulae[1].push_back("a");
      formulae[1].push_back("b");
      formulae[1].push_back("c");
      formulae[1].push_back("d");
    } else {
      for (int i = 0; i < formulae[n - 1].size(); i++) {
        formulae[n].push_back("-" + formulae[n - 1][i]);
      }
      if (n > 4) {
        for (int k = 1; k < n - 3; k++) {
          for (int i = 0; i < formulae[k].size(); i++) {
            for (int j = 0; j < formulae[n - 3 - k].size(); j++) {
              formulae[n].push_back("(" + formulae[k][i] + "^" +
                                    formulae[n - 3 - k][j] + ")");
              formulae[n].push_back("(" + formulae[k][i] + "*" +
                                    formulae[n - 3 - k][j] + ")");
            }
          }
        }
      }
    }
  }
}

unsigned int eval(string s, int ptr) {
  if (s[ptr] == '0') {
    return 0b0000000000000000;
  } else if (s[ptr] == '1') {
    return 0b1111111111111111;
  } else if (s[ptr] == 'a') {
    return 0b0000000011111111;
  } else if (s[ptr] == 'b') {
    return 0b0000111100001111;
  } else if (s[ptr] == 'c') {
    return 0b0011001100110011;
  } else if (s[ptr] == 'd') {
    return 0b0101010101010101;
  } else if (s[ptr] == '-') {
    unsigned int e = eval(s, ptr + 1);
    return ((~e) % (1 << 16));
  } else {
    int op = ptr;
    int kakko = 0;
    bool done = false;
    while (!done) {
      op++;
      if (kakko == 0) {
        if ((s[op] == '*' || s[op] == '^')) {
          done = true;
        }
      }
      if (s[op] == '(') {
        kakko++;
      } else if (s[op] == ')') {
        kakko--;
      }
    }
    unsigned int e1 = eval(s, ptr + 1);
    unsigned int e2 = eval(s, op + 1);
    if (s[op] == '^') {
      return e1 ^ e2;
    } else {
      return e1 & e2;
    }
  }
}

/*
int eval(string s, int ptr, int a, int b, int c, int d){
  if(s[ptr] == '0'){ return 0; }
  else if(s[ptr] == '1'){ return 1; }
  else if(s[ptr] == 'a'){ return a; }
  else if(s[ptr] == 'b'){ return b; }
  else if(s[ptr] == 'c'){ return c; }
  else if(s[ptr] == 'd'){ return d; }
  else if(s[ptr] == '-'){
    int e = eval(s,ptr+1,a,b,c,d);
    return (e == 0);
  } else {
    int op = ptr+1;
    while(s[op] != '*' && s[op] != '^'){op++;}
    int e1 = eval(s,ptr+1,a,b,c,d);
    int e2 = eval(s,op+1,a,b,c,d);
    if(s[op] == '^'){
      return (e1+e2)%2;
    }else{
      return e1*e2;
    }
  }
}
*/

int main(void) {
  formulae.resize(17);
  init();
  for (int i = 1; i < 17; i++) {
    for (int j = 0; j < (formulae[i].size()); j++) {
      unsigned int e = eval(formulae[i][j], 0);
      // if(i < 6){cout << formulae[i][j] << ":" << e << endl;}
      if (ans[e] == -1) {
        ans[e] = i;
      }
    }
  }
  for (;;) {
    string s;
    cin >> s;
    if (s == ".") {
      return 0;
    }
    unsigned int e = 0;
    e += eval(s, 0);
    cout << ans[e] << endl;
  }
  return 0;
}