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

#include <cstdlib>
#include <iostream>
#include <stack>
#define DEBUG false
using namespace std;

struct P {
  unsigned int i; // JèÔµêÌæªÌindex
  int n;          // JèÔµñ
};

bool isDigit(char c) {
  if ('0' <= c && c <= '9')
    return true;
  return false;
}

/* ið®©µÄ¢«A¶ð©½ñ¾¯nð¸ç·
 */
char parse(string s, unsigned int n) {
  const char *c = s.c_str();
  unsigned int i = 0;
  stack<P> st; // '('ÌÊuðL^
  P p;
  if (DEBUG)
    cout << "parse : " << s << " len=" << s.length() << endl;

  while (i < s.length()) {
    if (false && DEBUG)
      cout << "while : i=" << i << " n=" << n << " s[i]=" << s[i]
           << " stack:" << st.size() << " p.n=" << p.n << endl;
    if (isDigit(s[i])) {
      unsigned int num = atoi(c + i);
      if (DEBUG)
        cout << "num=" << num << endl;
      while (isDigit(s[i]))
        ++i;
      if (s[i] == '(') {
        i++; // '(' ÌªðCNg·é
        p.i = i;
        p.n = num;
        st.push(p); // JèÔµÌÅÌ¶ðÇÁ
      } else {
        --num;
        //	cout << "n="<<n<<" num="<<num<<endl;
        if (n < num)
          return s[i];
        else
          n -= num;
      }
    } else if (s[i] == ')') {
      p = st.top();
      st.pop();
      if (--p.n != 0) {
        st.push(p);
        i = p.i;
      } else
        ++i;
    } else {
      if (n == 0)
        return s[i];
      --n;
      ++i;
    }
  }
  return '0';
}

int main() {
  string s;
  unsigned int n;
  while (cin >> s >> n && (s != "0" || n)) {
    if (DEBUG) {
      cout << "\\nn=" << n << " in : " << s << endl;
    }

    if (DEBUG)
      cout << "answer=";
    cout << parse(s, n) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
#define DEBUG false
using namespace std;

unsigned int n; // oÍ·é¶

string tab = "";
string tabN = "  ";

string parse(string s) {
  string r = "", num, tmp;
  if (DEBUG)
    tab += tabN;

  for (unsigned int i = 0; i < s.length(); i++) {
    if (i > n)
      return s.substr(0, i);

    if ('0' <= s[i] && s[i] <= '9') {
      num = "";
      unsigned int j = i;
      for (j = i; j < s.length(); j++)
        if ('0' <= s[j] && s[j] <= '9')
          num = num + s[j];
        else
          break;
      if (s[j] != '(') {
        tmp = s[j];
      } else {
        int a = 0; // Ê"("Ì - ")"Ì
        tmp = "";
        while (j < s.length()) {
          if (s[j] == '(')
            ++a;
          else if (s[j] == ')')
            --a;
          tmp = tmp + s[j++];
          if (a == 0)
            break;
        }
        tmp = tmp.substr(1, tmp.length() - 2);
      }
      // ±Ì_Å num(tmp) ÌÅãÌ) ÌÊuªj
      j++;

      if (DEBUG)
        cout << tab << "num=" << num << " tmp=" << tmp << endl;

      tmp = parse(tmp);
      if (i + num.length() * tmp.length() > n) {
        n -= i;
        n = n % tmp.length(); // tmp Ìn ¶Úª¦
        return tmp;
      }

      string rp = "";
      int rpN = atoi(num.c_str());
      for (int k = 0; k < rpN; k++) {
        rp = rp + tmp;
        if (i + rp.length() > n)
          return s.substr(0, i) + rp;
      }

      string bef = s.substr(0, i);
      string back = "";
      if (j < s.length())
        back = s.substr(j - 1);
      s = bef + rp + back;
      if (bef.length() > n)
        return s;

      if (DEBUG) {
        cout << tab << "bef =" << bef << endl;
        cout << tab << "rp =" << rp << endl;
        cout << tab << "back =" << back << endl;
        cout << tab << "s =" << s << endl;
      }
    } else {
      r = r + s[i];
    }
    if (r.length() > n)
      break;
  }
  if (DEBUG)
    tab = tab.substr(tabN.length());

  return r;
}

int main() {
  string s, ans;

  while (true) {
    cin >> s >> n;
    if (s == "0" && !n)
      break;

    if (DEBUG) {
      cout << "in : " << s << endl;
      cout << "num=" << n << endl << endl;
    }
    ans = parse(s);
    if (DEBUG)
      cout << "return : " << ans << endl;

    if (n < ans.length())
      cout << ans[n] << endl;
    else
      cout << "0" << endl;
  }

  return 0;
}