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

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <stack>
#include <vector>
#define mod 1000000007
using namespace std;

typedef unsigned long long ll;

vector<string> a;
map<int, int> m;

string f1(string a) {
  ll x = (ll)atoi(a.c_str());
  x = (x * x) % mod;
  ostringstream os;
  os << x;
  return os.str();
}

string f2(string a, string b) {
  ll x = atoi(a.c_str()), y = atoi(b.c_str()), c = 1;
  for (int i = 0; i < y; i++) {
    if (x < c)
      return "0";
    c *= 2;
  }
  x = x / c;
  ostringstream os;
  os << x;
  return os.str();
}

void rec(int s, int t) {
  for (int i = s; i < t; i++) {
    if (a[i] == "<") {
      int l = i + 1, r = m[i];
      rec(l, r);
      i = r - 1;
    } else if (a[i] == ">>") {
      if (a[i + 1] == "<") {
        int l = i + 1, r = m[i + 1];
        rec(l, r + 1);
        a[r] = f2(a[i - 1], a[r]);
        i = r;
      } else
        a[i + 1] = f2(a[i - 1], a[i + 1]);
    } else if (a[i] == ">")
      a[i] = f1(a[i - 1]);
    else {
    }
  }
}

int main() {
  while (1) {
    char c;
    string s, t = "";
    vector<string> b;
    getline(cin, s);
    if (s[0] == '#')
      break;
    a.clear();
    for (int i = 0; i < s.length(); i++)
      if (s[i] != ' ')
        t += s[i];
    string tmp = "";
    for (int i = 0; i < t.length(); i++) {
      if (t[i] == 'S') {
        b.push_back("<");
        i++;
      } else if (t[i] == '>') {
        if (tmp != "")
          b.push_back(tmp);
        tmp = "";
        b.push_back(">");
      } else
        tmp += t[i];
      if (i == t.length() - 1 && tmp != "")
        b.push_back(tmp);
    }

    for (int i = b.size() - 1; 0 < i; i--) {
      if (b[i] != ">") {
        a.push_back(b[i]);
        if (b[i - 1] == ">") {
          i -= 2;
          a.push_back(">>");
        }
      } else
        a.push_back(b[i]);
    }
    a.push_back(b[0]);
    reverse(a.begin(), a.end());
    stack<int> st;
    for (int i = 0; i < a.size(); i++) {
      if (a[i] == "<")
        st.push(i);
      if (a[i] == ">") {
        int a = st.top();
        st.pop();
        m[a] = i;
      }
    }
    rec(0, a.size());
    cout << a[a.size() - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <vector>
#define mod 1000000007
using namespace std;

typedef unsigned long long ll;

vector<string> a;

string f1(string a) {
  ll x = (ll)atoi(a.c_str());
  x = (x * x) % mod;
  ostringstream os;
  os << x;
  return os.str();
}

string f2(string a, string b) {
  ll x = atoi(a.c_str()), y = atoi(b.c_str()), c = 1;
  for (int i = 0; i < y; i++) {
    if (x < c)
      return "0";
    c *= 2;
  }
  x = x / c;
  ostringstream os;
  os << x;
  return os.str();
}

void rec(int s, int t) {
  for (int i = s; i < t; i++) {
    if (a[i] == "<") {
      int l = 1, r = 0;
      for (int j = i + 1; j < a.size(); j++) {
        if (a[j] == "<")
          l++;
        if (a[j] == ">")
          r++;
        if (l == r) {
          rec(i + 1, j);
          i = j - 1;
          break;
        }
      }
    } else if (a[i] == ">>") {
      if (a[i + 1] == "<") {
        int l = 0, r = 0;
        vector<string> b;
        for (int j = i + 1; j < a.size(); j++) {
          b.push_back(a[j]);
          if (a[j] == "<")
            l++;
          if (a[j] == ">")
            r++;
          if (l == r) {
            rec(i + 1, j + 1);
            a[j] = f2(a[i - 1], a[j]);
            i = j;
            break;
          }
        }
      } else
        a[i + 1] = f2(a[i - 1], a[i + 1]);
    } else if (a[i] == ">")
      a[i] = f1(a[i - 1]);
    else {
    }
  }
}

int main() {
  while (1) {
    char c;
    string s, t = "";
    vector<string> b;
    getline(cin, s);
    if (s[0] == '#')
      break;
    a.clear();
    for (int i = 0; i < s.length(); i++)
      if (s[i] != ' ')
        t += s[i];
    string tmp = "";
    for (int i = 0; i < t.length(); i++) {
      if (t[i] == 'S') {
        b.push_back("<");
        i++;
      } else if (t[i] == '>') {
        if (tmp != "")
          b.push_back(tmp);
        tmp = "";
        b.push_back(">");
      } else
        tmp += t[i];
      if (i == t.length() - 1 && tmp != "")
        b.push_back(tmp);
    }

    for (int i = b.size() - 1; 0 < i; i--) {
      if (b[i] != ">") {
        a.push_back(b[i]);
        if (b[i - 1] == ">") {
          i -= 2;
          a.push_back(">>");
        }
      } else
        a.push_back(b[i]);
    }
    a.push_back(b[0]);
    reverse(a.begin(), a.end());
    rec(0, a.size());
    cout << a[a.size() - 1] << endl;
  }
  return 0;
}