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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
// I referred to other's code.
const int MAX_L = 10000001;
string s;
int needidx;
string expr(int &p);
int num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res = res * 10 + (s[p++] - '0');
  }
  return res;
}
string chara(int &p) {
  string res = "";
  while (isalpha(s[p])) {
    res += s[p++];
  }
  return res;
}
string term(int &p) {
  string res;
  if (isalpha(s[p])) {
    res = chara(p);
  } else {
    int n = num(p);
    string t;
    if (s[p] == '(') {
      p++;
      t = expr(p);
      p++;
    } else {
      t = chara(p);
    }
    REP(i, n) {
      if (res.size() > needidx)
        break;
      res += t;
    }
  }
  return res;
}
string expr(int &p) {
  string res = term(p);
  while (p < s.size() && s[p] != ')') {
    if (res.size() > needidx)
      break;
    res += term(p);
  }
  return res;
}

int main() {
  while (cin >> s >> needidx, s != "0") {
    int p = 0;
    string ans = expr(p);
    if (ans.size() > needidx) {
      cout << ans[needidx] << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
// I referred to other's code.
const int MAX_L = 10000001;
string s;
string expr(int &p);
int num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res = res * 10 + (s[p++] - '0');
  }
  return res;
}
string chara(int &p) {
  string res = "";
  while (isalpha(s[p])) {
    res += s[p++];
  }
  return res;
}
string term(int &p) {
  string res;
  if (isalpha(s[p])) {
    res = chara(p);
  } else {
    int n = num(p);
    string t;
    if (s[p] == '(') {
      p++;
      t = expr(p);
      p++;
    } else {
      t = chara(p);
    }
    REP(i, n) {
      if (res.size() > MAX_L)
        break;
      res += t;
    }
  }
  return res;
}
string expr(int &p) {
  string res = term(p);
  while (p < s.size() && s[p] != ')' && res.size() < MAX_L) {
    res += term(p);
  }
  return res;
}

int main() {
  int i;
  while (cin >> s >> i, s != "0") {
    int p = 0;
    string ans = expr(p);
    if (ans.size() > i) {
      cout << ans[i] << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}