Problem p02100 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

string st;
int a;
int getnum() {
  int num = 0;
  while (a != st.size() && isdigit(st[a])) {
    num *= 10;
    num += st[a] - '0';
    a++;
  }
  return num;
}
vector<int> kou() {
  vector<int> v(10);
  if (st[a] == 'x') {
    a++;
    assert(st[a] == '^');
    a++;
    int n(getnum());
    v[n] = 1;
  } else {
    int kei(getnum());
    if (a == st.size() || st[a] == '+' || st[a] == '-') {
      v[0] = kei;

    } else {
      assert(st[a] == 'x');
      a++;
      if (a == st.size() || st[a] == '+' || st[a] == '-') {
        v[1] = kei;
      } else {
        assert(st[a] == '^');
        a++;
        int num(getnum());
        v[num] = kei;
      }
    }
  }
  return v;
}
vector<int> takou() {
  vector<int> v(kou());
  while (a != st.size()) {
    bool plus = true;
    plus = st[a] == '+';
    a++;
    vector<int> w(kou());
    if (plus) {
      for (int i = 0; i < 10; ++i) {
        v[i] += w[i];
      }
    } else {
      for (int i = 0; i < 10; ++i) {
        v[i] -= w[i];
      }
    }
  }
  return v;
}
long long int pow_int(long long int a, int b) {
  if (b == 0)
    return 1;
  else {
    return a * pow_int(a, b - 1);
  }
}
int main() {
  cin >> st;
  a = 0;
  auto v(takou());
  vector<long long int> anss;
  for (long long int i = -4000; i <= 4000; ++i) {
    long long int sum = 0;
    for (int j = 0; j <= 5; ++j) {
      sum += pow_int(i, j) * v[j];
    }
    if (sum == 0) {
      anss.push_back(i);
    }
  }
  reverse(anss.begin(), anss.end());

  for (auto ans : anss) {
    cout << "(x";
    if (ans > 0) {
      cout << "-" << ans;
    } else {
      cout << "+" << -ans;
    }
    cout << ")";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

string st;
int a;
int getnum() {
  int num = 0;
  while (a != st.size() && isdigit(st[a])) {
    num *= 10;
    num += st[a] - '0';
    a++;
  }
  return num;
}
vector<int> kou() {
  vector<int> v(10);
  if (st[a] == 'x') {
    a++;
    if (a == st.size() || st[a] != '^') {
      v[1] = 1;
    } else {

      assert(st[a] == '^');
      a++;
      int n(getnum());
      v[n] = 1;
    }
  } else {
    int kei(getnum());
    if (a == st.size() || st[a] == '+' || st[a] == '-') {
      v[0] = kei;

    } else {
      assert(st[a] == 'x');
      a++;
      if (a == st.size() || st[a] == '+' || st[a] == '-') {
        v[1] = kei;
      } else {
        assert(st[a] == '^');
        a++;
        int num(getnum());
        v[num] = kei;
      }
    }
  }
  return v;
}
vector<int> takou() {
  vector<int> v(kou());
  while (a != st.size()) {
    bool plus = true;
    plus = st[a] == '+';
    a++;
    vector<int> w(kou());
    if (plus) {
      for (int i = 0; i < 10; ++i) {
        v[i] += w[i];
      }
    } else {
      for (int i = 0; i < 10; ++i) {
        v[i] -= w[i];
      }
    }
  }
  return v;
}
long long int pow_int(long long int a, int b) {
  if (b == 0)
    return 1;
  else {
    return a * pow_int(a, b - 1);
  }
}
int main() {
  cin >> st;
  a = 0;
  auto v(takou());
  vector<long long int> anss;
  for (long long int i = -4000; i <= 4000; ++i) {
    long long int sum = 0;
    for (int j = 0; j <= 5; ++j) {
      sum += pow_int(i, j) * v[j];
    }
    if (sum == 0) {
      anss.push_back(i);
    }
  }
  reverse(anss.begin(), anss.end());

  for (auto ans : anss) {
    cout << "(x";
    if (ans > 0) {
      cout << "-" << ans;
    } else {
      cout << "+" << -ans;
    }
    cout << ")";
  }
  cout << endl;

  return 0;
}