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

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string s;
ll a, t;
char out;
int cnt = 0;
string f(int a) {
  string w = "";
  while (t < s.size()) {
    if (out != '0')
      return "";
    if (isalpha(s[t])) {
      w += s[t];
      t++;
      cnt++;
      if (a < w.size()) {
        if (out == '0')
          out = w[a];
        return "";
      }
    } else if (s[t] == ')') {
      return w;
    } else {
      int co = 0;
      while (isdigit(s[t])) {
        co *= 10;
        co += s[t] - '0';
        t++;
      }
      string tmp;
      if (isalpha(s[t])) {
        tmp = s[t];
      } else {
        t++;
        tmp = f(a - w.size());
      }
      //			cout<<co<<" "<<tmp.size()<<endl;
      //			cout<<w<<endl;
      if (a < w.size() + tmp.size() * co) {
        if (a < w.size()) {
          if (out == '0')
            out = w[a];
        } else if (out == '0')
          out = tmp[(a - w.size()) % tmp.size()];
        return "";
      }
      rep(i, co) w += tmp;
      cnt += tmp.size() * (co - 1);
      t++;
    }
  }
  return w;
}
int main() {
  while (cin >> s >> a, s != "0") {
    out = '0';
    t = cnt = 0;
    f(a);
    cout << out << endl;
  }
}


Error: MLE

Fixed submission:

// g++ -std=c++11 a.cpp
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define FOR(i, a) for (auto i : a)
#define pb push_back
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
// #define int ll
typedef ll Def;
typedef pair<Def, Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def, pii> pip;
typedef vector<pip> vip;
#define mt make_tuple
typedef tuple<int, int, int, int> tp;
typedef vector<tp> vt;
template <typename A, typename B> bool cmin(A &a, const B &b) {
  return a > b ? (a = b, true) : false;
}
template <typename A, typename B> bool cmax(A &a, const B &b) {
  return a < b ? (a = b, true) : false;
}
// template<class C>constexpr int size(const C &c){return (int)c.size();}
// template<class T,size_t N> constexpr int size(const T
// (&xs)[N])noexcept{return (int)N;}
const double PI = acos(-1);
const double EPS = 1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9 + 10;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
string s;
ll a, t;
char out;
string f(int a) {
  string w = "";
  while (t < s.size()) {
    if (out != '0')
      return "";
    if (isalpha(s[t])) {
      w += s[t];
      t++;
      if (a < w.size()) {
        out = w[a];
        return "";
      }
    } else if (s[t] == ')') {
      return w;
    } else {
      int co = 0;
      while (isdigit(s[t])) {
        co *= 10;
        co += s[t] - '0';
        t++;
      }
      string tmp;
      if (isalpha(s[t])) {
        tmp = s[t];
      } else {
        t++;
        tmp = f(a);
      }
      rep(i, co) w += tmp;
      t++;
      if (a < w.size()) {
        out = w[a];
        return "";
      }
    }
  }
  return w;
}
int main() {
  while (cin >> s >> a, s != "0") {
    out = '0';
    t = 0;
    f(a);
    cout << out << endl;
  }
}