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

#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

#ifdef DEBUG
#define dump(s) cerr << #s << " = " << s << endl
#else
#define dump(...)
#endif

char s[5000];
int len;

int length(int const k, int const l, int const r) {
  int cur = 0, res = 0, p = l;
  while (s[p] && p < r) {
    if (isalpha(s[p])) {
      while (s[p] && p < r && isalpha(s[p])) {
        // printf(" %c\\n", s[p]);
        if (cur == k) {
          throw s[p];
        }
        res++;
        p++;
        cur++;
      }
    } else if (isdigit(s[p])) {
      int times = 0;
      while (s[p] && p < r && isdigit(s[p])) {
        times = times * 10 + s[p] - '0';
        p++;
      }
      int d = 0, i = p;
      p++;
      assert(s[i] == '(');
      while (1) {
        if (s[i] == '(')
          d++;
        else if (s[i] == ')')
          d--;
        if (d == 0)
          break;
        i++;
      }
      for (int j = 0; j < times; j++) {
        int d = length(k - cur, p, i);
        res += d;
        cur += d;
      }
      p = i + 1;
    }
  }
  return res;
}

int main() {
  static char t[5000];
  int k;
  while (cin >> t >> k && t[0] != '0') {
    int tlen = strlen(t);
    len = 0;
    for (int i = 0; i < tlen; i++) {
      if (i > 0 && isdigit(t[i - 1]) && isalpha(t[i])) {
        s[len++] = '(';
        s[len++] = t[i];
        s[len++] = ')';
      } else {
        s[len++] = t[i];
      }
    }
    s[len] = 0;
    // cout << s << endl;
    try {
      length(k, 0, len);
      puts("0");
    } catch (char e) {
      cout << e << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

#ifdef DEBUG
#define dump(s) cerr << #s << " = " << s << endl
#else
#define dump(...)
#endif

char s[500];
char *p;
int k;
int len;

int solve(int k, int l, int r) {
  int cur = k;
  dump(k);
  dump(l);
  dump(r);
  int res = 0;
  while (s[l] && l < r) {
    while (isalpha(s[l])) {
      if (cur == 0) {
        dump(k);
        dump(s[l]);
        throw s[l];
      }
      res++;
      l++;
      cur--;
    }
    int times = 0;
    while (isdigit(s[l])) {
      times = times * 10 + s[l] - '0';
      l++;
    }
    dump(l);
    dump(times);
    if (s[l] == '(') {
      int d = 0;
      int i = l;
      while (1) {
        if (s[i] == '(')
          d++;
        else if (s[i] == ')')
          d--;
        if (d == 0)
          break;
        i++;
      }
      int rangelen = solve(k, l + 1, i);
      for (int j = 0; j < times; j++) {
        res += solve(k, l + 1, i);
        k -= rangelen;
      }
      l = i + 1;
    } else if (isalpha(s[l])) {
      for (int j = 0; j < times; j++) {
        res += solve(k, l, l + 1);
        k -= 1;
      }
      l = l + 1;
    }
  }
  return res;
}

int main() {
  static char t[500];
  while (cin >> t >> k && t[0] != '0') {
    int tlen = strlen(t);
    len = 0;
    for (int i = 0; i < tlen; i++) {
      if (i > 0 && isdigit(t[i - 1]) && isalpha(t[i])) {
        s[len++] = '(';
        s[len++] = t[i];
        s[len++] = ')';
      } else {
        s[len++] = t[i];
      }
    }
    s[len] = 0;
    try {
      solve(k, 0, len);
      puts("0");
    } catch (char e) {
      cout << e << endl;
    }
  }
}