alexjercan/bugnet huggingface dataset:
Problem p00519 (C++) — buggy submission, verdict: Time 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;

vector<int> E[5000], G[5000];
int c[5000], r[5000];
int d[5000];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, n) scanf("%d%d", &c[i], &r[i]);
  rep(i, k) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  rep(i, n) {
    queue<int> que;
    memset(d, -1, sizeof(d));
    d[i] = 0;
    que.push(i);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      if (d[p] == r[i])
        continue;
      for (int v : E[p]) {
        if (d[v] == -1) {
          d[v] = d[p] + 1;
          que.push(v);
        }
      }
    }
    rep(j, n) {
      if (i != j && d[j] != -1)
        G[i].push_back(j);
    }
  }
  priority_queue<P, vector<P>, greater<P>> que;
  memset(d, 0x3f, sizeof(d));
  d[0] = 0;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (d[p.second] != p.first)
      continue;
    for (int v : G[p.second]) {
      if (d[v] > p.first + c[p.second]) {
        d[v] = p.first + c[p.second];
        que.push(P(d[v], v));
      }
    }
  }
  printf("%d\\n", d[n - 1]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define INF 1000000007
typedef pair<int, int> P;

struct edge {
  int to, cost;
};
int graf[5005][5005];
int ngraf[5005][5005];
int d[5005];
P cst[5005];
bool used[5005];

vector<edge> G[5005];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    cst[i] = P(a, b);
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    graf[a - 1][b - 1] = 1;
    graf[b - 1][a - 1] = 1;
  }

  for (int ia = 0; ia < n; ia++) { // bfs
    fill(d, d + n, INF);
    queue<int> Q;
    Q.push(ia);
    d[ia] = 0;
    while (Q.size()) {
      int a = Q.front();
      Q.pop();
      for (int i = 0; i < n; i++) {
        if (!graf[a][i])
          continue;
        if (d[i] != INF)
          continue;
        d[i] = d[a] + 1;
        Q.push(i);
      }
    }

    for (int i = 0; i < n; i++) {
      if (ia == i) {
        continue;
      }
      if (d[i] > cst[ia].second)
        continue;
      G[ia].push_back({i, cst[ia].first});
    }
  }

  priority_queue<P, vector<P>, greater<P>> Q;
  fill(d, d + n, INF);
  d[0] = 0;
  Q.push(P(0, 0));

  while (Q.size()) {
    P p = Q.top();
    Q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }

  cout << d[n - 1] << endl;
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 114514191

int N, K;
vector<int> path[5010];
int cost[5010][5010];
int C[5010];
int R[5010];

int dijkstra(int dest) {
  vector<bool> used(N, false);
  vector<int> d(N, INF);
  d[0] = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < N; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;

    for (int u = 0; u < N; u++) {
      d[u] = min(d[u], d[v] + cost[v][u]);
    }
  }
  return d[dest];
}

void bfs(int x) {
  queue<int> q;
  q.push(x);
  q.push(0);
  vector<bool> used(N, false);
  used[x] = true;

  while (!q.empty()) {
    int s = q.front();
    q.pop();
    int dist = q.front();
    q.pop();
    if (dist >= R[x])
      continue;

    for (auto d : path[s]) {
      if (!used[d]) {
        q.push(d);
        q.push(dist + 1);
        used[d] = true;
        cost[x][d] = C[x];
      }
    }
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> C[i] >> R[i];
  }
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    path[a - 1].push_back(b - 1);
    path[b - 1].push_back(a - 1);
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (i == j)
        cost[i][j] = 0;
      else
        cost[i][j] = INF;
    }
  }
  for (int i = 0; i < N; i++) {
    bfs(i);
  }
  cout << dijkstra(N - 1) << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int N, K;
bool path[5010][5010];
int C[5010];
int R[5010];
int cost[5010];
bool visited[5010];
queue<int> updates;

void bfs(int x) {
  int r = R[x];
  int v = cost[x] + C[x];
  for (int i = 0; i < N; i++)
    visited[i] = false;
  visited[x] = true;
  queue<int> q;
  q.push(x);
  q.push(0);

  while (!q.empty()) {
    int s = q.front();
    q.pop();
    int dist = q.front();
    q.pop();
    if (dist >= r)
      continue;

    for (int d = 0; d < N; d++) {
      if (!visited[d] && path[s][d]) {
        q.push(d);
        q.push(dist + 1);
        visited[d] = true;
        if (cost[d] == -1 || v < cost[d]) {
          cost[d] = v;
          updates.push(d);
        }
      }
    }
  }
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> C[i] >> R[i];
    cost[i] = -1;
  }
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    path[a - 1][b - 1] = true;
    path[b - 1][a - 1] = true;
  }

  cost[0] = 0;
  updates.push(0);

  while (!updates.empty()) {
    int i = updates.front();
    updates.pop();
    bfs(i);
  }
  cout << cost[N - 1] << "\\n";
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> P;
struct Jud {
  bool pass;
  int rest;
};
const int MAX_N = 5000;
const int INF = 100000000;
int N, K, C[MAX_N + 1], R[MAX_N + 1], A, B;
int cost[MAX_N + 1];
vector<int> V[MAX_N + 1];
queue<P> que;
Jud jud[MAX_N + 1];

void dfs(int t, int c, int r) {
  if (r == 0 || (jud[t].pass && jud[t].rest >= r))
    return;

  jud[t].pass = true;
  jud[t].rest = r;
  for (int i = 0; i < V[t].size(); i++) {
    if (cost[V[t][i]] > c) {
      cost[V[t][i]] = c;
      que.push(make_pair(V[t][i], c));
    }
    dfs(V[t][i], c, r - 1);
  }
}

int main() {
  P p;
  int t;

  for (int i = 0; i < MAX_N + 1; i++) {
    cost[i] = INF;
  }

  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &C[i], &R[i]);
  for (int i = 1; i <= K; i++) {
    scanf("%d%d", &A, &B);
    V[A].push_back(B);
    V[B].push_back(A);
  }

  cost[1] = 0;
  que.push(make_pair(1, 0)); //(town, cost)
  while (!que.empty()) {
    p = que.front();
    que.pop();

    t = p.first;

    if (p.second != cost[t])
      continue;

    for (int i = 1; i <= N; i++) {
      jud[i].pass = false;
      jud[i].rest = 0;
    }
    dfs(t, cost[t] + C[t], R[t]);
  }

  printf("%d\\n", cost[N]);

  // for(int i = 1; i <= N; i++) printf("cost[%d] = %d\\n", i, cost[i].first);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct Jud {
  bool pass;
  int rest;
};
const int MAX_N = 5000;
const int INF = 100000000;
int N, K, C[MAX_N + 1], R[MAX_N + 1], A, B;
int cost[MAX_N + 1];
vector<int> V[MAX_N + 1];
queue<int> que;
Jud jud[MAX_N + 1];

void dfs(int t, int c, int r) {
  if (r == 0 || (jud[t].pass && jud[t].rest >= r))
    return;

  jud[t].pass = true;
  for (int i = 0; i < V[t].size(); i++) {
    if (cost[V[t][i]] > c) {
      cost[V[t][i]] = c;
      que.push(V[t][i]);
    }
    dfs(V[t][i], c, r - 1);
  }
}

int main() {
  int t;

  for (int i = 0; i < MAX_N + 1; i++) {
    cost[i] = INF;
  }

  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &C[i], &R[i]);
  for (int i = 1; i <= K; i++) {
    scanf("%d%d", &A, &B);
    V[A].push_back(B);
    V[B].push_back(A);
  }

  cost[1] = 0;
  que.push(1); //(town, cost)
  while (!que.empty()) {
    t = que.front();
    que.pop();

    for (int i = 1; i <= N; i++) {
      jud[i].pass = false;
      jud[i].rest = 0;
    }
    dfs(t, cost[t] + C[t], R[t]);
  }

  printf("%d\\n", cost[N]);

  // for(int i = 1; i <= N; i++) printf("cost[%d] = %d\\n", i, cost[i].first);

  return 0;
}
Problem p00520 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int n, d[109], a[109], dp[109][40320];
map<vector<int>, int> dperm;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> d[i];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> pr = {0, 1, 2, 3, 4, 5, 6};
  int cntr = 0;
  do {
    dperm[pr] = cntr++;
  } while (next_permutation(pr.begin(), pr.end()));
  for (int i = 1; i < n; i++) {
    vector<int> p = {0, 1, 2, 3, 4, 5, 6, 7};
    int cnt = 0;
    do {
      vector<int> p2(p.begin(), p.begin() + 7);
      for (int j = 0; j < 7; j++) {
        if (p2[j] > p[7])
          p2[j]--;
      }
      int e = dperm[p2];
      int res = 0;
      for (int j = 1; i - j >= 0 && j <= d[i]; j++) {
        if (p[7 - j] > p[7])
          res += a[i];
      }
      for (int j = 1; i - j >= 0 && j <= 7; j++) {
        if (p[7 - j] < p[7] && d[i - j] >= j)
          res += a[i - j];
      }
      for (int j = 0; j < 8; j++) {
        dp[i][cnt] = max(dp[i][cnt], dp[i - 1][j * 5040 + e] + res);
      }
      cnt++;
    } while (next_permutation(p.begin(), p.end()));
  }
  int ret = 0;
  for (int i = 0; i < 40320; i++)
    ret = max(ret, dp[n - 1][i]);
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int N;

  cin >> N;

  vector<int> D(N);
  vector<int> A(N);
  vector<int> P(N);
  vector<int> S(N);
  vector<bool> E(N);

  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  for (int i = 0; i < N; i++) {
    P[i] = i;
  }

  int l, r, SUM, MAX = 0;

  do {
    for (int i = 0; i < N; i++) {
      S[i] = 0;
      E[i] = false;
    }

    for (int i = 0; i < N; i++) {
      E[P[i]] = true;

      l = P[i];
      r = P[i];

      for (int j = 1; j <= D[P[i]]; j++) {
        if (P[i] + j <= N - 1) {
          if (E[P[i] + j] == false) {
            S[P[i] + j] += A[P[i]];
          }
        }

        if (P[i] - j >= 0) {
          if (E[P[i] - j] == false) {
            S[P[i] - j] += A[P[i]];
          }
        }
      }
    }

    SUM = 0;

    for (int i = 0; i < N; i++) {
      SUM += S[i];
    }

    MAX = max(SUM, MAX);

  } while (next_permutation(P.begin(), P.end()));

  cout << MAX << endl;

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

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

int d[100], a[100];
map<vector<int>, int> id;
map<int, vector<int>> re;
int dp[100][5040];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &d[i]);
  rep(i, n) scanf("%d", &a[i]);
  vector<int> v(7);
  iota(v.begin(), v.end(), 0);
  int p = 0;
  do {
    id[v] = p;
    re[p] = v;
    int cnt = 0, t = 0;
    for (int i : v) {
      rep(j, 7) {
        if ((t >> j & 1) && abs(j - i) <= d[j])
          cnt += a[j];
      }
      t |= 1 << i;
    }
    dp[0][p] = cnt;
    p++;
  } while (next_permutation(v.begin(), v.end()));
  int ans = 0;
  for (int i = 1; i + 6 < n; i++)
    rep(j, 5040) {
      vector<int> v = re[j];
      rep(k, 8) {
        vector<int> t = v;
        t.insert(t.begin() + k, 7);
        int cnt = 0, p = 0;
        for (int u : t) {
          if (u == 7) {
            rep(y, 8) {
              if ((p >> y & 1) && y + d[i - 1 + y] >= 7)
                cnt += a[i - 1 + y];
            }
          }
          if ((p >> 7 & 1) && 7 - d[i + 6] <= u)
            cnt += a[i + 6];
          p |= 1 << u;
          vector<int> q = t;
          q.erase(find(q.begin(), q.end(), 0));
          for (int &c : q)
            c--;
          int v = id[q];
          dp[i][v] = max(dp[i][v], dp[i - 1][j] + cnt);
          ans = max(ans, dp[i][v]);
        }
      }
    }
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

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

int d[100], a[100];
int dp[100][1 << 8];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) scanf("%d", &d[i]);
  rep(i, n) scanf("%d", &a[i]);
  int m = min(8, n);
  rep(j, 1 << m) {
    rep(k, m) {
      if (j >> k & 1)
        continue;
      int cnt = 0;
      rep(t, m) {
        if ((j >> t & 1) && t - d[t] <= k && k <= t + d[t])
          cnt += a[t];
      }
      dp[0][j | 1 << k] = max(dp[0][j | 1 << k], dp[0][j] + cnt);
    }
  }
  for (int i = 1; i + 7 < n; i++) {
    vector<int> v(9);
    iota(v.begin(), v.end(), 0);
    do {
      int cnt = 0, k = 0, t = 0, y = 0;
      bool b = false;
      for (int j : v) {
        k |= 1 << j - 1;
        if (j == 8)
          b = true;
        else
          y |= 1 << j;
        if (!b) {
          t |= 1 << j;
          if (i - 1 + j + d[i - 1 + j] >= i + 7)
            cnt += a[i - 1 + j];
        } else if (j != 8 && i + 7 - d[i + 7] <= i - 1 + j)
          cnt += a[i + 7];
        dp[i][k] = max(dp[i][k], dp[i - 1][t] + dp[i - 1][y - t] + cnt);
      }
    } while (next_permutation(v.begin(), v.end()));
  }
  printf("%d\\n", dp[n - m][(1 << m) - 1]);
}
Problem p00521 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int m, n;
char flag[1100][1010];
char emblem[3][3];
bool g(int i, int j) {
  if (flag[i][j] == emblem[0][0])
    if (flag[i + 1][j] == emblem[1][0])
      if (flag[i][j + 1] == emblem[0][1])
        if (flag[i + 1][j + 1] == emblem[1][1])
          return true;
  return false;
}
int f() {
  int sum = 0;
  for (int i = 1; i < m; i++) {
    for (int j = 1; j < n; j++) {
      if (g(i, j))
        sum++;
    }
  }
  return sum;
}
int g1(int i, int j) {
  int cnt = 0;
  if (i > 1) {
    if (j > 1)
      cnt += g(i - 1, j - 1);
    cnt += g(i - 1, j);
  }
  if (j > 1)
    cnt += g(i, j - 1);
  cnt += g(i, j);
  return cnt;
}
int main() {
  cin >> m >> n;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      cin >> flag[i][j];
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      cin >> emblem[i][j];
  int ans = f();
  int ans1 = ans;
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      int a = g1(i, j);
      int b = g1(i, j);
      char ch = flag[i][j];
      flag[i][j] = 'J';
      b = max(b, g1(i, j));
      flag[i][j] = 'O';
      b = max(b, g1(i, j));
      flag[i][j] = 'I';
      b = max(b, g1(i, j));
      flag[i][j] = ch;
      if (b > a)
        ans1 = max(ans1, ans + b - a);
    }
  }
  cout << ans1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int m, n;
char flag[1100][1010];
char emblem[3][3];
int f() {
  int sum = 0;
  for (int i = 1; i < m; i++) {
    for (int j = 1; j < n; j++) {
      if (flag[i][j] == emblem[0][0]) {
        if (flag[i + 1][j] == emblem[1][0] && flag[i][j + 1] == emblem[0][1]) {
          if (flag[i + 1][j + 1] == emblem[1][1]) {
            sum++;
          }
        }
      }
    }
  }
  return sum;
}
int main() {
  cin >> m >> n;
  int ans = 0;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      cin >> flag[i][j];
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < 2; j++)
      cin >> emblem[i][j];
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      char ch = flag[i][j];
      flag[i][j] = 'J';
      ans = max(ans, f());
      flag[i][j] = 'O';
      ans = max(ans, f());
      flag[i][j] = 'I';
      ans = max(ans, f());
      flag[i][j] = ch;
    }
  }
  cout << ans << endl;
}
Problem p00521 (C++) — buggy submission, verdict: Runtime Error

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, m, cnt = 0;
char c[1002][1002], x[3][3];

bool check(int i, int j) {
  FOR(k, 1, 2)
  FOR(l, 1, 2)
  if (c[i + k - 1][j + l - 1] != x[k][l])
    return false;
  return true;
}

int solve(char y, int i, int j) {
  int cnt1 = 0, cnt2 = 0;
  char z = c[i][j];
  if (check(i, j))
    cnt1++;
  if (check(i - 1, j - 1))
    cnt1++;
  if (check(i - 1, j))
    cnt1++;
  if (check(i, j - 1))
    cnt1++;
  c[i][j] = y;
  if (check(i, j))
    cnt2++;
  if (check(i - 1, j - 1))
    cnt2++;
  if (check(i - 1, j))
    cnt2++;
  if (check(i, j - 1))
    cnt2++;
  c[i][j] = z;
  return (cnt2 - cnt1);
}

int main() {
  cin >> m >> n;
  FOR(i, 1, m) {
    scanf("\\n");
    FOR(j, 1, n)
    scanf("%c", &c[i][j]);
  }
  scanf("\\n%c%c\\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
  For(i, 1, m) For(j, 1, n) {
    if (check(i, j))
      cnt++;
  }
  int res = 0;
  FOR(i, 1, m)
  FOR(j, 1, n) {
    res = max(res, solve('J', i, j));
    res = max(res, solve('O', i, j));
    res = max(res, solve('I', i, j));
  }
  cout << cnt + res << '\\n';
}

Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define FORV(i, a, b) for (int i = a; i * i <= b; i++)
#define Forv(i, a, b) for (int i = a; i * i < b; i++)
#define For(i, a, b) for (int i = a; i < b; i++)
#define Ford(i, a, b) for (int i = a; i > b; i--)
#define Fill(s, a) memset(s, a, sizeof(s))
#define pb push_back
#define mp make_pair
#define fi first
#define se second

int n, m, cnt = 0;
char c[1001][1001], x[3][3];

bool check(int i, int j) {
  FOR(k, 1, 2)
  FOR(l, 1, 2)
  if (c[i + k - 1][j + l - 1] != x[k][l])
    return false;
  return true;
}

int solve(char y, int i, int j) {
  int cnt1 = 0, cnt2 = 0;
  char z = c[i][j];
  if (check(i, j))
    cnt1++;
  if (check(i - 1, j - 1))
    cnt1++;
  if (check(i - 1, j))
    cnt1++;
  if (check(i, j - 1))
    cnt1++;
  c[i][j] = y;
  if (check(i, j))
    cnt2++;
  if (check(i - 1, j - 1))
    cnt2++;
  if (check(i - 1, j))
    cnt2++;
  if (check(i, j - 1))
    cnt2++;
  c[i][j] = z;
  return (cnt2 - cnt1);
}

int main() {
  cin >> m >> n;
  FOR(i, 1, m) {
    scanf("\\n");
    FOR(j, 1, n)
    scanf("%c", &c[i][j]);
  }
  scanf("\\n%c%c\\n%c%c", &x[1][1], &x[1][2], &x[2][1], &x[2][2]);
  For(i, 1, m) For(j, 1, n) {
    if (check(i, j))
      cnt++;
  }
  int res = 0;
  FOR(i, 1, m)
  FOR(j, 1, n) {
    res = max(res, solve('J', i, j));
    res = max(res, solve('O', i, j));
    res = max(res, solve('I', i, j));
  }
  cout << cnt + res << '\\n';
}
Problem p00521 (C++) — buggy submission, verdict: Runtime Error

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

struct masu {
  int hiku;
  unordered_map<char, int> tasu;
};
masu t[1000][1000];
int main() {
  int a, b;
  cin >> a >> b;
  string c[1000];
  for (int d = 0; d < a; d++)
    cin >> c[d];
  string f[2];
  cin >> f[0] >> f[1];
  int sum = 0;
  for (int g = 0; g < a - 1; g++) {
    for (int h = 0; h < b - 1; h++) {
      int l = 0;
      bool r[2][2]{};
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          if (c[g + i][h + j] == f[i][j]) {
            l++;
            r[i][j] = true;
          }
        }
      }
      if (l == 4) {
        sum++;
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            if (c[g + i][h + j] == f[i][j]) {
              t[g + i][h + j].hiku++;
            }
          }
        }
      } else if (l == 3) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            if (!r[i][j])
              t[g + i][h + j].tasu[f[i][j]]++;
          }
        }
      }
    }
  }
  int MAX = 0;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      MAX = max(MAX,
                max({t[i][j].tasu['J'], t[i][j].tasu['O'], t[i][j].tasu['I']}) -
                    t[i][j].hiku);
    }
  }
  cout << sum + MAX << endl;
}

Error: -6
free(): invalid pointer


Fixed submission:

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

string c[1002];
int ruisekiwa[1000][1000];
int main() {
  int a, b;
  cin >> a >> b;
  for (int d = 0; d < a; d++) {
    cin >> c[d];
  }
  string f[2];
  cin >> f[0] >> f[1];
  int sum = 0;
  for (int g = 0; g < a - 1; g++) {
    for (int h = 0; h < b - 1; h++) {
      bool s[2][2]{};
      int l = 0;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
          if (c[g + i][h + j] == f[i][j]) {
            l++;
            s[i][j] = true;
          }
        }
      }
      if (l == 4) {
        sum++;
        for (int x = 0; x < 2; x++) {
          for (int y = 0; y < 2; y++) {
            ruisekiwa[g + x - 1][h + y - 1]--;
          }
        }
      } else if (l == 3) {
        for (int x = 0; x < 2; x++) {
          for (int y = 0; y < 2; y++) {
            if (!s[x][y])
              ruisekiwa[g + x - 1][h + y - 1]++;
          }
        }
      }
    }
  }
  int MAX = 0;
  for (int i = 0; i < a; i++) {
    for (int p = 0; p < a; p++) {
      MAX = max(MAX, ruisekiwa[i][p]);
    }
  }
  cout << sum + MAX << endl;
}
Problem p00521 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int mm[1005][1005][3];
string flag[1005], emb[2];
int main() {
  int m, n;
  cin >> m >> n;
  int ans = 0;
  for (int i = 0; i < m; ++i)
    cin >> flag[i];
  for (int i = 0; i < 2; ++i)
    cin >> emb[i];
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      int cnt = 0, mx, my;
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l) {
          if (flag[i + k][j + l] == emb[k][l])
            ++cnt;
          else {
            mx = l;
            my = k;
          }
        }
      }
      if (cnt == 4)
        ++ans;
      else if (cnt == 3) {
        if (emb[my][mx] == 'J')
          ++mm[i + my][j + mx][0];
        else if (emb[my][mx] == 'O')
          ++mm[i + my][j + mx][1];
        else
          ++mm[i + my][j + mx][2];
      }
    }
  }
  int w = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < 3; ++k)
        w = max(w, mm[i][j][k]);
    }
  }
  cout << ans + w << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int mm[1001][1001][3];
string flag[1000], emb[2];
int main() {
  int m, n;
  cin >> m >> n;
  int ans = 0;
  for (int i = 0; i < m; ++i)
    cin >> flag[i];
  for (int i = 0; i < 2; ++i)
    cin >> emb[i];
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      int cnt = 0, mx, my;
      for (int k = 0; k < 2; ++k) {
        for (int l = 0; l < 2; ++l) {
          if (flag[i + k][j + l] == emb[k][l])
            ++cnt;
          else {
            mx = l;
            my = k;
          }
        }
      }
      if (cnt == 4)
        ++ans;
      else if (cnt == 3) {
        if (emb[my][mx] == 'J')
          ++mm[i + my][j + mx][0];
        else if (emb[my][mx] == 'O')
          ++mm[i + my][j + mx][1];
        else
          ++mm[i + my][j + mx][2];
      }
    }
  }
  int w = 0;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < 3; ++k)
        w = max(w, mm[i][j][k]);
    }
  }
  cout << ans + w << endl;
  return 0;
}