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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &i : v)
    is >> i;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  const string delimiter = "\\n";
  REP(i, v.size()) {
    os << v[i];
    if (i != v.size() - 1)
      os << delimiter;
  }
  return os;
}
/*--------------------template--------------------*/

int add();

string s;
int p;

int num() {
  int res;
  if (s[p] == '(') {
    p++;
    res = add();
    assert(s[p] == ')');
    p++;
  } else if (isdigit(s[p])) {
    res = 0;
    while (p < s.size() && isdigit(s[p])) {
      res *= 10;
      res += s[p] - '0';
      p++;
    }
  } else
    assert(false);
  return res;
}

int mul() {
  int res = num();
  while (p < s.size() && (s[p] == '*' || s[p] == '/')) {
    char c = s[p];
    p++;
    int r = num();
    if (c == '*') {
      res *= r;
    } else if (c == '/') {
      res /= r;
    }
  }
  return res;
}

int add() {
  int res = mul();
  while (p < s.size() && (s[p] == '+' || s[p] == '-')) {
    char c = s[p];
    p++;
    int r = mul();
    if (c == '+') {
      res += r;
    } else if (c == '-') {
      res -= r;
    }
  }
  return res;
}

int solve() {
  p = 0;
  return add();
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  int n;
  cin >> n;
  REP(cs, n) {
    cin >> s;
    s = s.substr(0, s.size() - 1);
    cout << solve() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) begin(a), end(a)
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge &e) const { return cost < e.cost; }
  bool operator>(const edge &e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

int calc(string s) {
  vi v;
  REP(i, s.size()) {
    if (s[i] == '+')
      v.push_back(0);
    else if (s[i] == '-')
      v.push_back(1);
    else if (s[i] == '*')
      v.push_back(2);
    else if (s[i] == '/')
      v.push_back(3);
    else {
      int tmp = s[i] - '0';
      int t = i + 1;
      while (isdigit(s[t])) {
        tmp = tmp * 10 + s[t] - '0';
        t++;
      }
      i = t - 1;
      v.push_back(tmp);
    }
  }

  while (v.size() > 1) {
    for (int i = 1; i < v.size(); i += 2) {
      if (v[i] == 2) {
        int tmp = v[i - 1] * v[i + 1];
        v[i - 1] = tmp;
        v.erase(v.begin() + i + 1);
        v.erase(v.begin() + i);
        continue;
      } else if (v[i] == 3) {
        int tmp = v[i - 1] / v[i + 1];
        v[i - 1] = tmp;
        v.erase(v.begin() + i + 1);
        v.erase(v.begin() + i);
        continue;
      }
    }
    for (int i = 1; i < v.size(); i += 2) {
      if (v[i] == 0) {
        int tmp = v[i - 1] + v[i + 1];
        v[i - 1] = tmp;
        v.erase(v.begin() + i + 1);
        v.erase(v.begin() + i);
        break;
      } else if (v[i] == 1) {
        int tmp = v[i - 1] - v[i + 1];
        v[i - 1] = tmp;
        v.erase(v.begin() + i + 1);
        v.erase(v.begin() + i);
        break;
      }
    next:;
    }
  }
  return v[0];
}

int solve(string s) {
  while (1) {
    if (find(ALL(s), '(') == s.end()) {
      return calc(s);
    } else {
      string tmp;
      REP(i, s.size()) {
        if (s[i] != '(')
          tmp.push_back(s[i]);
        else {
          int j;
          for (j = i;; j++) {
            if (s[j] == ')') {
              int t = solve(s.substr(i + 1, j - i - 1));
              tmp += to_string(t);
              break;
            } else if (s[j] == '(')
              i = j;
          }
          i = j;
        }
      }
      return solve(tmp);
    }
  }
  return 0;
}

int main() {
  int sets;
  string s;
  cin >> sets;
  REP(set, sets) {
    cin >> s;
    s.erase(s.size() - 1);
    cout << solve(s) << endl;
  }
  return 0;
}