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

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

// dp[a][b]ÉÍfrom a to
// bÌ¶ððÍµ½Æ«A½¶Ì¶ªoÄ­é©ðL^µÄ¨­
ll dp[101][101];

ll cp = -1;
ll idx;
char result = 0;
bool fin = false;

// ðÍµÈªçJEgµÄ¢­
int dfs(const string str, const int from, const int to) {
  string res;
  ll pos = 0;
  ll resCnt = 0;
  while (1) {
    ll initPos = pos;
    if (pos >= str.size())
      break;
    if (str[pos] >= '0' && str[pos] <= '9') {
      for (ll i = pos + 1;; i++) {
        if (!(str[i] >= '0' && str[i] <= '9')) {
          pos = i;
          break;
        }
      }
      ll cnt = atoi(str.substr(initPos, pos - initPos).c_str());
      if (str[pos] == '(') {
        stack<ll> s;
        s.push(0);
        ll start = pos;
        while (1) {
          pos++;
          if (str[pos] == '(')
            s.push(0);
          else if (str[pos] == ')')
            s.pop();
          if (s.size() == 0)
            break;
        }
        string subs = str.substr(start + 1, pos - start - 1);
        for (int i = 0; i < cnt; i++) {
          // if(dp[start+1][pos-1]!=-1){
          //	ll sum=cp+dp[start+1][pos-1];
          //	if(sum<idx){
          //		cp=sum;
          //		resCnt+=dp[start+1][pos-1];
          //		continue;
          //	}
          // }
          resCnt += dfs(subs, start + 1, pos - 1);
          if (result != 0 || fin)
            return 0;
        }
        pos++;
      }
      // ¶Ìê
      else {
        for (int i = 0; i < cnt; i++) {
          cp++;
          if (cp > idx) {
            fin = true;
            return 0;
          }
          if (cp == idx) {
            result = str[pos];
            return 0;
          }
        }
        resCnt += cnt;
        pos++;
      }
    } else {
      for (ll i = initPos;; i++) {
        if (i >= str.size()) {
          pos = i + 1;
          break;
        } else if (!(str[i] >= 'A' && str[i] <= 'Z')) {
          pos = i;
          break;
        } else {
          resCnt++;
          cp++;
          if (cp > idx) {
            fin = true;
            return 0;
          }
          if (cp == idx) {
            result = str[i];
            return 0;
          }
        }
      }
    }
  }
  dp[from][to] = resCnt;
  return resCnt;
}

int main() {
  string str;
  while (cin >> str >> idx && !(str == "0" && idx == 0)) {
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        dp[i][j] = -1;
    fin = false;
    cp = -1;
    result = 0;
    dfs(str, 0, str.size() - 1);
    if (result == 0)
      cout << 0 << endl;
    else
      cout << result << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

// dp[a][b]ÉÍfrom a to
// bÌ¶ððÍµ½Æ«A½¶Ì¶ªoÄ­é©ðL^µÄ¨­
ll dp[101][101];

ll cp = -1;
int idx;
char result = 0;
bool fin = false;

// ðÍµÈªçJEgµÄ¢­
int dfs(string str, int from, int to) {
  string res;
  ll pos = 0;
  ll resCnt = 0;
  while (1) {
    ll initPos = pos;
    if (pos >= str.size())
      break;
    if (str[pos] >= '0' && str[pos] <= '9') {
      for (ll i = pos + 1;; i++) {
        if (!(str[i] >= '0' && str[i] <= '9')) {
          pos = i;
          break;
        }
      }
      ll cnt = atoi(str.substr(initPos, pos - initPos).c_str());
      if (str[pos] == '(') {
        stack<ll> s;
        s.push(0);
        ll start = pos;
        while (1) {
          pos++;
          if (str[pos] == '(')
            s.push(0);
          else if (str[pos] == ')')
            s.pop();
          if (s.size() == 0)
            break;
        }
        string subs = str.substr(start + 1, pos - start - 1);
        for (int i = 0; i < cnt; i++) {
          // if(dp[start+1][pos-1]!=-1){
          //	ll sum=cp+dp[start+1][pos-1];
          //	if(sum<idx){
          //		cp=sum;
          //		if(cp>idx){
          //			fin=true;
          //			return 0;
          //		}
          //		resCnt+=dp[start+1][pos-1];
          //		continue;
          //	}
          // }
          resCnt += dfs(subs, start + 1, pos - 1);
          if (result != 0 || fin)
            return 0;
        }
        pos++;
      }
      // ¶Ìê
      else {
        for (int i = 0; i < cnt; i++) {
          cp++;
          if (cp == idx) {
            result = str[pos];
            return 0;
          }
        }
        resCnt += cnt;
        pos++;
      }
    } else {
      for (ll i = initPos;; i++) {
        if (i >= str.size()) {
          pos = i + 1;
          break;
        } else if (!(str[i] >= 'A' && str[i] <= 'Z')) {
          pos = i;
          break;
        } else {
          resCnt++;
          cp++;
          if (cp > idx) {
            fin = true;
            return 0;
          }
          if (cp == idx) {
            result = str[i];
            return 0;
          }
        }
      }
    }
  }
  dp[from][to] = resCnt;
  return resCnt;
}

int main() {
  string str;
  while (cin >> str >> idx && !(str == "0" && idx == 0)) {
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        dp[i][j] = -1;
    fin = false;
    cp = -1;
    result = 0;
    dfs(str, 0, str.size() - 1);
    if (result == 0)
      cout << 0 << endl;
    else
      cout << result << endl;
  }
  return 0;
}