Problem p01105 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

/* 再帰降下法 */
// char *p = str; res = expr(p);

int val = 0;

int term(char *&s);
int number(char *&s);
int factor(char *&s);
int expr(char *&s);

// 数
int number(char *&s) {
  int res = 0;

  if (*s == '0' || *s == '1')
    return (*s++) - '0';
  if ('a' <= *s && *s <= 'd')
    return (val >> ((*s++) - 'a')) % 2;

  return res;
}

// 乗算除算(優先順位:高)
int term(char *&s) {
  int res = factor(s);
  while (1) {
    if (*s == '^')
      res ^= factor(++s);
    else if (*s == '*')
      res *= factor(++s);
    else
      break;
  }
  return res;
}

// 括弧か数
int factor(char *&s) {
  if (*s == '-')
    return !factor(++s);
  if (*s != '(')
    return number(s);
  int res = expr(++s);
  s++;
  return res;
}

// 式(優先順位: 低)
int expr(char *&s) {
  int res = term(s);
  return res;
}

int check(char *s) {
  int res = 0;
  for (int i = 0; i < 16; i++) {
    char *p = s;
    val = i;
    res = res * 2 + expr(p);
  }
  return res;
}

vector<string> vec[16];
int par = 0;

void make_expr() {
  string s = ".";
  for (int i = 0; i < 6; i++) {
    s[0] = "01abcd"[i];
    vec[1].push_back(s);
  }

  for (int i = 2; i <= 15; i++) {

    for (int j = 0; j < vec[i - 1].size(); j++) {
      vec[i].push_back("-" + vec[i - 1][j]);
    }

    for (int j = 1; j < i - 3; j++) {
      int r = i - 3 - j;
      if (j > r)
        continue;

      for (int p = 0; p < vec[j].size(); p++) {
        for (int q = 0; q < vec[r].size(); q++) {
          vec[i].push_back("(" + vec[j][p] + "*" + vec[r][q] + ")");
          vec[i].push_back("(" + vec[j][p] + "^" + vec[r][q] + ")");
          assert(j + r + 3 == i);
        }
      }
    }

    sort(vec[i].begin(), vec[i].end());
    vec[i].erase(unique(vec[i].begin(), vec[i].end()), vec[i].end());
    debug(vec[i].size());
  }
}

bool solve() {
  char s[30];

  scanf("%s", s);
  if (s[0] == '.')
    return 0;

  int res = check(s);

  for (int i = 1; i <= 15; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      char s[17];
      strcpy(s, vec[i][j].c_str());
      if (res == check(s)) {
        printf("%d\n", i);
        return 1;
      }
    }
  }

  printf("%d\n", 16);

  return 1;
}

int main() {
  make_expr();

  while (solve())
    ;

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

/* 再帰降下法 */
// char *p = str; res = expr(p);

int val = 0;

int term(char *&s);
int number(char *&s);
int factor(char *&s);
int expr(char *&s);

// 数
int number(char *&s) {
  int res = 0;

  if (*s == '0' || *s == '1')
    return (*s++) - '0';
  if ('a' <= *s && *s <= 'd')
    return (val >> ((*s++) - 'a')) % 2;

  return res;
}

// 乗算除算(優先順位:高)
int term(char *&s) {
  int res = factor(s);
  while (1) {
    if (*s == '^')
      res ^= factor(++s);
    else if (*s == '*')
      res *= factor(++s);
    else
      break;
  }
  return res;
}

// 括弧か数
int factor(char *&s) {
  if (*s == '-')
    return !factor(++s);
  if (*s != '(')
    return number(s);
  int res = expr(++s);
  s++;
  return res;
}

// 式(優先順位: 低)
int expr(char *&s) {
  int res = term(s);
  return res;
}

int check(char *s) {
  int res = 0;
  for (int i = 0; i < 16; i++) {
    char *p = s;
    val = i;
    res = res * 2 + expr(p);
  }
  return res;
}

vector<string> vec[16];
int par = 0;

void make_expr() {
  string s = ".";
  for (int i = 0; i < 6; i++) {
    s[0] = "01abcd"[i];
    vec[1].push_back(s);
  }

  for (int i = 2; i <= 15; i++) {

    for (int j = 0; j < vec[i - 1].size(); j++) {
      vec[i].push_back("-" + vec[i - 1][j]);
    }

    for (int j = 1; j < i - 3; j++) {
      int r = i - 3 - j;
      if (j > r)
        continue;

      for (int p = 0; p < vec[j].size(); p++) {
        for (int q = 0; q < vec[r].size(); q++) {
          vec[i].push_back("(" + vec[j][p] + "*" + vec[r][q] + ")");
          vec[i].push_back("(" + vec[j][p] + "^" + vec[r][q] + ")");
          assert(j + r + 3 == i);
        }
      }
    }

    sort(vec[i].begin(), vec[i].end());
    vec[i].erase(unique(vec[i].begin(), vec[i].end()), vec[i].end());
    // debug(vec[i].size());
  }
}

bool solve() {
  char s[30];

  scanf("%s", s);
  if (s[0] == '.')
    return 0;

  int res = check(s);

  for (int i = 1; i <= 15; i++) {
    for (int j = 0; j < vec[i].size(); j++) {
      char s[17];
      strcpy(s, vec[i][j].c_str());
      if (res == check(s)) {
        printf("%d\n", i);
        return 1;
      }
    }
  }

  printf("%d\n", 16);

  return 1;
}

int main() {
  make_expr();

  while (solve())
    ;

  return 0;
}