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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

string str;
const string invalid = "", undefined = "undefined";
string number_memo[51][51], expr_memo[51][51];

string number(const int begin, const int end) {
  string &res = number_memo[begin][end];
  if (res != undefined)
    return res;

  if (str[begin] == '0') {
    if (end - begin == 1)
      return res = "0";
    else
      return res = invalid;
  }
  res = invalid;
  int pos = begin;
  while (pos != end and (isdigit(str[pos]) or str[pos] == '?')) {
    res += str[pos] == '?' ? '9' : str[pos];
    ++pos;
  }
  if (pos != end)
    res = invalid;
  return res;
}

string expr(const int begin, const int end) {
  string &res = expr_memo[begin][end];
  if (res != undefined)
    return res;

  res = number(begin, end);
  if (end - begin < 6)
    return res;
  const char initial = str[begin];
  if ((initial != '?' and initial != 'L' and initial != 'R') or
      (str[begin + 1] != '?' and str[begin + 1] != '(') or
      (str[end - 1] != '?' and str[end - 1] != ')'))
    return res;
  rep(i, begin + 3, end - 2) {
    if (str[i] != '?' and str[i] != ',')
      continue;
    const string tmp1 = expr(begin + 2, i), tmp2 = expr(i + 1, end - 1);
    if (tmp1 == invalid or tmp2 == invalid)
      continue;
    if (initial == 'L' or initial == '?') {
      if (tmp1.size() > res.size())
        res = tmp1;
      else if (tmp1.size() == res.size())
        res = max(res, tmp1);
    }
    if (initial == 'R' or initial == '?') {
      if (tmp2.size() > res.size())
        res = tmp2;
      else if (tmp2.size() == res.size())
        res = max(res, tmp2);
    }
  }
  return res;
}

void solve() {
  fill_n((string *)number_memo, 51 * 51, undefined);
  fill_n((string *)expr_memo, 51 * 51, undefined);
  cin >> str;
  const string ans = expr(0, str.size());
  if (ans == invalid)
    cout << "invalid" << endl;
  else
    cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

string str;
const string invalid = "";
string number_memo[51][51], expr_memo[51][51];

string number(const int begin, const int end) {
  string &res = number_memo[begin][end];
  // if(res!="undefined") return res;

  if (str[begin] == '0') {
    if (end - begin == 1)
      return res = "0";
    else
      return res = invalid;
  }
  res = invalid;
  int pos = begin;
  while (pos != end and (isdigit(str[pos]) or str[pos] == '?')) {
    res += str[pos] == '?' ? '9' : str[pos];
    ++pos;
  }
  if (pos != end)
    res = invalid;
  return res;
}

string expr(const int begin, const int end) {
  string &res = expr_memo[begin][end];
  // if(res!="undefined") return res;

  res = number(begin, end);
  if (end - begin < 6)
    return res;
  const char initial = str[begin];
  if ((initial != '?' and initial != 'L' and initial != 'R') or
      (str[begin + 1] != '?' and str[begin + 1] != '(') or
      (str[end - 1] != '?' and str[end - 1] != ')'))
    return res;
  rep(i, begin + 3, end - 2) {
    if (str[i] != '?' and str[i] != ',')
      continue;
    const string tmp1 = expr(begin + 2, i), tmp2 = expr(i + 1, end - 1);
    if (tmp1 == invalid or tmp2 == invalid)
      continue;
    if (initial == 'L') {
      if (tmp1.size() > res.size())
        res = tmp1;
      else if (tmp1.size() == res.size())
        res = max(res, tmp1);
    }
    if (initial == 'R') {
      if (tmp2.size() > res.size())
        res = tmp2;
      else if (tmp2.size() == res.size())
        res = max(res, tmp2);
    }
    if (initial == '?') {
      if (tmp1.size() > res.size())
        res = tmp1;
      else if (tmp1.size() == res.size())
        res = max(res, tmp1);
      if (tmp2.size() > res.size())
        res = tmp2;
      else if (tmp2.size() == res.size())
        res = max(res, tmp2);
    }
  }
  return res;
}

void solve() {
  fill_n((string *)number_memo, 51 * 51, "undefined");
  fill_n((string *)expr_memo, 51 * 51, "undefined");
  cin >> str;
  const string ans = expr(0, str.size());
  if (ans == invalid)
    cout << "invalid" << endl;
  else
    cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cout << fixed << setprecision(8);
  solve();
  return 0;
}