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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

typedef string::const_iterator State;
class ParseError {};
void sp(State &);
int expr(State &);
int term(State &);
int number(State &);

string s;
int expr(State &begin) {
  int ret = term(begin);
  while (s.end() - begin >= 2) {
    // cout << *begin << " " << begin-s.begin() << " " << ret << endl;
    if (*begin == '>' && *(begin + 1) == '>' && *(begin + 2) != '>') {
      begin++; // >
      begin++; // >
      ret >>= min(term(begin), 31);
    } else {
      break;
    }
  }
  // cout << "expr:" << ret << endl;
  return ret;
}

int term(State &begin) {
  if (*begin == 'S') {
    begin++;
    begin++;
    // cout << "s " << *begin << " " << begin-s.begin() << endl;
    ll ret = expr(begin);
    ret = (ret * ret) % MOD;
    begin++;
    // cout << "te:" << ret << endl;
    return ret;
  } else {
    // cout << "tn:" << *begin << " " << begin - s.begin() << endl;
    int ret = number(begin);
    // cout << "tenum:" << ret << endl;
    return ret;
  }
}

int number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

signed main(void) {
  while (true) {
    string t;
    getline(cin, t);
    if (t == "#")
      break;

    s = "";
    REP(i, t.size()) if (t[i] != ' ') s += t[i];
    // cout << s << endl;
    State begin = s.begin();
    cout << expr(begin) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

typedef string::const_iterator State;
class ParseError {};
void sp(State &);
int expr(State &);
int term(State &);
int number(State &);

string s;
int expr(State &begin) {
  int ret = term(begin);
  while (1) {
    // cout << *begin << " " << begin-s.begin() << " " << ret << endl;
    if (*begin == '>' && *(begin + 1) == '>' && *(begin + 2) != '>') {
      begin++; // >
      begin++; // >
      int y = term(begin);
      REP(i, y) ret /= 2;
    } else {
      break;
    }
  }
  // cout << "expr:" << ret << endl;
  return ret;
}

int term(State &begin) {
  if (*begin == 'S') {
    begin++;
    begin++;
    // cout << "s " << *begin << " " << begin-s.begin() << endl;
    int ret = expr(begin);
    ret = (ret * ret) % MOD;
    begin++;
    // cout << "te:" << ret << endl;
    return ret;
  } else {
    // cout << "tn:" << *begin << " " << begin - s.begin() << endl;
    int ret = number(begin);
    // cout << "tenum:" << ret << endl;
    return ret;
  }
}

int number(State &begin) {
  int ret = 0;
  while (isdigit(*begin)) {
    ret *= 10;
    ret += *begin - '0';
    begin++;
  }
  return ret;
}

signed main(void) {
  while (true) {
    string t;
    getline(cin, t);
    if (t == "#")
      break;

    s = "";
    REP(i, t.size()) if (t[i] != ' ') s += t[i];
    // cout << s << endl;
    State begin = s.begin();
    cout << expr(begin) << endl;
  }

  return 0;
}