alexjercan/bugnet huggingface dataset:
Problem p00726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

const int inf = 2000000;

string s;
int k;
int l[100];

int lexpr(int &i);
void expr(int &i, int k, char &c);

int lfactor(int &i) {
  int p = 0;
  while (isdigit(s[i])) {
    p = p * 10 + s[i++] - '0';
  }
  if (s[i] != '(') {
    ++i;
    return p;
  }
  ++i;
  ll m = lexpr(i);
  ++i;
  return (int)min(m * p, (ll)inf);
}

int lterm(int &i) {
  if (isdigit(s[i])) {
    return lfactor(i);
  }
  i++;
  return 1;
}

int lexpr(int &i) {
  int p = i;
  int m = 0;
  while (s[i] && s[i] != ')') {
    m += lterm(i);
    if (m > inf) {
      m = inf;
      break;
    }
  }
  return l[p] = m;
}

int factor(int &i, int k, char &c) {
  int p = 0;
  while (isdigit(s[i])) {
    p = p * 10 + s[i++] - '0';
  }
  if (s[i] != '(') {
    if (k < p) {
      c = s[i];
    }
    ++i;
    return p;
  }
  ++i;
  ll m = l[i];
  if (k < m * p) {
    expr(i, k % m, c);
    ++i;
    return k + 1;
  }
  expr(i, m + 1, c);
  ++i;
  return m * p;
}

int term(int &i, int k, char &c) {
  if (isdigit(s[i])) {
    return factor(i, k, c);
  }
  if (k == 0) {
    c = s[i];
  }
  ++i;
  return 1;
}

void expr(int &i, int k, char &c) {
  while (s[i] && s[i] != ')') {
    int m = term(i, k, c);
    if (k < m) {
      break;
    }
    k -= m;
  }
}

int main() {
  while (1) {
    cin >> s >> k;
    if (s == "0" && k == 0) {
      break;
    }
    int i = 0;
    lexpr(i);
    i = 0;
    char c = '0';
    expr(i, k, c);
    cout << c << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

typedef long long ll;

string s;
int k;

int term(int &i, int k, char &c);
int factor(int &i, int k, char &c);

int expr(int &i, int k, char &c) {
  int l = 0;
  while (s[i] && s[i] != ')') {
    int t = term(i, k, c);
    k -= t;
    l += t;
    if (0 > k) {
      break;
    }
  }
  return l;
}

int term(int &i, int k, char &c) {
  if ('0' <= s[i] && s[i] <= '9') {
    return factor(i, k, c);
  }

  if (k == 0) {
    c = s[i];
  }

  ++i;
  return 1;
}

int factor(int &i, int k, char &c) {
  int n = 0;
  while ('0' <= s[i] && s[i] <= '9') {
    n = n * 10 + (s[i++] - '0');
  }

  if (s[i] != '(') {
    if (k < n) {
      c = s[i];
    }
    ++i;
    return n;
  }

  ++i;

  int j = i;
  int l = expr(i, k, c);
  ++i;

  if (ll(l) * n <= k) {
    return l * n;
  } else {
    i = j;
    expr(i, k % l, c);
    ++i;
    return k + 1;
  }
}

int main() {
  while (1) {
    cin >> s >> k;
    if (s == "0" && k == 0) {
      break;
    }
    int i = 0;
    char c = '0';
    expr(i, k, c);
    cout << c << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int solve(stringstream &ss, int remains, char &result) {
  int length = 0;
  while (ss.tellg() < ss.str().size() && remains >= 0) {
    if (isdigit(ss.peek())) {
      int loop;
      ss >> loop;
      char c = ss.get();
      if (c == '(') {
        streampos pos = ss.tellg();
        int len = solve(ss, remains, result);
        if (len < 0) {
          return -1;
        }
        if (len * loop > remains) {
          ss.seekg(pos, ios_base::beg);
          solve(ss, remains % len, result);
          return -1;
        }
        remains -= len * loop;
        length += len * loop;
      } else {
        if (loop > remains) {
          result = c;
          return -1;
        }
        remains -= loop;
        length += loop;
      }
    } else {
      char c = ss.get();
      if (c == ')') {
        break;
      } else if (remains-- == 0) {
        result = c;
        return -1;
      }
      ++length;
    }
  }
  return length;
}

int main() {
  while (true) {
    string s;
    int pos;
    cin >> s >> pos;
    if (s == "0" && pos == 0) {
      break;
    }
    char answer = '0';
    stringstream ss(s);
    solve(ss, pos, answer);
    cout << answer << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int solve(stringstream &ss, int remains, char &result) {
  int length = 0;
  while (ss.tellg() < ss.str().size() && remains >= 0) {
    if (isdigit(ss.peek())) {
      int loop;
      ss >> loop;
      char c = ss.get();
      if (c == '(') {
        streampos pos = ss.tellg();
        int len = solve(ss, remains, result);
        if (len * loop > remains) {
          ss.seekg(pos, ios_base::beg);
          solve(ss, remains % len, result);
        }
        remains -= len * loop;
        length += len * loop;
      } else {
        if (loop > remains) {
          result = c;
        }
        remains -= loop;
        length += loop;
      }
    } else {
      char c = ss.get();
      if (c == ')') {
        break;
      } else if (remains-- == 0) {
        result = c;
      }
      ++length;
    }
  }
  return length;
}

int main() {
  while (true) {
    string s;
    int pos;
    cin >> s >> pos;
    if (s == "0" && pos == 0) {
      break;
    }
    char answer = '0';
    stringstream ss(s);
    solve(ss, pos, answer);
    cout << answer << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string parse(string V, int w) {
  while (V.size() >= 2 && V[0] == '(' && V[V.size() - 1] == ')')
    V = V.substr(1, V.size() - 2);
  if (V.size() == 1)
    return V;
  int depth = 0;
  string E = "", F = "";
  vector<pair<int, string>> v;
  for (int i = 0; i < V.size(); i++) {
    if (V[i] == '(')
      depth++;
    if (V[i] == ')')
      depth--;
    F += V[i];
    if (depth == 0 && ((V[i] >= 'A' && V[i] <= 'Z') || V[i] == ')')) {
      string Y = "";
      int c = 0;
      while (F[c] >= '0' && F[c] <= '9') {
        Y += F[c];
        c++;
      }
      int D = 1;
      if (Y.size() >= 1)
        D = stoi(Y);
      v.push_back(make_pair(D, F.substr(c, F.size() - c)));
      F = "";
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (E.size() >= w)
      break;
    string Z = parse(v[i].second, w);
    for (int j = 0; j < v[i].first; j++) {
      if (E.size() >= w)
        break;
      E += Z;
    }
  }
  return E.substr(0, min((int)E.size(), w));
}
int main() {
  while (true) {
    string S;
    int T;
    cin >> S >> T;
    if (S == "0" && T == 0)
      break;
    string U = parse(S, T + 1);
    if (U.size() <= T) {
      cout << "0" << endl;
    } else
      cout << U[T] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string parse(string V, int w) {
  while (V.size() >= 2 && V[0] == '(' && V[V.size() - 1] == ')')
    V = V.substr(1, V.size() - 2);
  if (V.size() == 1)
    return V;
  int depth = 0;
  string E = "", F = "";
  vector<pair<int, string>> v;
  for (int i = 0; i < V.size(); i++) {
    if (V[i] == '(')
      depth++;
    if (V[i] == ')')
      depth--;
    F += V[i];
    if (depth == 0 && ((V[i] >= 'A' && V[i] <= 'Z') || V[i] == ')')) {
      string Y = "";
      int c = 0;
      while (F[c] >= '0' && F[c] <= '9') {
        Y += F[c];
        c++;
      }
      int D = 1;
      if (Y.size() >= 1)
        D = stoi(Y);
      v.push_back(make_pair(D, F.substr(c, F.size() - c)));
      F = "";
    }
  }
  for (int i = 0; i < v.size(); i++) {
    if (E.size() >= w)
      break;
    for (int j = 0; j < v[i].first; j++) {
      if (E.size() >= w)
        break;
      E += parse(v[i].second, w - E.size());
    }
  }
  return E.substr(0, min((int)E.size(), w));
}
int main() {
  while (true) {
    string S;
    int T;
    cin >> S >> T;
    if (S == "0" && T == 0)
      break;
    string U = parse(S, T + 1);
    if (U.size() <= T) {
      cout << "0" << endl;
    } else
      cout << U[T] << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

char solve(string str, int &index, size_t &pos) {
  // cout << "solve : " << str << endl;
  int cnt = 1, len = str.length();
  if (str[0] == '(') {
    str = str.substr(1);
    cnt = -1;
  }
  while (!str.empty() && cnt--) {
    if (str[0] >= '0' && str[0] <= '9') {
      size_t i, p;
      int num = stoi(str, &i);
      string s = str.substr(i);
      rep(k, num) {
        char res = solve(s, index, p);
        if (res != '0')
          return res;
      }
      // cout << p << endl;
      str = s.substr(p);
    } else if (str[0] == ')') {
      str = str.substr(1);
      break;
    } else {
      int i = 0;
      while (str[i] < '0' || str[i] > '9') {
        if (str[i] == ')')
          break;
        if (index-- == 0)
          return str[i];
        i++;
      }
      str = str.substr(i);
    }
  }
  pos = len - str.length();
  return '0';
}

int main() {
  string s;
  int i;
  while (cin >> s >> i, s != "0") {
    rep(j, s.length()) {
      if (s[j] == '(' && (j == 0 || s[j - 1] < '0' || s[j - 1] > '9')) {
        s = s.substr(0, j) + "1" + s.substr(j);
        j++;
      }
    }
    string ss = "1(" + s + ")";
    size_t p;
    cout << solve(ss, i, p) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

char solve(string str, int &index, size_t &pos) {
  // cout << "solve : " << str << endl;
  int cnt = 1, len = str.length();
  if (str[0] == '(') {
    str = str.substr(1);
    cnt = -1;
  }
  while (!str.empty() && cnt--) {
    if (str[0] >= '0' && str[0] <= '9') {
      size_t i, p;
      int num = stoi(str, &i);
      string s = str.substr(i);
      rep(k, num) {
        char res = solve(s, index, p);
        if (res != '0')
          return res;
      }
      str = s.substr(p);
    } else if (str[0] == ')') {
      break;
    } else {
      int i = 0;
      while (str[i] < '0' || str[i] > '9') {
        if (str[i] == ')')
          break;
        if (index-- == 0)
          return str[i];
        i++;
      }
      str = str.substr(i);
    }
  }
  pos = len - str.length() + 1;
  return '0';
}

int main() {
  string s;
  int i;
  while (cin >> s >> i, s != "0") {
    string ss = "1(" + s + ")";
    size_t p;
    cout << solve(ss, i, p) << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int index;

int cur;
bool endf;

string rec() {
  string res;

  while (cur < s.size() && !endf) {
    while ('A' <= s[cur] && s[cur] <= 'Z') {
      res += s[cur];
      cur++;
    }

    if ('0' <= s[cur] && s[cur] <= '9') {
      int n = s[cur++] - '0';
      while ('0' <= s[cur] && s[cur] <= '9') {
        n = 10 * n + s[cur++] - '0';
      }

      string pattern;
      if (s[cur] == '(') {
        cur++;
        pattern = rec();
      } else {
        pattern = s[cur++];
      }

      for (int i = 0; i < n; ++i) {
        res += pattern;
        if (res.size() > index) {
          endf = true;
          break;
        }
      }
    }

    if (s[cur] == ')') {
      cur++;
      return res;
    }
  }

  return res;
}

int main() {
  while (true) {
    cin >> s >> index;
    if (s == "0" && index == 0)
      break;

    cur = 0;
    endf = false;
    string res = rec();
    // cout << res << endl;
    if (res.size() <= index) {
      cout << 0 << endl;
    } else {
      cout << res[index] << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

string s;
int index;

int cur;
bool endf;

string rec() {
  string res;

  while (cur < s.size() && !endf) {
    while ('A' <= s[cur] && s[cur] <= 'Z') {
      res += s[cur];
      cur++;
    }

    if ('0' <= s[cur] && s[cur] <= '9') {
      int n = s[cur++] - '0';
      while ('0' <= s[cur] && s[cur] <= '9') {
        n = 10 * n + s[cur++] - '0';
      }

      string pattern;
      if (s[cur] == '(') {
        cur++;
        pattern = rec();
      } else {
        pattern = s[cur++];
      }

      if (pattern.size() > index) {
        res += pattern;
        endf = true;
        break;
      }

      for (int i = 0; i < n; ++i) {
        res += pattern;
      }
    }

    if (s[cur] == ')') {
      cur++;
      return res;
    }
  }

  return res;
}

int main() {
  while (true) {
    cin >> s >> index;
    if (s == "0" && index == 0)
      break;

    cur = 0;
    endf = false;
    string res = rec();
    // cout << res << endl;
    if (res.size() <= index) {
      cout << 0 << endl;
    } else {
      cout << res[index] << endl;
    }
  }
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s;
const int MAX = 1e6 + 1;

typedef long long Int;

Int cnt(int &p);

Int get_num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += (s[p] - '0');
    p++;
  }
  return res;
}

Int cnt_large(int &p) {
  Int res = 0;
  while (s[p] != ')') {
    Int val = cnt(p);
    res += val;
    if (res > MAX)
      res = MAX;
  }
  return res;
}

Int cnt(int &p) {
  if (isalpha(s[p])) {
    p++;
    return 1;
  }
  if (isdigit(s[p])) {
    Int num = get_num(p);
    if (isalpha(s[p])) {
      p++;
      return num;
    }
    assert(s[p] == '(');
    p++;
    Int val = cnt_large(p);
    assert(s[p] == ')');
    p++;
    if (val > MAX) {
      return MAX;
    }
    if (num * val > MAX)
      return MAX;
    return num * val;
  }
  return -1000000;
}

char target(int &p, Int x) {
  while (p < s.size()) {
    int prep = p;
    Int val = cnt(p);
    if (val >= x) {
      p = prep;
      if (isalpha(s[p])) {
        return s[p];
      }
      Int num = get_num(p);
      if (isalpha(s[p])) {
        return s[p];
      }
      assert(s[p] == '(');
      p++;
      int prep2 = p;
      Int val2 = cnt_large(p);
      p = prep2;
      return target(p, (x % val2 == 0) ? val2 : x % val2);
    }
    x -= val;
  }
  return '0';
}

int main() {
  int n;
  while (cin >> s >> n, s != "0") {

    int p = 0;
    cout << target(p, n + 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;
const int MAX = 1e6 + 1;

typedef long long Int;

Int cnt(int &p);

Int get_num(int &p) {
  int res = 0;
  while (isdigit(s[p])) {
    res *= 10;
    res += (s[p] - '0');
    p++;
  }
  return res;
}

Int cnt_large(int &p) {
  Int res = 0;
  while (s[p] != ')') {
    Int val = cnt(p);
    res += val;
    if (res > MAX)
      return MAX;
  }
  return res;
}

Int cnt(int &p) {
  if (isalpha(s[p])) {
    p++;
    return 1;
  }
  if (isdigit(s[p])) {
    Int num = get_num(p);
    if (isalpha(s[p])) {
      p++;
      return num;
    }
    assert(s[p] == '(');
    p++;
    Int val = cnt_large(p);
    assert(s[p] == ')');
    p++;
    if (val > MAX) {
      return MAX;
    }
    if (num * val > MAX)
      return MAX;
    return num * val;
  }
  return -1000000;
}

char target(int &p, Int x) {
  while (p < s.size()) {
    int prep = p;
    Int val = cnt(p);
    if (val >= x) {
      p = prep;
      if (isalpha(s[p])) {
        return s[p];
      }
      Int num = get_num(p);
      if (isalpha(s[p])) {
        return s[p];
      }
      assert(s[p] == '(');
      p++;
      int prep2 = p;
      Int val2 = cnt_large(p);
      p = prep2;
      return target(p, (x % val2 == 0) ? val2 : x % val2);
    }
    x -= val;
  }
  return '0';
}

int main() {
  int n;
  while (cin >> s >> n, s != "0") {

    int p = 0;
    cout << target(p, n + 1) << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

string ans, str;
int n, p;

string dfs() {
  int res = 0;
  string st = "";
  while (p < str.size()) {
    if (isdigit(str[p])) {
      res = 0;
      while (isdigit(str[p])) {
        res *= 10;
        res += str[p] - '0';
        p++;
      }
    } else if (isalpha(str[p])) {
      st = "";
      while (isalpha(str[p])) {
        st += str[p];
        p++;
      }
    } else if (str[p] == '(') {
      p++;
      string tmp = dfs();
      for (int i = 0; i < res; i++) {
        if (st.size() > n) {
          return st;
        }
        st += tmp;
      }
    } else if (str[p] == ')') {
      p++;
      return st;
    } else {
      break;
    }
  }
  return st;
}

string complement(string s) {
  int p = 0;
  string expanded_string = "";
  while (p < s.size()) {
    if (!isalpha(s[p]) && isalpha(s[p + 1])) {
      expanded_string += s[p];
      if (isdigit(s[p])) {
        expanded_string += "(";
      } else {
        expanded_string += "1(";
      }
      p++;
      while (isalpha(s[p])) {
        expanded_string += s[p++];
      }
      expanded_string += ')';
    } else {
      expanded_string += s[p++];
    }
  }
  return expanded_string;
}

int main() {
  while (true) {
    p = 0;
    cin >> str >> n;
    if (str == "0") {
      break;
    }
    str = complement(str);
    ans = dfs();
    if (ans.size() <= n) {
      cout << "0" << endl;
    } else {
      cout << ans[n] << endl;
    }
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;

string ans, str;
int p;

string dfs() {
  int res = 0;
  string st = "";
  while (p < str.size()) {
    if (isdigit(str[p])) {
      res = 0;
      while (isdigit(str[p])) {
        res *= 10;
        res += str[p] - '0';
        p++;
      }
    } else if (isalpha(str[p])) {
      st = "";
      while (isalpha(str[p])) {
        st += str[p];
        p++;
      }
    } else if (str[p] == '(') {
      p++;
      string tmp = dfs();
      for (int i = 0; i < res; i++) {
        st += tmp;
      }
    } else if (str[p] == ')') {
      p++;
      return st;
    }
  }
  return st;
}

string complement(string s) {
  int p = 0;
  string expanded_string = "";
  while (p < s.size()) {
    if (isdigit(s[p]) && isalpha(s[p + 1])) {
      expanded_string += s[p++];
      expanded_string += '(';
      while (isalpha(s[p])) {
        expanded_string += s[p++];
      }
      expanded_string += ')';
    } else {
      expanded_string += s[p++];
    }
  }
  return expanded_string;
}

int main() {
  int n;
  while (true) {
    p = 0;
    cin >> str >> n;
    if (str == "0") {
      break;
    }
    str = complement(str);
    ans = dfs();
    if (ans.size() <= n) {
      cout << "0" << endl;
    } else {
      cout << ans[n] << endl;
    }
  }

  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, char>;

string S;
int n, it;

int num() {
  int res = 0;
  while (it < n && isdigit(S[it])) {
    res = res * 10 + S[it] - '0';
    it++;
  }
  return res;
}

P parser(int t) {
  P res(0, '0');
  while (it < n && S[it] != ')') {
    if (isdigit(S[it])) {
      int nm = num();
      if (S[it] == '(') {
        it++;
        int tmp = it;
        auto p = parser(t);
        if (p.second != '0') {
          res.second = p.second;
          break;
        } else if (t < p.first * nm) {
          it = tmp;
          t %= p.first;
          res.second = parser(t).second;
          break;
        }
        t -= p.first * nm;
        res.first += p.first * nm;
        assert(S[it++] == ')');
      } else {
        if (t < nm) {
          res.second = S[it];
        }
        t -= nm;
        res.first += nm;
        it++;
      }
    } else {
      if (t == 0) {
        res.second = S[it];
      }
      t--;
      res.first++;
      it++;
    }
    if (t < 0) {
      break;
    }
  }
  return res;
}

int main() {
  int i;
  while (cin >> S >> i, S != "0" || i != 0) {
    n = S.size();
    it = 0;
    cout << parser(i).second << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, char>;

string S;
int n, it;

int num() {
  int res = 0;
  while (it < n && isdigit(S[it])) {
    res = res * 10 + S[it] - '0';
    it++;
  }
  return res;
}

P parser(int t) {
  P res(0, '0');
  while (it < n && S[it] != ')') {
    if (isdigit(S[it])) {
      int nm = num();
      if (S[it] == '(') {
        it++;
        int tmp = it;
        auto p = parser(t);
        if (p.second != '0') {
          res.second = p.second;
          break;
        } else if (t < p.first * nm) {
          it = tmp;
          t %= p.first;
          res.second = parser(t).second;
        }
        t -= p.first * nm;
        res.first += p.first * nm;
        assert(S[it++] == ')');
      } else {
        if (t < nm) {
          res.second = S[it];
        }
        t -= nm;
        res.first += nm;
        it++;
      }
    } else {
      if (t == 0) {
        res.second = S[it];
      }
      t--;
      res.first++;
      it++;
    }
    if (t < 0) {
      break;
    }
  }
  return res;
}

int main() {
  int i;
  while (cin >> S >> i, S != "0" || i != 0) {
    n = S.size();
    it = 0;
    cout << parser(i).second << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int N;
string dfs(string s) {
  string r;
  int i = 0;
  long long n = -1;
  for (; i < s.size();) {
    if ('0' <= s[i] && s[i] <= '9') {
      if (n < 0)
        n = s[i] - '0';
      else
        n = n * 10 + s[i] - '0';
      i++;
    } else if (s[i] == '(') {
      if (n < 0)
        n = 1;
      int cnt = 1, j = i + 1;
      for (; cnt;) {
        if (s[j] == '(')
          cnt++;
        else if (s[j] == ')')
          cnt--;
        j++;
      }
      string r0 = dfs(s.substr(i + 1, j - 1 - i - 1));
      for (int _ = 0; _ < n; _++) {
        r += r0;
        if (r.size() > N)
          return r;
      }
      n = -1;
      i = j;
    } else {
      if (n < 0)
        n = 1;
      r += string(n, s[i]);
      n = -1;
      i++;
    }
  }
  return r;
}

int main() {
  for (;;) {
    string s;
    cin >> s >> N;
    if (s == "0")
      break;
    string r = dfs(s);
    printf("%c\\n", r.size() > N ? r[N] : '0');
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int N;
string dfs(string s) {
  string r;
  int i = 0;
  long long n = -1;
  for (; i < s.size();) {
    if ('0' <= s[i] && s[i] <= '9') {
      if (n < 0)
        n = s[i] - '0';
      else
        n = n * 10 + s[i] - '0';
      i++;
    } else if (s[i] == '(') {
      if (n < 0)
        n = 1;
      int cnt = 1, j = i + 1;
      for (; cnt;) {
        if (s[j] == '(')
          cnt++;
        else if (s[j] == ')')
          cnt--;
        j++;
      }
      string r0 = dfs(s.substr(i + 1, j - 1 - i - 1));
      r += r0;
      if (r.size() > N)
        return r;
      for (int _ = 1; _ < n; _++)
        r += r0;
      n = -1;
      i = j;
    } else {
      if (n < 0)
        n = 1;
      r += string(n, s[i]);
      n = -1;
      i++;
    }
  }
  return r;
}

int main() {
  for (;;) {
    string s;
    cin >> s >> N;
    if (s == "0")
      break;
    string r = dfs(s);
    printf("%c\\n", r.size() > N ? r[N] : '0');
  }
}