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

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int32_t, int32_t>;

// std::cout << std::setprecision(20) << 1.1 << endl;

/*
int32_t CountD(const string &T) {
  const int32_t L = static_cast<int32_t>(T.length());
  int32_t max_count = 0;
  int32_t c_count = 0;
  for (int32_t i = 0; i < L; ++i) {
    if (T.c_str()[i] == 'P') {
    } else {
      ++c_count;
    }
    if (c_count > max_count) {
      max_count = c_count;
    }
  }
  return max_count;
}

int32_t CountPD(const string &T) {
  const int32_t L = static_cast<int32_t>(T.length());
  int32_t count = 0;
  int32_t i = 0;
  while (i < L - 1) {
    if (T.c_str()[i] == 'P' && T.c_str()[i + 1] == 'D') {
      ++count;
      i += 2;
    } else {
      ++i;
    }
  }
  return count;
}

pair<int32_t, string> Dfs(const int32_t index, const string T, const
vector<int32_t> &positions) { if (index ==
static_cast<int32_t>(positions.size())) { return pair<int32_t, string>(CountD(T)
+ CountPD(T), T); } else { const int32_t position = positions[index]; if
(position == 0) { return Dfs(index + 1, string("D") + T.substr(position + 1,
T.length()), positions); } else if (T.c_str()[position - 1] == 'P') { return
Dfs(index + 1, T.substr(0, position) + string("D") + T.substr(position + 1,
T.length()), positions); } else if (T.c_str()[position - 1] == 'D' && position <
T.length()-1 && T.c_str()[position + 1] == 'D') { return Dfs(index + 1,
T.substr(0, position) + string("D") + T.substr(position + 1, T.length()),
positions); } else if (T.c_str()[position - 1] == 'D' && position < T.length()-1
&& T.c_str()[position + 1] == 'P') { return Dfs(index + 1, T.substr(0, position)
+ string("D") + T.substr(position + 1, T.length()), positions); } else { const
pair<int32_t, string> a = Dfs(index + 1, T.substr(0, position) + string("D") +
T.substr(position + 1, T.length()), positions); const pair<int32_t, string> b =
          Dfs(index + 1, T.substr(0, position) + string("P") + T.substr(position
+ 1, T.length()), positions); if (a.first > b.first) { return a; } else { return
b;
      }
    }
  }
}*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string T;
  cin >> T;
  const int32_t L = static_cast<int32_t>(T.length());
  vector<int32_t> positions;
  string result = "";
  for (int32_t i = 0; i < L; ++i) {
    if (T.c_str()[i] == '?') {
      positions.push_back(i);
      result += "D";
    } else {
      result += T.substr(i, 1);
    }
  }
  // const pair<int32_t, string> result = Dfs(0, T, positions);
  // cout << result.second << endl;
  cout << result << endl;
  return 0;
}


Error: TLE

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using Pair = pair<int32_t, int32_t>;

// std::cout << std::setprecision(20) << 1.1 << endl;

int32_t CountD(const string &T) {
  const int32_t L = static_cast<int32_t>(T.length());
  int32_t max_count = 0;
  int32_t c_count = 0;
  for (int32_t i = 0; i < L; ++i) {
    if (T.c_str()[i] == 'P') {
    } else {
      ++c_count;
    }
    if (c_count > max_count) {
      max_count = c_count;
    }
  }
  return max_count;
}

int32_t CountPD(const string &T) {
  const int32_t L = static_cast<int32_t>(T.length());
  int32_t count = 0;
  int32_t i = 0;
  while (i < L - 1) {
    if (T.c_str()[i] == 'P' && T.c_str()[i + 1] == 'D') {
      ++count;
      i += 2;
    } else {
      ++i;
    }
  }
  return count;
}

pair<int32_t, string> Dfs(const int32_t index, const string T,
                          const vector<int32_t> &positions) {
  if (index == static_cast<int32_t>(positions.size())) {
    return pair<int32_t, string>(CountD(T) + CountPD(T), T);
  } else {
    const int32_t position = positions[index];
    const pair<int32_t, string> a = Dfs(index + 1,
                                        T.substr(0, position) + string("D") +
                                            T.substr(position + 1, T.length()),
                                        positions);
    const pair<int32_t, string> b = Dfs(index + 1,
                                        T.substr(0, position) + string("P") +
                                            T.substr(position + 1, T.length()),
                                        positions);
    if (a.first > b.first) {
      return a;
    } else {
      return b;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string T;
  cin >> T;
  const int32_t L = static_cast<int32_t>(T.length());
  vector<int32_t> positions;
  for (int32_t i = 0; i < L; ++i) {
    if (T.c_str()[i] == '?') {
      positions.push_back(i);
    }
  }
  const pair<int32_t, string> result = Dfs(0, T, positions);
  cout << result.second << endl;
  return 0;
}