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

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

long long int t;
const long long int MAX_T = 1e8;
long long int read(int &i, string &s);
long long int counta(int &i, string &s);
char solve(int &i, string &s) {
  int prei = i;
  long long int ret;
  while (ret = read(i, s)) {
    prei = i;
    if (ret == -1) {
      t--;
      if (t == 0)
        return s[i - 1];
    } else {
      long long int val = counta(i, s);
      if (val * ret >= t) {
        t %= val;
        if (t == 0)
          t = val;
        i = prei;
        return solve(i, s);
      } else {
        t -= val * ret;
      }
    }
  }
  return '0';
}

long long int counta(int &i, string &s) {
  long long int ret;
  long long int res = 0;
  while (ret = read(i, s)) {
    if (ret == -1) {
      res++;
    } else {
      res += counta(i, s) * ret;
      res = min(res, MAX_T);
    }
  }
  return min(MAX_T, res);
}

long long int read(int &i, string &s) {
  if (i >= s.size() || s[i] == ')') {
    i++;
    return 0;
  }
  if (isalpha(s[i])) {
    i++;
    return -1;
  }

  long long int prei = i;
  for (; i < s.size() && isdigit(s[i]); i++)
    ;
  long long int res = (i - prei > 8 ? MAX_T : stoi(s.substr(prei, i - prei)));
  i++;
  return res;
}

int main() {
  string s;
  while (cin >> s >> t, s != "0") {
    int i = 0;
    t++;
    string s2 = "";
    s2 += s[0];
    for (int i = 1; i < s.size(); i++) {
      if (isdigit(s[i - 1]) && !isdigit(s[i]) && s[i] != '(') {
        s2 += '(';
        s2 += s[i];
        s2 += ')';
      } else {
        s2 += s[i];
      }
    }
    s = s2;

    cout << solve(i, s) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

long long int t;
const long long int MAX_T = 1e8;
long long int read(int &i, string &s);
long long int counta(int &i, string &s);
char solve(int &i, string &s) {
  int prei = i;
  long long int ret;
  while (ret = read(i, s)) {
    prei = i;
    if (ret == -1) {
      t--;
      if (t == 0)
        return s[i - 1];
    } else {
      long long int val = counta(i, s);
      if (val * ret >= t) {
        t %= val;
        if (t == 0)
          t = val;
        i = prei;
        return solve(i, s);
      } else {
        t -= val * ret;
      }
    }
  }
  return '0';
}

long long int counta(int &i, string &s) {
  long long int ret;
  long long int res = 0;
  while (ret = read(i, s)) {
    if (ret == -1) {
      res++;
    } else {
      res += counta(i, s) * ret;
      res = min(res, MAX_T);
    }
  }
  return min(MAX_T, res);
}

long long int read(int &i, string &s) {
  if (i >= s.size() || s[i] == ')') {
    i++;
    return 0;
  }
  if (isalpha(s[i])) {
    i++;
    return -1;
  }
  if (!isdigit(s[i])) {
    for (;;)
      ;
  }
  long long int prei = i;
  for (; i < s.size() && isdigit(s[i]); i++)
    ;
  long long int res = (i - prei > 8 ? MAX_T : stoi(s.substr(prei, i - prei)));
  if (s[i] == '(')
    i++;
  return res;
}

int main() {
  string s;
  while (cin >> s >> t, s != "0") {
    int i = 0;
    t++;
    string s2 = "";
    s2 += s[0];
    for (int i = 1; i < s.size(); i++) {
      if (isdigit(s[i - 1]) && s[i] != '(') {
        s2 += '(';
        s2 += s[i];
        s2 += ')';
      } else {
        s2 += s[i];
      }
    }
    s = s2;

    cout << solve(i, s) << endl;
  }
  return 0;
}