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

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

string s;
int n;

int calc(int count, int idx) {
  if (count == -1)
    return -1;
  idx++; //(
  while (s[idx] != ')') {
    if (count == -1)
      return -1;
    if (isalpha(s[idx])) {
      if (count == n) {
        cout << s[idx] << endl;
        return -1;
      }
      count++;
      idx++;
    } else {
      int m = 0;
      while (isdigit(s[idx])) {
        m *= 10;
        m += s[idx] - '0';
        idx++;
      }
      rep(i, m) count = calc(count, idx);

      idx++;
      int b = 1;
      while (b != 0) {
        if (s[idx] == '(')
          b++;
        else if (s[idx] == ')')
          b--;
        idx++;
      }
    }
  }
  return count;
}

int main() {
  while (cin >> s >> n) {
    for (int i = 0; i < s.size(); i++) {
      if (isdigit(s[i]) && isalpha(s[i + 1]))
        s = s.substr(0, i + 1) + "(" + s.substr(i + 1, 1) + ")" +
            s.substr(i + 2);
    }

    int count = 0;

    if (s == "0" && n == 0)
      break;
    for (int i = 0; i < s.size();) {
      if (count == -1)
        break;
      if (isalpha(s[i])) {
        if (count == n) {
          cout << s[i] << endl;
          count = -1;
          break;
        }
        count++;
        i++;
      } else {
        int m = 0;
        while (isdigit(s[i])) {
          m *= 10;
          m += s[i] - '0';
          i++;
        }

        rep(j, m) count = calc(count, i);

        i++;
        int b = 1;
        while (b != 0) {
          if (s[i] == '(')
            b++;
          else if (s[i] == ')')
            b--;
          i++;
        }
      }
    }
    if (count != -1)
      cout << 0 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

const double eps = 1e-10;
const double pi = acos(-1.0);
const double inf = (int)1e8;

string s;
int n;
int calc(int count, int idx) {
  if (count == -1)
    return -1;
  idx++; //(
  while (s[idx] != ')') {
    if (count == n) {
      cout << s[idx] << endl;
      return -1;
    }

    if (isalpha(s[idx])) {
      count++;
      idx++;
    } else {
      int m = 0;
      while (isdigit(s[idx])) {
        m *= 10;
        m += s[idx] - '0';
        idx++;
      }
      rep(i, m) count = calc(count, idx);

      idx++;
      int b = 1;
      while (b != 0) {
        if (s[idx] == '(')
          b++;
        else if (s[idx] == ')')
          b--;
        idx++;
      }
    }
  }
  return count;
}

int main() {
  while (cin >> s >> n) {
    for (int i = 0; i < s.size(); i++) {
      if (isdigit(s[i]) && isalpha(s[i + 1]))
        s = s.substr(0, i + 1) + "(" + s.substr(i + 1, 1) + ")" +
            s.substr(i + 2);
    }

    int count = 0;

    if (s == "0" && n == 0)
      break;
    for (int i = 0; i < s.size();) {
      if (count == n) {
        cout << s[i] << endl;
        count = -1;
      }
      if (count == -1)
        break;

      if (isalpha(s[i])) {
        count++;
        i++;
      } else {
        int m = 0;
        while (isdigit(s[i])) {
          m *= 10;
          m += s[i] - '0';
          i++;
        }

        rep(j, m) count = calc(count, i);

        i++;
        int b = 1;
        while (b != 0) {
          if (s[i] == '(')
            b++;
          else if (s[i] == ')')
            b--;
          i++;
        }
      }
    }
    if (count != -1)
      cout << 0 << endl;
  }
}