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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 147483600

int k;

void solve(vector<char> &s, vector<int> &ans, string str) {
  int n = s.size();
  if (n == 0)
    return;
  for (auto c : str) {
    vector<vector<int>> dp(n, vector<int>(k + 1, -INF));
    dp[0][0] = (s[0] == c);
    dp[0][1] = (s[0] != c);
    repl(i, 1, n) {
      dp[i][0] = dp[i - 1][0] + (s[i] == c);
      repl(j, 1, k + 1) {
        if (j % 2 == 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + (s[i] == c);
        else
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + (s[i] != c);
      }
    }
    rep(i, k + 1) ans[i] = max(ans[i], 2 * dp[n - 1][i] - n);
  }
}

int main() {
  string s;
  cin >> s >> k;

  vector<char> x, y;
  for (auto c : s) {
    if (c == 'R' || c == 'L')
      x.pb(c);
    else
      y.pb(c);
  }

  vector<int> xx(k + 1, 0), yy(k + 1, 0);

  solve(x, xx, "RL");
  solve(y, yy, "UD");

  int ans = 0;
  rep(i, k + 1) rep(j, k + 1) if (i + j <= k) ans = max(ans, xx[i] + yy[j]);

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 147483600

int k;

void solve(vector<char> &s, vector<int> &ans, string str) {
  int n = s.size();
  for (auto c : str) {
    vector<vector<int>> dp(n, vector<int>(k + 1, -INF));
    dp[0][0] = (s[0] == c);
    dp[0][1] = (s[0] != c);
    repl(i, 1, n) {
      dp[i][0] = dp[i - 1][0] + (s[i] == c);
      repl(j, 1, k + 1) {
        if (j % 2 == 0)
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + (s[i] == c);
        else
          dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + (s[i] != c);
      }
    }
    rep(i, k + 1) ans[i] = max(ans[i], 2 * dp[n - 1][i] - n);
  }
}

int main() {
  string s;
  cin >> s >> k;

  vector<char> x, y;
  for (auto c : s) {
    if (c == 'R' || c == 'L')
      x.pb(c);
    else
      y.pb(c);
  }

  vector<int> xx(k + 1, 0), yy(k + 1, 0);

  solve(x, xx, "RL");
  solve(y, yy, "UD");

  int ans = 0;
  rep(i, k + 1) rep(j, k + 1) if (i + j <= k) ans = max(ans, xx[i] + yy[j]);

  cout << ans << endl;

  return 0;
}