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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template <typename T> using uset = unordered_set<T>;
template <typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template <typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return (a > b && (a = b, true));
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return (a < b && (a = b, true));
}
template <typename T> bool isin(T y, T x, T h, T w) {
  return (0 <= y && 0 <= x && y < h && x < w);
}
template <typename T> bool isin1(T y, T x, T h, T w) {
  return (0 < y && 0 < x && y <= h && x <= w);
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << v[0];
  for (int i = 1; i < v.size(); i++)
    os << " " << v[i];
  return (os);
}

template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &u : v)
    is >> u;
  return (is);
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return (is >> p.first >> p.second);
}

void expr(string &S, int &i);

string T;
int N;

int number(string &S, int &i) {
  int val = 0;
  while (isdigit(S[i])) {
    val *= 10;
    val += S[i] - '0';
    i++;
  }
  return (val);
}

string String(string &S, int &i) {
  string s;
  while (T.size() <= N && i < S.size() && isupper(S[i])) {
    s += S[i];
    T += S[i];
    i++;
  }
  return (s);
}

void par(string &S, int &i) {
  int val = number(S, i);
  // cout << "number : " << val << endl;
  string s;
  if (S[i] == '(') {
    int plen = T.size();
    i++;
    expr(S, i);
    if (T.size() > N)
      return;
    // cout << val << " : " << plen << " " << T.size() << endl;
    for (int j = plen; j < T.size(); j++) {
      s += T[j];
    }
    i++;
  } else {
    s += String(S, i);
  }
  val--;
  for (int j = 0; j < val; j++) {
    T += s;
    if (T.size() > N)
      return;
  }
}

void expr(string &S, int &i) {
  while (i < S.size() && isupper(S[i]) || isdigit(S[i])) {
    if (isupper(S[i])) {
      String(S, i);
      // cout << i << " " << S.size() << " " << T.size() << endl;
      if (T.size() > N)
        return;
    } else {
      par(S, i);
      if (T.size() > N)
        return;
    }
  }
  return;
}

bool solve() {
  string S;
  cin >> S >> N;
  if (S == "0" && N == 0)
    return (false);
  T = "";
  int i = 0;
  expr(S, i);
  // cout << T.size() << endl;
  if (T.size() <= N) {
    cout << 0 << endl;
  } else {
    cout << T[N] << endl;
  }
  return (true);
}

void Main() {
  while (solve())
    ;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
  Main();
  return (0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 1000100
using namespace std;
typedef pair<int, int> P;
string s[2];
int n, c, f;

P stoi(int x) {
  int r = 0, idx = x;
  while ('0' <= s[c][idx] && s[c][idx] <= '9')
    r = r * 10 + s[c][idx] - '0', idx++;
  return P(r, idx);
}

int main() {
  while (1) {
    cin >> s[0] >> n;
    if (s[0] == "0" && n == 0)
      break;
    s[1].clear();
    c = 0;
    f = 1;

    int flg2 = 0;
    while (f) {
      f = 0;

      for (int i = 0; i < (int)s[c].size(); i++) {
        int e = i;
        while (s[c][e] && !('0' <= s[c][e] && s[c][e] <= '9'))
          e++;
        if (e != i) {
          s[!c] += s[c].substr(i, e - i);
          i = e;
        }

        if ('0' <= s[c][i] && s[c][i] <= '9') {
          if (!f && n < i) {
            c = !c;
            break;
          }
          f = 1;
          P r = stoi(i);
          int dig = r.first;
          int idx = r.second;
          string pls;
          if (s[c][idx] != '(')
            pls += s[c][idx];
          else {
            int cls = 1;
            int start = idx;
            while (!flg2) {
              idx++;
              if (s[c][idx] == '(')
                cls++;
              if (s[c][idx] == ')')
                cls--;
              if (!cls)
                break;
            }
            if (flg2)
              idx = s[c].size();
            if (idx == s[c].size())
              flg2 = 1;
            pls = s[c].substr(start + 1, idx - start - 1);
            i = idx;
          }
          for (int j = 0; j < dig && s[!c].size() + pls.size() < N; j++)
            s[!c] += pls;
        }
      }
      s[c].clear();
      c = !c;
    }
    if (s[c].size() <= n)
      cout << 0 << endl;
    else
      cout << s[c][n] << endl;
  }
  return 0;
}