Problem p02664 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  string s;
  cin >> s;
  int n = sz(s);
  const int inf = 1e9 + 228;
  vector<vi> dp(2, vi(n, -inf));
  vector<vi> pre(2, vi(n, -1));
  const string kek = "PD";
  map<char, int> re;
  re['P'] = 0;
  re['D'] = 1;
  if (s[0] == '?') {
    dp[0][0] = 0;
    dp[1][0] = 1;
  } else {
    dp[re[s[0]]][0] = re[s[0]];
  }
  for (int i = 1; i < n; i++) {
    for (int cur = 0; cur < 2; cur++) {
      if (s[i] != '?' && cur != re[s[i]]) {
        continue;
      }
      for (int p = 0; p < 2; p++) {
        if (dp[p][i - 1] == -inf) {
          continue;
        }
        int stonks = dp[p][i - 1] + cur + (!p) * cur;
        if (stonks > dp[cur][i]) {
          dp[cur][i] = stonks;
          pre[cur][i] = p;
        }
      }
    }
  }
  string t;
  int pos = n - 1;
  int cur = 0;
  if (dp[1][n - 1] > dp[0][n - 1]) {
    cur = 1;
  }
  while (pos >= 0) {
    t.pb(kek[cur]);
    cur = pre[cur][pos];
    pos--;
  }
  reverse(all(t));
  cout << t;
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;

int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0);
  string s;
  cin >> s;
  int n = sz(s);
  const int inf = 1e9 + 228;
  vector<vi> dp(2, vi(n, -inf));
  vector<vi> pre(2, vi(n, -1));
  const string kek = "PD";
  map<char, int> re;
  re['P'] = 0;
  re['D'] = 1;
  if (s[0] == '?') {
    dp[0][0] = 0;
    dp[0][1] = 1;
  } else {
    dp[0][re[s[0]]] = re[s[0]];
  }
  for (int i = 1; i < n; i++) {
    for (int cur = 0; cur < 2; cur++) {
      if (s[i] != '?' && cur != re[s[i]]) {
        continue;
      }
      for (int p = 0; p < 2; p++) {
        if (dp[p][i - 1] == -inf) {
          continue;
        }
        int stonks = dp[p][i - 1] + cur + (!p) * cur;
        if (stonks > dp[cur][i]) {
          dp[cur][i] = stonks;
          pre[cur][i] = p;
        }
      }
    }
  }
  string t;
  int pos = n - 1;
  int cur = 0;
  if (dp[1][n - 1] > dp[0][n - 1]) {
    cur = 1;
  }
  while (pos >= 0) {
    t.pb(kek[cur]);
    cur = pre[cur][pos];
    pos--;
  }
  reverse(all(t));
  cout << t;
}