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

#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
ll mod = 1000000007;
ll expr(string &s, int a, int b);
ll term(string &s, int a, int b);
ll number(string &s, int a, int b);

ll calc(string &s, int &t) {
  ll ans;
  bool flag = 0;
  while (1) {
    if (s[t] == '>' || s[t] == '$') {
      t++;
      break;
    }
    if (s[t] >= '0' && s[t] <= '9') {
      ll tmp = 0;
      while (s[t] >= '0' && s[t] <= '9') {
        tmp *= 10;
        tmp += (ll)(s[t] - '0');
        t++;
      }
      if (!flag) {
        ans = tmp;
        flag = 1;
      } else {
        if (tmp >= 30)
          ans = 0;
        else
          for (int i = 0; i < tmp; i++) {
            ans /= 2;
          }
      }
    }
    if (s[t] == 'p') {
      t++;
    }
    if (s[t] == 'S') {
      t += 2;
      ll tmp = calc(s, t);
      tmp = tmp * tmp % mod;
      if (!flag) {
        ans = tmp;
        flag = 1;
      } else {
        if (tmp >= 30)
          ans = 0;
        else
          for (int i = 0; i < tmp; i++) {
            ans /= 2;
          }
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    string s1, s;
    getline(cin, s1);
    if (s1[0] == '#')
      break;
    for (int i = 0; i < s1.size(); i++) {
      if (s1[i] != ' ') {
        s.push_back(s1[i]);
      }
    }
    vector<bool> p((int)s.size());
    for (int i = 0; i < s.size(); i++) {
      if (s[i] != '<' && s[i] != '>') {
        if (i == 0)
          continue;
        if (s[i - 1] == '>') {
          p[i - 1] = 1;
          p[i - 2] = 1;
        }
      }
    }
    string ss;
    int tmp = 0;
    while (tmp < (int)s.size()) {
      if (p[tmp]) {
        ss.push_back('p');
        tmp += 2;
      } else {
        ss.push_back(s[tmp]);
        tmp++;
      }
    }
    ss.push_back('$');
    int cnt = 0;
    cout << calc(ss, cnt) << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
ll mod = 1000000007;
ll expr(string &s, int a, int b);
ll term(string &s, int a, int b);
ll number(string &s, int a, int b);

ll calc(string &s, int &t) {
  ll ans;
  bool flag = 0;
  while (t < s.size()) {
    if (s[t] == '>' || s[t] == '$') {
      t++;
      break;
    }
    if (s[t] >= '0' && s[t] <= '9') {
      ll tmp = 0;
      while (s[t] >= '0' && s[t] <= '9') {
        tmp *= 10;
        tmp += (ll)(s[t] - '0');
        t++;
      }
      if (!flag) {
        ans = tmp;
        flag = 1;
      } else {
        if (tmp >= 30)
          ans = 0;
        for (int i = 0; i < tmp; i++) {
          ans /= 2;
        }
      }
    }
    if (s[t] == 'p') {
      t++;
    }
    if (s[t] == 'S') {
      t += 2;
      ll tmp = calc(s, t);
      tmp = tmp * tmp % mod;
      if (!flag) {
        ans = tmp;
        flag = 1;
      } else {
        if (tmp >= 30)
          ans = 0;
        for (int i = 0; i < tmp; i++) {
          ans /= 2;
        }
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    string s1, s;
    getline(cin, s1);
    if (s1[0] == '#')
      break;
    for (int i = 0; i < s1.size(); i++) {
      if (s1[i] != ' ') {
        s.push_back(s1[i]);
      }
    }
    vector<bool> p((int)s.size());
    for (int i = 0; i < s.size(); i++) {
      if (s[i] != '<' && s[i] != '>') {
        if (i == 0)
          continue;
        if (s[i - 1] == '>') {
          p[i - 1] = 1;
          p[i - 2] = 1;
        }
      }
    }
    string ss;
    int tmp = 0;
    while (tmp < (int)s.size()) {
      if (p[tmp]) {
        ss.push_back('p');
        tmp += 2;
      } else {
        ss.push_back(s[tmp]);
        tmp++;
      }
    }
    ss.push_back('$');
    int cnt = 0;
    cout << calc(ss, cnt) << endl;
  }
  return 0;
}