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

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

set<string> s;
int dp[1 << 16];
int place;

int dfs2(string str) {
  if (str[place] == '-') {
    place++;
    return 1 - dfs2(str);
  } else if (str[place] == '(') {
    place++;
    int a = dfs2(str);
    char c = str[place];
    place++;
    int b = dfs2(str);
    place++;
    if (c == '^')
      return a ^ b;
    else
      return a & b;
  } else {
    return str[place++] - '0';
  }
}

int f(string str) {
  place = 0;
  return dfs2(str);
}

int investigate(string str) {
  int ret = 0;
  REP(i, (1 << 4)) {
    string now = "";
    REP(j, str.size()) {
      if (str[j] >= 'a' && str[j] <= 'd') {
        now += (char)('0' + ((i & (1 << (str[j] - 'a'))) > 0));
      } else {
        now += str[j];
      }
    }
    ret |= (f(now) << i);
  }
  return dp[ret] = min(dp[ret], (int)str.size());
}

void check(string str) {
  int cou = 0;
  REP(i, str.size()) {
    if (str[i] == 'X')
      cou++;
  }
  int num = 1;
  REP(i, cou) num *= 6;
  REP(i, num) {
    string now = "";
    int tmp = i;
    REP(j, str.size()) {
      if (str[j] == 'X') {
        int seica = tmp % 6;
        if (seica < 4) {
          now += (char)('a' + seica);
        } else {
          now += (char)('0' + (seica - 4));
        }
        tmp /= 6;
      } else {
        now += str[j];
      }
    }
    investigate(now);
  }
}

void dfs(string str) {
  if (str.size() > 14 || s.count(str))
    return;
  s.insert(str);
  check(str);
  REP(i, str.size()) {
    if (str[i] == 'X') {
      dfs(str.substr(0, max(0LL, i)) + "(X*X)" + str.substr(i + 1));
      dfs(str.substr(0, max(0LL, i)) + "(X^X)" + str.substr(i + 1));
      if (!(i > 0 && str[i - 1] == '-'))
        dfs(str.substr(0, max(0LL, i)) + "-X" + str.substr(i + 1));
    }
  }
}

int main() {
  REP(i, 1 << 16) dp[i] = INF;
  dfs("X");
  string str;
  while (cin >> str, str[0] != '.') {
    cout << investigate(str) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;

set<string> s;
int dp[1 << 16];
int place;

int dfs2(string str) {
  if (str[place] == '-') {
    place++;
    return 1 - dfs2(str);
  } else if (str[place] == '(') {
    place++;
    int a = dfs2(str);
    char c = str[place];
    place++;
    int b = dfs2(str);
    place += 2;
    if (c == '^')
      return a ^ b;
    else
      return a & b;
  } else {
    return str[place++] - '0';
  }
}

int f(string str) {
  place = 0;
  return dfs2(str);
}

void investigate(string str) {
  int ret = 0;
  REP(i, (1 << 4)) {
    bool flag = false;
    string now = "";
    REP(j, str.size()) {
      if (str[j] >= 'a' && str[j] <= 'd') {
        flag = true;
        now += (char)('0' + (i & (1 << (str[j] - 'a'))));
      } else {
        now += str[j];
      }
    }
    ret |= (f(now) << i);
    if (!flag) {
      if (ret)
        ret = (1 << 16) - 1;
      else
        ret = 0;
      break;
    }
  }
  dp[ret] = min(dp[ret], (int)str.size());
}

void check(string str) {
  int cou = 0;
  REP(i, str.size()) {
    if (str[i] == 'X')
      cou++;
  }
  int num = 1;
  REP(i, cou) num *= 6;
  REP(i, num) {
    string now = "";
    int tmp = num;
    REP(j, str.size()) {
      if (str[j] == 'X') {
        int seica = num % 6;
        if (seica < 4) {
          now += (char)('a' + seica);
        } else {
          now += (char)('0' + (seica - 4));
        }
        tmp /= 6;
      } else {
        now += str[j];
      }
    }
    investigate(now);
  }
}

void dfs(string str) {
  if (str.size() >= 16 || s.count(str))
    return;
  s.insert(str);
  check(str);
  REP(i, str.size()) {
    if (str[i] == 'X') {
      dfs(str.substr(0, max(0LL, i)) + "(X*X)" + str.substr(i + 1));
      dfs(str.substr(0, max(0LL, i)) + "(X^X)" + str.substr(i + 1));
      if (!(i > 0 && str[i - 1] == '-'))
        dfs(str.substr(0, max(0LL, i)) + "-X" + str.substr(i + 1));
    }
  }
}

int main() {
  REP(i, 1 << 16) dp[i] = INF;
  dfs("X");
  string str;
  while (cin >> str, str[0] != '.') {
    int tmp = dp[f(str)];
    if (tmp != INF) {
      cout << tmp << endl;
    } else {
      cout << 16 << endl;
    }
  }

  return 0;
}