Problem p00623 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int curPos = 0;
string str;
int info[128] = {};
vector<vector<int>> g;
int number[128] = {};
int mx = 0;
void parse(int pos) {
  // cout << curPos << " " << str[curPos] << endl;
  mx = max(pos, mx);
  if (str[curPos] >= '0' && str[curPos] <= '9') {
    number[pos] = info[str[curPos] - '0' - 1];
    curPos++;
    return;
  }

  assert(str[curPos] == '(');
  curPos++;
  g[pos].push_back(pos * 2 + 1);
  parse(pos * 2 + 1);

  assert(str[curPos] == ' ');
  curPos++; // space

  g[pos].push_back(pos * 2 + 2);
  parse(pos * 2 + 2);
  assert(str[curPos] == ')');
  curPos++;
  return;
}

int calc(int a, int b, int t) {
  if (t == 0)
    return a & b;
  if (t == 1)
    return a | b;
  return a ^ b;
}

int tmp[30] = {};

vector<int> all(int pos) {
  if (number[pos] >= 0) {
    vector<int> c(16);
    c[number[pos]] = 1;
    // cout << pos << " : ";
    // for(int i = 0 ; i < 16 ; i++) cout << c[i] << " "; cout << endl;
    return c;
  } else {
    vector<int> a, b, c(16);
    a = all(pos * 2 + 1);
    b = all(pos * 2 + 2);
    for (int k = 0; k < 3; k++) {
      for (int i = 0; i < 16; i++) {
        for (int j = 0; j < 16; j++) {
          c[calc(i, j, k)] += a[i] * b[j];
        }
      }
    }
    // cout << pos << " : ";
    // for(int i = 0 ; i < 16 ; i++) cout << c[i] << " "; cout << endl;
    return c;
  }
}

int main() {
  int n;
  while (1) {
    while (getline(cin, str) && (str == "" || str == "END")) {
      if (str == "END")
        return 0;
    }
    for (int i = 0; i < 128; i++)
      number[i] = -1;
    cin >> n;
    g.clear();
    g.resize(128);
    for (int i = 0; i < n; i++) {
      int inf = 0, a;
      for (int j = 0; j < 4; j++) {
        cin >> a;
        inf |= (a << j);
      }
      info[i] = inf;
    }
    str += "~";
    curPos = 0;
    mx = 0;
    parse(0);
    assert(str[curPos] == '~');
    /*for(int i = 0 ; i <= mx ; i++){
            cout << i << "(" << number[i] << ")" << " : ";
            for(int j =0 ; j < g[i].size() ; j++) cout << g[i][j] << " ";
            cout << endl;
    }*/
    vector<int> answer = all(0);
    cout << answer[15] << endl;
  }
}

Error: TLE

Fixed submission:

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

int curPos = 0;
string str;
int info[18] = {};
vector<vector<int>> g;
int number[18] = {};
int mx = 0;
void parse(int pos) {
  // cout << curPos << " " << str[curPos] << endl;
  mx = max(pos, mx);
  if (str[curPos] >= '0' && str[curPos] <= '9') {
    number[pos] = info[str[curPos] - '0' - 1];
    curPos++;
    return;
  }

  curPos++;
  g[pos].push_back(pos * 2 + 1);
  parse(pos * 2 + 1);

  curPos++; // space

  g[pos].push_back(pos * 2 + 2);
  parse(pos * 2 + 2);

  curPos++;
  return;
}

int calc(int a, int b, int t) {
  if (t == -1)
    return a & b;
  if (t == -2)
    return a | b;
  return a ^ b;
}

int tmp[30] = {};

int all(int pos) {
  if (tmp[pos] >= 0) {
    return number[pos];
  } else {
    return calc(all(pos * 2 + 1), all(pos * 2 + 2), tmp[pos]);
  }
}

int answer = 0;
int dfs(int pos) {
  if (pos == mx + 1) {
    int r = all(0);
    answer += r == 15;
    return 0;
  }
  if (number[pos] == -1) {
    for (int i = 1; i <= 3; i++) {
      tmp[pos] = -i;
      dfs(pos + 1);
    }
  } else {
    tmp[pos] = number[pos];
    dfs(pos + 1);
  }
}

int main() {
  int n;
  while (1) {
    while (getline(cin, str) && (str == "" || str == "END")) {
      if (str == "END")
        return 0;
    }

    for (int i = 0; i < 18; i++)
      number[i] = -1;
    cin >> n;
    g.clear();
    g.resize(18);
    for (int i = 0; i < n; i++) {
      int inf = 0, a;
      for (int j = 0; j < 4; j++) {
        cin >> a;
        inf |= (a << j);
      }
      info[i] = inf;
    }
    curPos = 0;
    mx = 0;
    parse(0);
    /*for(int i = 0 ; i <= mx ; i++){
            cout << i << "(" << number[i] << ")" << " : ";
            for(int j =0 ; j < g[i].size() ; j++) cout << g[i][j] << " ";
            cout << endl;
    }*/
    answer = 0;
    dfs(0);
    cout << answer << endl;
  }
}