Problem p00041 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

string str;
char cc[4] = {'a', '+', '-', '*'};
char cn[5] = {'q', 'a', 'b', 'c', 'd'};
int calc(int a, int b, int c) {
  if (c == 1)
    return a + b;
  if (c == 2)
    return a - b;
  if (c == 3)
    return a * b;
}

bool solve(int a, int b, int c, int d) {
  bool f = false;
  ;
  int be = -1000, af = -1000, ch = -1000;
  int sum = 0, ans;
  if (a == -1000)
    sum++;
  else
    ans = a;
  if (b == -1000)
    sum++;
  else
    ans = b;
  if (c == -1000)
    sum++;
  else
    ans = c;
  if (d == -1000)
    sum++;
  else
    ans = d;
  if (sum == 3) {
    if (ans == 10)
      return true;
    else
      return false;
  }

  if (a != -1000 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (b != -1000 && f == false) {
        if (f == false)
          be = 1, af = 2, ch = i;
        f = solve(-1000, calc(a, b, i), c, d);
      }
      if (c != -1000 && f == false) {
        if (f == false)
          be = 1, af = 3, ch = i;
        f = solve(-1000, b, calc(a, c, i), d);
      }
      if (d != -1000 && f == false) {
        if (f == false)
          be = 1, af = 4, ch = i;
        f = solve(-1000, b, c, calc(a, d, i));
      }
    }
  }
  if (b != -1000 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1000 && f == false) {
        if (f == false)
          be = 2, af = 1, ch = i;
        f = solve(calc(b, a, i), -1000, c, d);
      }
      if (c != -1000 && f == false) {
        if (f == false)
          be = 2, af = 3, ch = i;
        f = solve(a, -1000, calc(b, c, i), d);
      }
      if (d != -1000 && f == false) {
        if (f == false)
          be = 2, af = 4, ch = i;
        f = solve(a, -1000, c, calc(b, d, i));
      }
    }
  }
  if (c != -1000 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1000 && f == false) {
        if (f == false)
          be = 3, af = 1, ch = i;
        f = solve(calc(c, a, i), b, -1000, d);
      }
      if (b != -1000 && f == false) {
        if (f == false)
          be = 3, af = 2, ch = i;
        f = solve(a, calc(c, b, i), -1000, d);
      }
      if (d != -1000 && f == false) {
        if (f == false)
          be = 3, af = 4, ch = i;
        f = solve(a, b, -1000, calc(c, d, i));
      }
    }
  }
  if (d != -1000 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1000 && f == false) {
        if (f == false)
          be = 4, af = 1, ch = i;
        f = solve(calc(d, a, i), b, c, -1000);
      }
      if (b != -1000 && f == false) {
        if (f == false)
          be = 4, af = 2, ch = i;
        f = solve(a, calc(d, b, i), c, -1000);
      }
      if (c != -1000 && f == false) {
        if (f == false)
          be = 4, af = 3, ch = i;
        f = solve(a, b, calc(d, c, i), -1000);
      }
    }
  }
  if (f == true) {
    if (be != -1000 && af != -1000 && ch != -1000) {
      if (str.empty()) {
        str = '(';
        str += cn[be];
        str += cc[ch];
        str += cn[af];
        str += ')';

      } else {
        if (str.find(cn[be]) != -1) {
          int pos = str.find(cn[be]);
          str.erase(pos, 1);
          str.insert(pos, "(");
          str.insert(pos + 1, 1, cn[be]);
          str.insert(pos + 2, 2, cc[ch]);
          str.insert(pos + 3, 3, cn[af]);
          str.insert(pos + 4, ")");
        } else if (str.find(cn[af]) != -1) {
          int pos = str.find(cn[af]);
          str.erase(pos, 1);
          str.insert(pos, "(");
          str.insert(pos + 1, 1, cn[be]);
          str.insert(pos + 2, 1, cc[ch]);
          str.insert(pos + 3, 1, cn[af]);
          str.insert(pos + 4, ")");
        }
      }
    }
    return true;
  } else
    return false;
}

int main(void) {
  int a, b, c, d;
  int pos;
  while (1) {
    cin >> a >> b >> c >> d;
    str = ("");
    if (a == 0 && b == 0 && c == 0 && d == 0)
      break;
    solve(a, b, c, d);
    if (!str.empty()) {
      pos = str.find('a');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(a + '0'));
      pos = str.find('b');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(b + '0'));
      pos = str.find('c');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(c + '0'));
      pos = str.find('d');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(d + '0'));
      cout << str << endl;
    } else
      cout << "0" << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

string str;
char cc[4] = {'a', '+', '-', '*'};
char cn[5] = {'q', 'a', 'b', 'c', 'd'};
int calc(int a, int b, int c) {
  if (c == 1)
    return a + b;
  if (c == 2)
    return a - b;
  if (c == 3)
    return a * b;
}

bool solve(int a, int b, int c, int d) {
  bool f = false;
  ;
  int be = -1, af = -1, ch = -1;
  int sum = 0, ans;
  if (a == -1)
    sum++;
  else
    ans = a;
  if (b == -1)
    sum++;
  else
    ans = b;
  if (c == -1)
    sum++;
  else
    ans = c;
  if (d == -1)
    sum++;
  else
    ans = d;
  if (sum == 3) {
    if (ans == 10)
      return true;
    else
      return false;
  }

  if (a != -1 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (b != -1 && f == false) {
        if (f == false)
          be = 1, af = 2, ch = i;
        f = solve(-1, calc(a, b, i), c, d);
      }
      if (c != -1 && f == false) {
        if (f == false)
          be = 1, af = 3, ch = i;
        f = solve(-1, b, calc(a, c, i), d);
      }
      if (d != -1 && f == false) {
        if (f == false)
          be = 1, af = 4, ch = i;
        f = solve(-1, b, c, calc(a, d, i));
      }
    }
  }
  if (b != -1 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1 && f == false) {
        if (f == false)
          be = 2, af = 1, ch = i;
        f = solve(calc(b, a, i), -1, c, d);
      }
      if (c != -1 && f == false) {
        if (f == false)
          be = 2, af = 3, ch = i;
        f = solve(a, -1, calc(b, c, i), d);
      }
      if (d != -1 && f == false) {
        if (f == false)
          be = 2, af = 4, ch = i;
        f = solve(a, -1, c, calc(b, d, i));
      }
    }
  }
  if (c != -1 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1 && f == false) {
        if (f == false)
          be = 3, af = 1, ch = i;
        f = solve(calc(c, a, i), b, -1, d);
      }
      if (b != -1 && f == false) {
        if (f == false)
          be = 3, af = 2, ch = i;
        f = solve(a, calc(c, b, i), -1, d);
      }
      if (d != -1 && f == false) {
        if (f == false)
          be = 3, af = 4, ch = i;
        f = solve(a, b, -1, calc(c, d, i));
      }
    }
  }
  if (d != -1 && f == false) {
    for (int i = 1; i <= 3; i++) {
      if (a != -1 && f == false) {
        if (f == false)
          be = 4, af = 1, ch = i;
        f = solve(calc(d, a, i), b, c, -1);
      }
      if (b != -1 && f == false) {
        if (f == false)
          be = 4, af = 2, ch = i;
        f = solve(a, calc(d, b, i), c, -1);
      }
      if (c != -1 && f == false) {
        if (f == false)
          be = 4, af = 3, ch = i;
        f = solve(a, b, calc(d, c, i), -1);
      }
    }
  }
  if (f == true) {
    if (be != -1 && af != -1 && ch != -1) {
      if (str.empty()) {
        str = '(';
        str += cn[be];
        str += cc[ch];
        str += cn[af];
        str += ')';

      } else {
        if (str.find(cn[be]) != -1) {
          int pos = str.find(cn[be]);
          str.erase(pos, 1);
          str.insert(pos, "(");
          str.insert(pos + 1, 1, cn[be]);
          str.insert(pos + 2, 2, cc[ch]);
          str.insert(pos + 3, 3, cn[af]);
          str.insert(pos + 4, ")");
        } else if (str.find(cn[af]) != -1) {
          int pos = str.find(cn[af]);
          str.erase(pos, 1);
          str.insert(pos, "(");
          str.insert(pos + 1, 1, cn[be]);
          str.insert(pos + 2, 1, cc[ch]);
          str.insert(pos + 3, 1, cn[af]);
          str.insert(pos + 4, ")");
        }
      }
    }
    return true;
  } else
    return false;
}

int main(void) {
  int a, b, c, d;
  int pos;
  while (1) {
    cin >> a >> b >> c >> d;
    str = ("");
    if (a == 0 && b == 0 && c == 0 && d == 0)
      break;
    solve(a, b, c, d);
    if (!str.empty()) {
      pos = str.find('a');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(a + '0'));
      pos = str.find('b');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(b + '0'));
      pos = str.find('c');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(c + '0'));
      pos = str.find('d');
      str.erase(pos, 1);
      str.insert(pos, 1, (char)(d + '0'));
      cout << str << endl;
    } else
      cout << "0" << endl;
  }
  return 0;
}