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

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;

const int INFTY = 1 << 29;

int dfs(vvi &a, int ci, int cj, int depth) {
  if (depth == 10)
    return INFTY;

  int h = a.size(), w = a[0].size();
  int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1};
  int res = INFTY;
  rep(k, 4) {
    for (int i = ci + di[k], j = cj + dj[k];; i += di[k], j += dj[k]) {
      if (i < 0 || h <= i || j < 0 || w <= j)
        break;
      if (a[i][j] == 1) {
        if (abs(i - ci) + abs(j - cj) > 1) {
          a[i][j] = 0;
          res = min(res, dfs(a, i - di[k], j - dj[k], depth + 1));
          a[i][j] = 1;
        }
        break;
      }
      if (a[i][j] == 3)
        return depth + 1;
    }
  }
  return res;
}

int main() {
  for (int w, h; cin >> w >> h;) {
    vvi a(h, vi(w));
    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) if (a[i][j] == 2) {
      a[i][j] = 0;
      int res = dfs(a, i, j, 0);
      cout << (res == INFTY ? -1 : res) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;

const int INFTY = 1 << 29;

int dfs(vvi &a, int ci, int cj) {
  int h = a.size(), w = a[0].size();
  int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1};
  int res = INFTY;
  rep(k, 4) {
    for (int i = ci + di[k], j = cj + dj[k];; i += di[k], j += dj[k]) {
      if (i < 0 || h <= i || j < 0 || w <= j)
        break;
      if (a[i][j] == 1) {
        if (abs(i - ci) + abs(j - cj) > 1) {
          a[i][j] = 0;
          res = min(res, 1 + dfs(a, i - di[k], j - dj[k]));
          a[i][j] = 1;
        }
        break;
      }
      if (a[i][j] == 3)
        return 1;
    }
  }
  return res;
}

int main() {
  for (int w, h; cin >> w >> h;) {
    vvi a(h, vi(w));
    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) if (a[i][j] == 2) {
      a[i][j] = 0;
      int res = dfs(a, i, j);
      cout << (res > 10 ? -1 : res) << endl;
    }
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int INF = 1 << 28;
int res;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int solve(int count, vector<vector<int>> v, int x, int y) {
  if (count > res || count >= 10)
    return INF;
  if (v[x][y] == 3)
    return count;

  int ret = INF;
  for (int i = 0; i < 4; i++) {
    vector<vector<int>> tmp(v.begin(), v.end());
    int nx = x + dx[i];
    int ny = y + dy[i];
    bool flag = false;
    while (0 <= nx && nx < tmp.size() && 0 <= ny && ny < tmp[0].size() &&
           (tmp[nx][ny] == 0 || tmp[nx][ny] == 3)) {
      flag = true;
      if (tmp[nx][ny] == 3) {
        return count + 1;
      }
      nx += dx[i];
      ny += dy[i];
    }
    if (0 <= nx && nx < tmp.size() && 0 <= ny && ny < tmp[0].size() && flag) {
      tmp[nx][ny] = 0;
      ret = min(solve(count + 1, tmp, nx - dx[i], ny - dy[i]), ret);
    }
  }

  return ret;
}

int main() {
  int n, m;
  while (cin >> m >> n, n || m) {
    res = INF;
    vector<vector<int>> v(n, vector<int>(m));
    int sx, sy;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int num;
        cin >> num;
        if (num == 2)
          sx = i, sy = j, num = 0;
        v[i][j] = num;
      }
    }
    int res = solve(0, v, sx, sy);

    cout << (res == INF ? -1 : res) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

const int INF = 1 << 28;
int res;

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int solve(int count, vector<vector<int>> v, int x, int y) {
  if (count > res || count > 10)
    return INF;
  if (v[x][y] == 3)
    return count;

  int ret = INF;
  for (int i = 0; i < 4; i++) {
    vector<vector<int>> tmp(v.begin(), v.end());
    int nx = x + dx[i];
    int ny = y + dy[i];
    bool flag = false;
    while (0 <= nx && nx < tmp.size() && 0 <= ny && ny < tmp[0].size() &&
           (tmp[nx][ny] == 0 || tmp[nx][ny] == 3)) {
      flag = true;
      if (tmp[nx][ny] == 3) {
        return solve(count + 1, tmp, nx, ny);
      }
      nx += dx[i];
      ny += dy[i];
    }
    if (0 <= nx && nx < tmp.size() && 0 <= ny && ny < tmp[0].size() && flag) {
      tmp[nx][ny] = 0;
      ret = min(solve(count + 1, tmp, nx - dx[i], ny - dy[i]), ret);
    }
  }

  return ret;
}

int main() {
  int n, m;
  while (cin >> m >> n, n || m) {
    res = INF;
    vector<vector<int>> v(n, vector<int>(m));
    int sx, sy;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int num;
        cin >> num;
        if (num == 2)
          sx = i, sy = j, num = 0;
        v[i][j] = num;
      }
    }
    int res = solve(0, v, sx, sy);

    cout << (res == INF ? -1 : res) << endl;
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define foreach(itr, a)                                                        \\
  for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

int w, h, ans = 0;
int sx, sy;
int f[21][21];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); }

void dfs(int x, int y, int depth) {
  if (depth + 1 >= ans)
    return;

  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];

    if (!ng(nx, ny) and f[ny][nx] != 1) {
      while (!ng(nx, ny)) {
        // goal
        if (f[ny][nx] == 3) {
          ans = min(ans, depth + 1);
          return;
        }

        if (f[ny][nx] == 1) {
          f[ny][nx] = 0;
          dfs(nx - dx[i], ny - dy[i], depth + 1);
          f[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
  }

  return;
}

int main() {
  while (cin >> w >> h and w) {
    rep(i, h) rep(j, w) {
      cin >> f[i][j];
      if (f[i][j] == 2) {
        sx = j;
        sy = i;
        f[i][j] = 0;
      }
    }

    ans = 11;
    dfs(sx, sy, 0);
    if (ans <= 10)
      cout << ans << "\\n";
    else
      cout << -1 << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define foreach(itr, a)                                                        \\
  for (__typeof((a).begin()) itr = (a).begin(); itr != (a).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

int w, h, ans = 0;
int sx, sy;
int f[21][21];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool ng(int x, int y) { return (x < 0 or y < 0 or x >= w or y >= h); }

void dfs(int x, int y, int depth) {
  if (depth + 1 >= ans)
    return;

  rep(i, 4) {
    int nx = x + dx[i], ny = y + dy[i];

    if (!ng(nx, ny) and f[ny][nx] != 1) {
      while (!ng(nx, ny)) {
        // goal
        if (f[ny][nx] == 3) {
          ans = min(ans, depth + 1);
          return;
        }

        if (f[ny][nx] == 1) {
          f[ny][nx] = 0;
          dfs(nx - dx[i], ny - dy[i], depth + 1);
          f[ny][nx] = 1;
          break;
        }
        nx += dx[i];
        ny += dy[i];
      }
    }
  }

  return;
}

int main() {
  while (cin >> w >> h and w) {
    rep(i, h) rep(j, w) {
      cin >> f[i][j];
      if (f[i][j] == 2) {
        sx = j;
        sy = i;
        f[i][j] = 0;
      }
    }

    ans = 114514;
    dfs(sx, sy, 0);
    if (ans <= 10)
      cout << ans << "\\n";
    else
      cout << -1 << "\\n";
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int h, w;
int map[20][20];
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int ans;
int out(int y, int x) { return y < 0 || h <= y || x < 0 || w <= x; }
void search(int depth, int y, int x) {
  int i, ny, nx;
  if (ans <= depth)
    return;
  for (i = 0; i < 4; i++) {
    ny = y + dy[i];
    nx = x + dx[i];
    if (out(ny, nx) || map[ny][nx] == 1)
      continue;
    do {
      if (map[ny][nx] == 3) {
        ans = depth;
        return;
      }
      ny += dy[i];
      nx += dx[i];
    } while (!out(ny, nx) && map[ny][nx] != 1);
    if (!out(ny, nx) && map[ny][nx] == 1) {
      map[ny][nx] = 0;
      search(depth + 1, ny - dy[i], nx - dx[i]);
      map[ny][nx] = 1;
    }
  }
}
int main() {
  int i, j;
  int sy, sx;
  while (scanf("%d%d", &w, &h), w | h) {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        scanf("%d", &map[i][j]);
        if (map[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }
    }
    ans = 11;
    search(1, sy, sx);
    printf("%d\\n", ans == 11 ? -1 : ans);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

int h, w;
int map[20][20];
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};

int ans;
int out(int y, int x) { return y < 0 || h <= y || x < 0 || w <= x; }
void search(int depth, int y, int x) {
  int i, ny, nx;
  if (ans <= depth)
    return;
  for (i = 0; i < 4; i++) {
    ny = y + dy[i];
    nx = x + dx[i];
    if (out(ny, nx) || map[ny][nx] == 1)
      continue;
    while (!out(ny, nx) && map[ny][nx] != 1) {
      if (map[ny][nx] == 3) {
        ans = depth;
        return;
      }
      ny += dy[i];
      nx += dx[i];
    }
    if (map[ny][nx] == 1) {
      map[ny][nx] = 0;
      search(depth + 1, ny - dy[i], nx - dx[i]);
      map[ny][nx] = 1;
    }
  }
}
int main() {
  int i, j;
  int sy, sx;
  while (scanf("%d%d", &w, &h), w | h) {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        scanf("%d", &map[i][j]);
        if (map[i][j] == 2) {
          sy = i;
          sx = j;
        }
      }
    }
    ans = 11;
    search(1, sy, sx);
    printf("%d\\n", ans == 11 ? -1 : ans);
  }
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int sx, sy;
int ans = 100;

bool outofrange(int y, int x) {
  if (y < 0 || y >= h || x < 0 || x >= w) {
    return true;
  } else {
    return false;
  }
}

void dfs(vector<vector<int>> grid, int sy, int sx, int depth) {
  if (depth + 1 >= ans || depth + 1 > 10) {
    return;
  }

  for (int i = 0; i < 4; i++) {
    int ny = sy + dy[i];
    int nx = sx + dx[i];

    if (outofrange(ny, nx) == true || grid[ny][nx] == 1) {
      continue;
    }

    while (outofrange(ny, nx) == false) {
      if (grid[ny][nx] == 3) {
        ans = min(ans, depth + 1);
        return;
      }

      if (grid[ny][nx] == 1) {
        grid[ny][nx] = 0;
        dfs(grid, ny - dy[i], nx - dx[i], depth + 1);
        grid[ny][nx] = 1;
        break;
      }

      ny += dy[i];
      nx += dx[i];
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0) {
      break;
    }

    ans = 100;
    vector<vector<int>> grid(h, vector<int>(w));
    pair<int, int> start, goal;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> grid[y][x];

        if (grid[y][x] == 2) {
          sy = y;
          sx = x;
        }
      }
    }

    dfs(grid, sy, sx, 0);

    cout << (ans <= 10 ? ans : -1) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int w, h;
int sx, sy;
int ans = 100;

bool outofrange(int y, int x) {
  if (y < 0 || y >= h || x < 0 || x >= w) {
    return true;
  } else {
    return false;
  }
}

void dfs(vector<vector<int>> grid, int sy, int sx, int depth) {
  if (depth + 1 >= ans) {
    return;
  }

  for (int i = 0; i < 4; i++) {
    int ny = sy + dy[i];
    int nx = sx + dx[i];

    if (outofrange(ny, nx) == true || grid[ny][nx] == 1) {
      continue;
    }

    while (outofrange(ny, nx) == false) {
      if (grid[ny][nx] == 3) {
        ans = min(ans, depth + 1);
        return;
      }

      if (grid[ny][nx] == 1) {
        grid[ny][nx] = 0;
        dfs(grid, ny - dy[i], nx - dx[i], depth + 1);
        grid[ny][nx] = 1;
        break;
      }

      ny += dy[i];
      nx += dx[i];
    }
  }
}

int main() {
  while (true) {
    cin >> w >> h;

    if (w == 0 && h == 0) {
      break;
    }

    ans = 100;
    vector<vector<int>> grid(h, vector<int>(w));
    pair<int, int> start, goal;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> grid[y][x];

        if (grid[y][x] == 2) {
          sy = y;
          sx = x;
        }
      }
    }

    dfs(grid, sy, sx, 0);

    cout << (ans <= 10 ? ans : -1) << endl;
  }

  return 0;
}
Problem p00725 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
bool map[21][21];
int h, w, z, xs, ys, xg, yg;
int ans;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void DFS(int y, int x, int len) {
  if (len >= 12)
    return;
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (map[yy][xx] == true) {
      while (true) {
        if (xx <= -1 || yy <= -1 || xx >= w || yy >= h)
          break;
        if (xx == xg && yy == yg) {
          ans = min(ans, len);
          break;
        }
        if (map[yy][xx] == false) {
          map[yy][xx] = true;
          DFS(yy - dy[i], xx - dx[i], len + 1);
          map[yy][xx] = false;
          break;
        }
        xx += dx[i], yy += dy[i];
      }
    }
  }
}
int main() {
  while (true) {
    cin >> w >> h;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> z;
        if (z == 0)
          map[i][j] = true;
        if (z == 1)
          map[i][j] = false;
        if (z == 2) {
          xs = j, ys = i, map[i][j] = true;
        }
        if (z == 3) {
          xg = j, yg = i;
          map[i][j] = true;
        }
      }
    }
    ans = 11;
    DFS(ys, xs, 1);
    if (ans > 10)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
bool map[20][20];
int h, w, z, xs, ys, xg, yg;
int ans;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void DFS(int y, int x, int len) {
  for (int i = 0; i < 4; i++) {
    int xx = x + dx[i], yy = y + dy[i];
    if (map[yy][xx] == true) {
      while (true) {
        if (xx <= -1 || yy <= -1 || xx >= w || yy >= h)
          break;
        if (xx == xg && yy == yg) {
          ans = min(ans, len);
          break;
        }
        if (map[yy][xx] == false) {
          map[yy][xx] = true;
          DFS(yy - dy[i], xx - dx[i], len + 1);
          map[yy][xx] = false;
          break;
        }
        xx += dx[i], yy += dy[i];
      }
    }
  }
}
int main() {
  while (true) {
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> z;
        if (z == 0)
          map[i][j] = true;
        if (z == 1)
          map[i][j] = false;
        if (z == 2) {
          xs = j, ys = i, map[i][j] = true;
        }
        if (z == 3) {
          xg = j, yg = i;
          map[i][j] = true;
        }
      }
    }
    ans = 11;
    DFS(ys, xs, 1);
    if (ans > 10)
      cout << "-1" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cctype>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string s;
int n, p[300], v[300], w[300];
int main() {
  while (cin >> s >> n, s != "0") {
    string t = string({s[0]});
    for (int i = 1; i < s.size(); i++) {
      if (isdigit(s[i - 1]) && isalpha(s[i]))
        t += "(" + string({s[i]}) + ")";
      else
        t += s[i];
    }
    int pos1 = 0, w1 = 0;
    s = "";
    memset(p, 0, sizeof(p));
    memset(w, 0, sizeof(w));
    memset(v, 0, sizeof(v));
    for (int i = 0; i < t.size(); i++) {
      if (isdigit(t[i]))
        w1 = 10 * w1 + t[i] - 48;
      else {
        if (t[i] == '(')
          v[pos1] = w[pos1] = w1, w1 = 0;
        s += t[i];
        pos1++;
      }
    }
    stack<int> st;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '(')
        st.push(i);
      if (s[i] == ')')
        p[i] = st.top(), p[st.top()] = i, st.pop();
    }
    int pos = 0;
    for (int i = 0; i <= n; i++) {
      while (pos < s.size()) {
        if (s[pos] == '(') {
          if (w[pos])
            w[pos]--, pos++;
          else
            w[pos] = v[pos], pos = p[pos] + 1;
        } else if (s[pos] == ')')
          pos = p[pos];
        else {
          if (i != n)
            pos++;
          break;
        }
      }
    }
    cout << (pos < s.size() ? s[pos] : '0') << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cctype>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
string s;
int n, p[200], v[200], w[200];
int main() {
  while (cin >> s >> n, s != "0") {
    string t = string({s[0]});
    for (int i = 1; i < s.size(); i++) {
      if (isdigit(s[i - 1]) && isalpha(s[i]))
        t += "(" + string({s[i]}) + ")";
      else
        t += s[i];
    }
    int pos1 = 0, w1 = 0;
    s = "";
    for (int i = 0; i < t.size(); i++) {
      if (isdigit(t[i]))
        w1 = 10 * w1 + t[i] - 48;
      else {
        if (t[i] == '(')
          v[pos1] = w[pos1] = w1, w1 = 0;
        s += t[i];
        pos1++;
      }
    }
    stack<int> st;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '(')
        st.push(i);
      if (s[i] == ')')
        p[i] = st.top(), p[st.top()] = i, st.pop();
    }
    int pos = 0;
    for (int i = 0; i <= n; i++) {
      if (pos == s.size()) {
        pos = -1, printf("0\\n");
        break;
      }
      while (true) {
        if (s[pos] == '(') {
          if (w[pos])
            w[pos]--, pos++;
          else
            w[pos] = v[pos], pos = p[pos] + 1;
        } else if (s[pos] == ')')
          pos = p[pos];
        else {
          pos++;
          break;
        }
      }
    }
    if (pos != -1)
      cout << s[pos - 1] << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

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

using namespace std;

const int imax = 1234567;

int n;
string S;
size_t cur = 0;

int digit();
int number();
string expression();

int digit() { return S[cur++] - '0'; }

int number() {
  int res = digit();
  while (isdigit(S[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

string expression(void) {
  string rec = "";
  while (cur < n) {
    // cout << cur << endl;
    if (rec.size() >= imax)
      return rec;

    if (isupper(S[cur])) {

      rec += S[cur++];

    } else if (isdigit(S[cur])) {

      int r = number();
      string c = "";

      if (S[cur] == '(') {
        cur++;
        c = expression();
      } else
        c = S[cur++];

      rep(i, r) {
        if (rec.size() >= imax)
          return rec;
        rec += c;
      }
    } else if (S[cur] == ')') {
      cur++;
      return rec;
    }
  }
  return rec;
}

int main(void) {
  int i;
  while (cin >> S >> i) {
    if (S == "0")
      break;
    n = S.size();
    cur = 0;
    string res = expression();
    if (i >= res.size())
      cout << 0 << endl;
    else
      cout << res[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

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

using namespace std;

const int imax = 1234567;

int n;
string S;
size_t cur = 0;

int digit();
int number();
string expression();

int digit() { return S[cur++] - '0'; }

int number() {
  int res = digit();
  while (isdigit(S[cur])) {
    res = res * 10 + digit();
  }
  return res;
}

string expression(void) {
  string rec = "";
  while (cur < n) {
    // cout << cur << endl;
    if (rec.size() >= imax)
      return rec;

    if (isupper(S[cur])) {

      rec += S[cur++];

    } else if (isdigit(S[cur])) {

      int r = number();
      string c = "";

      if (S[cur] == '(') {
        cur++;
        c = expression();
      } else
        c = S[cur++];

      rep(i, r) {
        if (rec.size() >= imax)
          return rec;
        rec += c;
      }
    } else if (S[cur] == ')') {
      cur++;
      return rec;
    }
  }
  return rec;
}

int main(void) {
  int i;
  while (cin >> S >> i) {
    if (S == "0")
      break;
    n = S.size();
    cur = 0;
    string res = expression();
    if (res[i] == '\\0')
      cout << 0 << endl;
    else
      cout << res[i] << endl;
  }
  return 0;
}
Problem p00726 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s, an;
int n;
void dfs(int x) {
  int t = 0;
  for (int i = x, c = 0; i < s.size() && 0 <= c; i++) {
    if (s[i] == '(') {
      while (!c && t > 0 && t-- && an.size() <= n)
        dfs(i + 1);
      c++;
    }
    if (s[i] == ')')
      c--;
    if (!c && '0' <= s[i] && s[i] <= '9')
      t *= 10, t += s[i] - '0';
    if (!c && 'A' <= s[i] && s[i] <= 'Z')
      an += s[i];
  }
}

int main() {
  while (cin >> s >> n && s[0] != '0') {
    an.clear();
    for (int i = 0; i < s.size(); i++)
      if ('0' <= s[i] && s[i] <= '9' && 'A' <= s[i + 1] && s[i + 1] <= 'Z')
        s = s.substr(0, i + 1) + '(' + s[i + 1] + ')' +
            s.substr(i + 2, s.size());
    dfs(0);
    if (an.size() > n)
      cout << an[n] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;
int n;
string dfs(int x) {
  int a = 1, t = 0;
  string str, res;
  for (int i = x; 0 < a; i++) {
    if ('0' <= s[i] && s[i] <= '9')
      t *= 10, t += s[i] - '0';
    else if (s[i] == '(' && a == 1) {
      res = dfs(i + 1);
      t = max(1, t);
      while (t--)
        str += res;
      a++;
    } else if (s[i] == ')')
      a--, t = 0;
    else if (a == 1)
      str += s[i];
  }
  return str;
}
int main() {
  while (cin >> s >> n) {
    s += ")";
    for (int i = 0; i < s.size(); i++)
      if ('0' <= s[i] && s[i] <= '9' && s[i + 1] != '(')
        s = s.substr(0, i + 1) + '(' + s[i + 1] + ')' +
            s.substr(i + 2, s.size());
    string ans = dfs(0);
    if (ans.size() > n)
      cout << ans[n] << endl;
    else
      cout << 0 << endl;
  }
  return 0;
}