alexjercan/bugnet huggingface dataset:
Problem p00524 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

struct st {
  int p, h;
  ll c;
};
bool operator<(st a, st b) { return a.c > b.c; }

int h[100000], y;
ll d[100000];
vector<P> E[100000];
int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);
  rep(i, n) scanf("%d", &h[i]);
  rep(i, m) {
    int a, b, t;
    scanf("%d%d%d", &a, &b, &t);
    a--;
    b--;
    if (t <= h[a])
      E[a].push_back(P(t, b));
    if (t <= h[b])
      E[b].push_back(P(t, a));
  }
  fill(d, d + n, LLONG_MAX);
  priority_queue<st> que;
  d[0] = 0;
  que.push({0, x, 0});
  while (!que.empty()) {
    st s = que.top();
    que.pop();
    if (d[s.p] != s.c)
      continue;
    for (P u : E[s.p]) {
      int t = 0, k = s.h - u.first;
      if (k < 0)
        t = 0 - k, k = 0;
      if (k > h[u.second])
        t = k - h[u.second], k = h[u.second];
      if (d[u.second] > s.c + u.first + t) {
        if (u.second == n - 1)
          y = k;
        d[u.second] = s.c + u.first + t;
        que.push({u.second, k, d[u.second]});
      }
    }
  }
  if (d[n - 1] == LLONG_MAX)
    puts("-1");
  else
    printf("%lld\\n", d[n - 1] + h[n - 1] - y);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

struct st {
  int p, h;
  ll c;
};
bool operator<(st a, st b) { return a.c > b.c; }

int h[100000];
map<int, ll> d[100000];
list<P> E[100000];
bool b[100000];
int main() {
  int n, m, x;
  scanf("%d%d%d", &n, &m, &x);
  rep(i, n) { scanf("%d", &h[i]); }
  rep(i, m) {
    int a, b, t;
    scanf("%d%d%d", &a, &b, &t);
    a--;
    b--;
    if (t <= h[a])
      E[a].push_back(P(t, b));
    if (t <= h[b])
      E[b].push_back(P(t, a));
  }
  priority_queue<st> que;
  d[0][x] = 0;
  que.push({0, x, 0});
  while (!que.empty()) {
    st s = que.top();
    que.pop();
    if (d[s.p][s.h] != s.c)
      continue;
    bool ok = true;
    for (P u : E[s.p]) {
      if (b[u.second])
        continue;
      int t = 0, k = s.h - u.first;
      if (k < 0)
        t = 0 - k, k = 0, ok = false;
      if (k > h[u.second])
        t = k - h[u.second], k = h[u.second], ok = false;
      if ((d[u.second].find(k) == d[u.second].end()) ||
          (d[u.second][k] > s.c + u.first + t)) {
        d[u.second][k] = s.c + u.first + t;
        que.push({u.second, k, d[u.second][k]});
      }
    }
    if (ok)
      b[s.p] = true;
  }
  ll Min = LLONG_MAX;
  for (auto p : d[n - 1]) {
    Min = min(Min, p.second + h[n - 1] - p.first);
  }
  if (Min == LLONG_MAX)
    puts("-1");
  else
    printf("%lld\\n", Min);
}
Problem p00524 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
  int to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

int N, M, X;
int H[100000];

vector<edge> G[100000];
int dist[100000];

signed main() {
  cin >> N >> M >> X;
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }

  fill_n(dist, N, INF);

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  dist[0] = 0;
  while (que.size()) {
    P p = que.top();
    que.pop();
    if (dist[p.second] < p.first)
      continue;
    int h = (p.first >= X ? 0 : X - p.first);

    for (int i = 0; i < G[p.second].size(); i++) {
      edge &e = G[p.second][i];
      if (H[p.second] < e.cost)
        continue;
      int cost;
      if (h - e.cost > H[e.to])
        cost = h - H[e.to];
      else if (h - e.cost >= 0)
        cost = e.cost;
      else
        cost = e.cost - (h - e.cost);
      if (dist[e.to] <= p.first + cost)
        continue;
      dist[e.to] = p.first + cost;
      que.push(P(dist[e.to], e.to));
    }
  }
  if (dist[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
         << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1001001001001001001LL;
typedef pair<int, int> P;
struct edge {
  int to, cost;
  edge(int a, int b) : to(a), cost(b) {}
};

int N, M, X;
int H[100000];

vector<edge> G[100000];
int dist[100000];

signed main() {
  cin >> N >> M >> X;
  for (int i = 0; i < N; i++)
    cin >> H[i];
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a].push_back(edge(b, c));
    G[b].push_back(edge(a, c));
  }

  fill_n(dist, N, INF);

  priority_queue<P> que;
  que.push(P(0, 0));
  dist[0] = 0;
  while (que.size()) {
    P p = que.top();
    que.pop();
    if (dist[p.second] < p.first)
      continue;
    int h = (p.first >= X ? 0 : X - p.first);

    for (int i = 0; i < G[p.second].size(); i++) {
      edge &e = G[p.second][i];
      if (H[p.second] < e.cost)
        continue;
      int cost;
      if (h - e.cost > H[e.to])
        cost = h - H[e.to];
      else if (h - e.cost >= 0)
        cost = e.cost;
      else
        cost = e.cost - (h - e.cost);
      if (dist[e.to] <= p.first + cost)
        continue;
      dist[e.to] = p.first + cost;
      que.push(P(dist[e.to], e.to));
    }
  }
  if (dist[N - 1] == INF)
    cout << -1 << endl;
  else
    cout << dist[N - 1] + H[N - 1] - (dist[N - 1] >= X ? 0 : X - dist[N - 1])
         << endl;
  return 0;
}
Problem p00524 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

vector<pint> vp[100001];
ll ans;
ll h[100001];
ll INF = 1000100010001000100;
int n;
ll x;
bool used[100001];
ll dijkstra(int st) {
  priority_queue<pint> pq;
  pq.push(make_pair(x, st));
  while (!pq.empty()) {
    pint pi = pq.top();
    pq.pop();
    ll t = pi.second;
    if (used[t])
      continue;
    used[t] = true;
    if (t == n - 1)
      return ans = pi.first;
    for (vector<pint>::iterator it = vp[t].begin(); it != vp[t].end(); ++it) {
      pq.push(make_pair(min(h[it->second], pi.first - it->first), it->second));
    }
  }
  return ans = -INF;
}
int main() {
  int m, sr, ds, dt;
  cin >> n >> m >> x;
  rep(i, n) cin >> h[i];
  rep(i, m) {
    cin >> sr >> ds >> dt;
    --sr, --ds;
    if (h[sr] >= dt)
      vp[sr].pb(dt, ds);
    if (h[ds] >= dt)
      vp[ds].pb(dt, sr);
  }
  dijkstra(0);
  if (ans != -INF)
    cout << x - ans * 2 + h[n - 1] << endl;
  else
    cout << -1 << endl;
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define pb emplace_back
typedef long long ll;
typedef pair<ll, ll> pint;

vector<pint> vp[100001];
ll ans;
ll h[100001];
ll INF = 1000100010001000100;
int n;
ll x;
ll dijkstra(int st) {
  priority_queue<pint> pq;
  pq.push(make_pair(x, st));
  while (!pq.empty()) {
    pint pi = pq.top();
    pq.pop();
    ll t = pi.second;
    if (t == n - 1)
      return ans = pi.first;
    // if(d[t]<pi.first) continue;
    for (vector<pint>::iterator it = vp[t].begin(); it != vp[t].end(); ++it) {
      pq.push(make_pair(min(h[it->second], pi.first - it->first), it->second));
    }
  }
  return ans = -1;
}
int main() {
  int m, sr, ds, dt;
  cin >> n >> m >> x;
  rep(i, n) cin >> h[i];
  rep(i, m) {
    cin >> sr >> ds >> dt;
    --sr, --ds;
    if (h[sr] >= dt)
      vp[sr].pb(dt, ds);
    if (h[ds] >= dt)
      vp[ds].pb(dt, sr);
  }
  dijkstra(0);
  if (ans != -1)
    cout << x - ans * 2 + h[n - 1] << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p00526 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
#define N 100000

using namespace std;

int main() {
  int n;
  cin >> n;
  string line;
  cin.ignore();
  getline(cin, line);

  int sum[N] = {-1};
  int write = 1;
  int before = line.c_str()[0] - '0';

  for (int i = 1; i < n; i++) {
    if (before - (line.c_str()[2 * i] - '0') == 0) {
      sum[write++] = i - 1;
    }
    before = line.c_str()[2 * i] - '0';
  }
  sum[write++] = n - 1;

  int max_size = 0;
  if (write <= 3) {
    max_size = n;
  } else {
    for (int i = 3; i < write; i++) {
      max_size = std::max(max_size, sum[i] - sum[i - 3]);
    }
  }
  cout << max_size << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define N 100000

using namespace std;

inline void DeleteSpace(string &buf) {
  size_t pos;
  while ((pos = buf.find_first_of(" ???\\t")) != string::npos) {
    buf.erase(pos, 1);
  }
}

int main() {
  int n;
  cin >> n;
  string line;
  cin.ignore();
  getline(cin, line);
  DeleteSpace(line);

  int sum[N] = {-1};
  int write = 1;
  int before = line.c_str()[0] - '0';

  for (int i = 1; i < n; i++) {
    if (before - (line.c_str()[i] - '0') == 0) {
      sum[write++] = i - 1;
    }
    before = line.c_str()[i] - '0';
  }
  sum[write++] = n - 1;

  int max_size = 0;
  if (write <= 3) {
    max_size = n;
  } else {
    for (int i = 3; i < write; i++) {
      max_size = std::max(max_size, sum[i] - sum[i - 3]);
    }
  }
  cout << max_size << endl;
  return 0;
}
Problem p00526 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> r(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);

    if (i % 2) {
      r[i] ^= 1;
    } // Reverse
  }

  vector<int> compress;
  compress.push_back(0);

  for (int i = 1; i < n; i++) {
    if (r[i - 1] != r[i]) {
      compress.push_back(i);
    }
  }

  compress.push_back(n);

  int ret = 0;

  if (compress.size() <= 3) {
    ret = n;
  } else {
    for (int i = 0; i < compress.size() - 3; i++) {
      ret = max(ret, compress[i + 3] - compress[i]);
    }
  }

  printf("%d\\n", ret);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  scanf("%d", &n);

  vector<int> r(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &r[i]);

    if (i % 2) {
      r[i] ^= 1;
    } // Reverse
  }

  vector<int> compress;
  compress.push_back(0);

  for (int i = 1; i < n; i++) {
    if (r[i - 1] != r[i]) {
      compress.push_back(i);
    }
  }

  compress.push_back(n);

  int ret = 0;

  for (int i = 0; i < compress.size() - 3; i++) {
    ret = max(ret, compress[i + 3] - compress[i]);
  }

  printf("%d\\n", ret);

  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;

int main() {
  int m, n, ma = 0;
  static int dp[2001][2001][2] = {}; // 0:O 1:I
  string s, t;
  cin >> m >> n >> s >> t;
  for (int i = m - 1; i >= 0; i--) {
    if (s[i] == 'I')
      dp[i][n][1] = max(1, dp[i + 1][n][0] + 1);
    else if (dp[i + 1][n][1])
      dp[i][n][0] = dp[i + 1][n][1] + 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == 'I')
      dp[m][i][1] = max(1, dp[m][i + 1][0] + 1);
    else if (dp[m][i + 1][1])
      dp[m][i][0] = dp[m][i + 1][1] + 1;
  }
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      if (s[i] == 'I')
        dp[i][j][1] = dp[i + 1][j][0] + 1;
      else if (dp[i + 1][j][1])
        dp[i][j][0] = dp[i + 1][j][1] + 1;
      if (t[j] == 'I')
        dp[i][j][1] = max(dp[i][j][1], dp[i][j + 1][0] + 1);
      else if (dp[i][j + 1][1])
        dp[i][j][0] = max(dp[i][j][0], dp[i][j + 1][1] + 1);
      ma = max(ma, dp[i][j][1]);
    }
  }
  cout << ma << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int main() {
  int m, n, ma = 0;
  int dp[2001][2001][2] = {}; // 0:O 1:I
  string s, t;
  cin >> m >> n >> s >> t;
  for (int i = m - 1; i >= 0; i--) {
    if (s[i] == 'I')
      dp[i][n][1] = max(1, dp[i + 1][n][0] + 1);
    else
      dp[i][n][0] = dp[i + 1][n][1] + 1;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (t[i] == 'I')
      dp[m][i][1] = max(1, dp[m][i + 1][0] + 1);
    else
      dp[m][i][0] = dp[m][i + 1][1] + 1;
  }
  for (int i = m - 1; i >= 0; i--) {
    for (int j = n - 1; j >= 0; j--) {
      if (s[i] == 'I')
        dp[i][j][1] = dp[i + 1][j][0] + 1;
      else
        dp[i][j][0] = dp[i + 1][j][1] + 1;
      if (t[j] == 'I')
        dp[i][j][1] = max(dp[i][j][1], dp[i][j + 1][0] + 1);
      else
        dp[i][j][0] = max(dp[i][j][0], dp[i][j + 1][1] + 1);
      ma = max(ma, dp[i][j][1]);
    }
  }
  cout << ma << endl;
  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int i, j, m, n, ans = 0, l;
  static int r[2001][2001][2] = {0};
  char s[4001], t[4001];
  scanf("%d%d", &m, &n);
  scanf("%s", s);
  scanf("%s", t);
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      for (l = 0; l < 2; l++) {
        if (l == 0) {
          if (s[i] == 'I')
            r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
          if (t[j] == 'I')
            r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
        } else {
          if (s[i] == 'O' && r[i][j][1] != 0)
            r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
          if (t[j] == 'O' && r[i][j][1] != 0)
            r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
        }
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      ans = max(ans, r[i][j][1]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
  int i, j, m, n, ans = 0, l;
  static int r[52][52][2] = {0};
  char s[4001], t[4001];
  scanf("%d%d", &m, &n);
  scanf("%s", s);
  scanf("%s", t);
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      for (l = 0; l < 2; l++) {
        if (l == 0) {
          if (s[i] == 'I')
            r[i + 1][j][1] = max(r[i + 1][j][1], r[i][j][0] + 1);
          if (t[j] == 'I')
            r[i][j + 1][1] = max(r[i][j + 1][1], r[i][j][0] + 1);
        } else {
          if (s[i] == 'O' && r[i][j][1] != 0)
            r[i + 1][j][0] = max(r[i + 1][j][0], r[i][j][1] + 1);
          if (t[j] == 'O' && r[i][j][1] != 0)
            r[i][j + 1][0] = max(r[i][j + 1][0], r[i][j][1] + 1);
        }
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      ans = max(ans, r[i][j][1]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

char s[2][2001];
int dp[2002][2002][2], n[2];
int main() {
  scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
  int Max = 0;
  for (int i = n[0]; i >= 0; i--)
    for (int j = n[1]; j >= 0; j--)
      rep(t, 2) rep(k, 2) {
        int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
        if (b == n[k] || a % 2 == t)
          continue;
        if (t != (s[k][b] == 'I' ? 0 : 1))
          dp[i][j][t] = max(dp[i][j][t], a + 1);
        if (dp[i][j][t] % 2)
          Max = max(Max, dp[i][j][t]);
      }
  printf("%d\\n", Max);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

char s[2][2001];
int dp[2001][2001][2], n[2];
int main() {
  scanf("%d%d%s%s", &n[0], &n[1], s[0], s[1]);
  int Max = 0;
  for (int i = n[0]; i >= 0; i--)
    for (int j = n[1]; j >= 0; j--)
      rep(t, 2) rep(k, 2) {
        int a = (k ? dp[i][j + 1][!t] : dp[i + 1][j][!t]), b = (k ? j : i);
        if (b == n[k] || a % 2 == t)
          continue;
        if (t != (s[k][b] == 'I' ? 0 : 1))
          dp[i][j][t] = max(dp[i][j][t], a + 1);
        if (dp[i][j][t] % 2)
          Max = max(Max, dp[i][j][t]);
      }
  printf("%d\\n", Max);
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int dp[2001][2001][2];
//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int main() {
  int N, M;
  cin >> N >> M;
  string a, b;
  cin >> a >> b;
  // reverse(a.begin(), a.end());
  // reverse(b.begin(), b.end());
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 2000; ++i) {
    for (int j = 0; j <= 2000; ++j) {
      dp[i][j][1] = 0;
      dp[i][j][0] = -1e9;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (i != N) {
          if ((a[i] == 'I') == k) {
            dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
          }
        }
        if (j != M) {
          if ((b[j] == 'I') == k) {
            dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
          }
        }
      }
      ans = max(ans, dp[i][j][0]);
    }
  }
  if (ans && ans % 2 == 0) {
    ans--;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

int dp[2001][2001][2];
//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
int main() {
  int N, M;
  cin >> N >> M;
  string a, b;
  cin >> a >> b;
  // reverse(a.begin(), a.end());
  // reverse(b.begin(), b.end());
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i <= 2000; ++i) {
    for (int j = 0; j <= 2000; ++j) {
      dp[i][j][1] = 0;
      dp[i][j][0] = -1e9;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N; ++i) {
    for (int j = 0; j <= M; ++j) {
      for (int k = 0; k < 2; ++k) {
        if (i != N) {
          if ((a[i] == 'I') == k) {
            dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
          }
        }
        if (j != M) {
          if ((b[j] == 'I') == k) {
            dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
          }
        }
      }
      ans = max(ans, dp[i][j][0]);
    }
  }
  if (ans % 2 == 0) {
    assert(false);
    ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
// #define int long long
#define inf 1145141919ll
#define oku 1000000007ll
using namespace std;
int a, b, c, d;
int dx[] = {0, -1, 0, 1, 0};
struct A {
  A *e;
  int temp;
};
struct B {
  A *src;
  A *dst;
  int cost;
};
struct C {
  int u;
  int v;
  int len;
  C(int x, int y, int z) : u(x), v(y), len(z) {}
};
int i, j, k, n;
int p[2000], q[2000], r[2][2001][2001];
signed main() {
  fill(&r[0][0][0], &r[0][0][0] + 2 * 2001 * 2001, -1);
  char ta[2];
  ta[0] = 'O';
  ta[1] = 'I';
  cin >> a >> b;
  string s, t;
  cin >> s >> t;
  queue<C> qu;
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (r[0][i][j] > 0)
        continue;
      qu.push({i, j, 0});
      while (!qu.empty()) {
        auto x = qu.front();
        qu.pop();
        if (r[x.len & 1][x.u][x.v] >= x.len)
          continue;
        else
          r[x.len & 1][x.u][x.v] = x.len;
        if (x.len & 1)
          n = max(n, x.len);
        if (x.u < a && s[x.u] == ta[(x.len & 1) ^ 1])
          qu.push({x.u + 1, x.v, x.len + 1});
        if (x.v < b && t[x.v] == ta[(x.len & 1) ^ 1])
          qu.push({x.u, x.v + 1, x.len + 1});
      }
    }
  }
  cout << n << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
// #define int long long
#define inf 1145141919ll
#define oku 1000000007ll
using namespace std;
int a, b, c, d;
int dx[] = {0, -1, 0, 1, 0};
struct A {
  A *e;
  int temp;
};
struct B {
  A *src;
  A *dst;
  int cost;
};
struct C {
  int u;
  int v;
  int len;
  C(int x, int y, int z) : u(x), v(y), len(z) {}
};
int i, j, k, n;
int p[2000], q[2000], r[2][2001][2001];
signed main() {
  char ta[2];
  ta[0] = 'O';
  ta[1] = 'I';
  cin >> a >> b;
  string s, t;
  cin >> s >> t;
  queue<C> qu;
  for (i = 0; i <= a; i++) {
    for (j = 0; j <= b; j++) {
      if (r[0][i][j] == -1)
        continue;
      qu.push({i, j, 0});
      while (!qu.empty()) {
        auto x = qu.front();
        qu.pop();
        r[x.len & 1][x.u][x.v] = -1;
        if (x.len & 1)
          n = max(n, x.len);
        if (x.u < a && s[x.u] == ta[(x.len & 1) ^ 1])
          qu.push({x.u + 1, x.v, x.len + 1});
        if (x.v < b && t[x.v] == ta[(x.len & 1) ^ 1])
          qu.push({x.u, x.v + 1, x.len + 1});
      }
    }
  }
  cout << n << endl;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[4001], T[4001];
int dp[4000][4000][2];
int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j][1] = -0x7ffffff;
      dp[i][j][0] = 0;
    }
  }
  scanf("%s", S + 1);
  scanf("%s", T + 1);
  int MAX = 0;
  for (int s = 0; s <= N; s++) {
    for (int t = 0; t <= M; t++) {
      if (s < N && S[s + 1] == 'I') {
        dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s + 1][t][1]);
      }
      if (t < M && T[t + 1] == 'I') {
        dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s][t + 1][1]);
      }
      if (s < N && S[s + 1] == 'O') {
        dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s+1][t][0]);
      }
      if (t < M && T[t + 1] == 'O') {
        dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s][t+1][0]);
      }
    }
  }
  cout << MAX << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int N, M;
char S[2001], T[2001];
int dp[2000][2000][2];
int main() {

  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j][1] = -0x7ffffff;
      dp[i][j][0] = 0;
    }
  }
  scanf("%s", S + 1);
  scanf("%s", T + 1);
  int MAX = 0;
  for (int s = 0; s <= N; s++) {
    for (int t = 0; t <= M; t++) {
      if (s < N && S[s + 1] == 'I') {
        dp[s + 1][t][1] = max(dp[s + 1][t][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s + 1][t][1]);
      }
      if (t < M && T[t + 1] == 'I') {
        dp[s][t + 1][1] = max(dp[s][t + 1][1], dp[s][t][0] + 1);
        MAX = max(MAX, dp[s][t + 1][1]);
      }
      if (s < N && S[s + 1] == 'O') {
        dp[s + 1][t][0] = max(dp[s + 1][t][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s+1][t][0]);
      }
      if (t < M && T[t + 1] == 'O') {
        dp[s][t + 1][0] = max(dp[s][t + 1][0], dp[s][t][1] + 1);
        // MAX = max(MAX, dp[s][t+1][0]);
      }
    }
  }
  cout << MAX << endl;
  return 0;
}