Problem p02100 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long
#define int long long
#define ALL(x) (x).begin(), (x).end()

using namespace std;

ll f(vector<ll> &a, ll x) {
  return a[0] + a[1] * x + a[2] * x * x + a[3] * x * x * x +
         a[4] * x * x * x * x + a[5] * x * x * x * x * x;
}

signed main() {
  string str;
  cin >> str;
  vector<ll> a(10, 0);
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == '+' || str[i] == '-') {
      int j = i;
      while (str[j] != 'x' && j < str.size())
        j++;
      int s;
      if (j < str.size()) {
        if (j != str.size() && str[j + 1] == '^') {
          s = str[j + 2] - '0';
        } else {
          s = 1;
        }
      } else {
        s = 0;
      }
      string temp = str.substr(i + 1, j - i - 1);
      bool flag = true;
      for (auto t : temp) {
        if (t < '0' || t > '9') {
          flag = false;
          break;
        }
      }
      if (flag) {
        a[s] = stoll(temp);
      } else {
        a[s] = 1;
      }
      if (str[i] == '-') {
        a[s] *= -1;
      }
    }
  }
  if (str[0] == 'x') {
    if (str[1] == '^') {
      a[str[2] - '0'] = 1;
    } else {
      a[1] = 1;
    }
  }
  vector<ll> b;
  for (ll i = -2 * 1000; i <= 2 * 1000; i++) {
    if (f(a, i) == 0LL) {
      b.push_back(i);
    }
  }
  sort(ALL(b));
  for (int i = b.size() - 1; i >= 0; i--) {
    cout << "(x";
    if (b[i] > 0) {
      cout << "-";
    } else {
      cout << "+";
    }
    cout << llabs(b[i]);
    cout << ")";
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long
#define int long long
#define ALL(x) (x).begin(), (x).end()

using namespace std;

ll f(vector<ll> &a, ll x) {
  return a[0] + a[1] * x + a[2] * x * x + a[3] * x * x * x +
         a[4] * x * x * x * x + a[5] * x * x * x * x * x;
}

signed main() {
  string str;
  cin >> str;
  vector<ll> a(10, 0);
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == '+' || str[i] == '-') {
      int j = i;
      while (str[j] != 'x' && j < str.size())
        j++;
      int s;
      if (j < str.size()) {
        if (j != str.size() && str[j + 1] == '^') {
          s = str[j + 2] - '0';
        } else {
          s = 1;
        }
      } else {
        s = 0;
      }
      string temp = str.substr(i + 1, j - i - 1);
      bool flag = true;
      for (auto t : temp) {
        if (t < '0' || t > '9') {
          flag = false;
          break;
        }
      }
      if (flag && temp.size() > 0) {
        try {
          a[s] = stoll(temp);
        } catch (const std::invalid_argument) {
          continue;
        }
      } else {
        a[s] = 1;
      }
      if (str[i] == '-') {
        a[s] *= -1;
      }
    }
  }
  if (str[0] == 'x') {
    if (str[1] == '^') {
      a[str[2] - '0'] = 1;
    } else {
      a[1] = 1;
    }
  }
  vector<ll> b;
  for (ll i = -2 * 1000; i <= 2 * 1000; i++) {
    if (f(a, i) == 0LL) {
      b.push_back(i);
    }
  }
  sort(ALL(b));
  for (int i = b.size() - 1; i >= 0; i--) {
    cout << "(x";
    if (b[i] > 0) {
      cout << "-";
    } else {
      cout << "+";
    }
    cout << llabs(b[i]);
    cout << ")";
  }
  cout << endl;

  return 0;
}