prithivMLmods/Coder-Stat huggingface dataset:
Problem p00448 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

int bd[10][1000];

int main() {
  while (1) {
    int r, c;
    scanf(" %d %d", &r, &c);
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        scanf(" %d", &bd[i][j]);
      }
    }
    int ans = -1;
    int up = (1 << r);
    for (int sl = 0; sl < up; sl++) {
      int h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }

      int cnt = 0;
      for (int j = 0; j < c; j++) {
        int sm = 0;
        for (int i = 0; i < r; i++) {
          sm += bd[i][j];
        }
        cnt += max(r - sm, sm);
      }
      ans = max(cnt, ans);
      h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

int bd[10][10000];

int main() {
  while (1) {
    int r, c;
    scanf(" %d %d", &r, &c);
    if (r == 0 && c == 0)
      break;
    for (int i = 0; i < r; i++) {
      for (int j = 0; j < c; j++) {
        scanf(" %d", &bd[i][j]);
      }
    }
    int ans = -1;
    int up = (1 << r);
    for (int sl = 0; sl < up; sl++) {
      int h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }

      int cnt = 0;
      for (int j = 0; j < c; j++) {
        int sm = 0;
        for (int i = 0; i < r; i++) {
          sm += bd[i][j];
        }
        cnt += max(r - sm, sm);
      }
      ans = max(cnt, ans);
      h = sl;
      for (int i = 0; i < r; i++) {
        if (h % 2 == 1) {
          for (int j = 0; j < c; j++) {
            bd[i][j] = (bd[i][j] == 0) ? 1 : 0;
          }
        }
        h /= 2;
      }
    }

    printf("%d\n", ans);
  }
  return 0;
}
Problem p00448 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int x[10][10000], H, W;
int main() {
  while (true) {
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> x[i][j];
    }
    int maxn = 0;
    for (int i = 0; i < (1 << H); i++) {
      int cnt = 0;
      for (int j = 0; j < W; j++) {
        int r = 0;
        for (int k = 0; k < H; k++) {
          int Y = (i / (1 << k)) % 2;
          r += (x[k][j] + Y) % 2;
        }
        cnt += max(r, H - r);
      }
      maxn = max(maxn, cnt);
    }
    cout << maxn << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int x[10][10000], H, W;
int main() {
  while (true) {
    cin >> H >> W;
    if (H + W == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> x[i][j];
    }
    int maxn = 0;
    for (int i = 0; i < (1 << H); i++) {
      int cnt = 0;
      for (int j = 0; j < W; j++) {
        int r = 0;
        for (int k = 0; k < H; k++) {
          int Y = (i / (1 << k)) % 2;
          r += (x[k][j] + Y) % 2;
        }
        cnt += max(r, H - r);
      }
      maxn = max(maxn, cnt);
    }
    cout << maxn << endl;
  }
  return 0;
}
Problem p00449 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pi;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> Graph;

int Dijkstr(Graph &info, int s, int g) {

  // if(info.size() <= 0) return -1;

  vector<int> min_cost; // その場の最小値
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  min_cost.resize(info.size(), -1);
  que.push(Pi(0, s));
  min_cost[s] = 0;

  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    if (p.second == g)
      return p.first;
    if (p.first > min_cost[p.second])
      continue;
    for (int i = 0; i < info[p.second].size(); i++) {
      edge &e = info[p.second][i];
      if (min_cost[e.to] == -1 || e.to + p.first < min_cost[e.to]) {
        min_cost[e.to] = p.first + e.cost;
        que.push(Pi(min_cost[e.to], e.to));
      }
    }
  }
  return -1;
}

int main() {

  int n, k, sele, a, b, c, d, e;
  while (cin >> n >> k, n) { // nが0以外の間
    Graph info;
    info.resize(k);
    for (int i = 0; i < k; i++) {
      cin >> sele;
      if (sele == 0) {
        cin >> a >> b;
        // aからbへの経路探索

        cout << Dijkstr(info, a, b) << endl;
      } else if (sele == 1) {
        cin >> c >> d >> e;
        info[c].push_back((edge){d, e});
        info[d].push_back((edge){c, e});
        // cout << "ehehe" << endl;
      }
    }
  }
}


Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

typedef pair<int, int> Pi;
struct edge {
  int to, cost;
};
typedef vector<vector<edge>> Graph;

int Dijkstr(Graph &info, int s, int g) {

  // if(info.size() <= 0) return -1;

  vector<int> min_cost; // その場の最小値
  priority_queue<Pi, vector<Pi>, greater<Pi>> que;
  min_cost.resize(info.size(), -1);
  que.push(Pi(0, s));
  min_cost[s] = 0;

  while (!que.empty()) {
    Pi p = que.top();
    que.pop();
    if (p.second == g)
      return p.first;
    if (p.first > min_cost[p.second])
      continue;
    for (int i = 0; i < info[p.second].size(); i++) {
      edge &e = info[p.second][i];
      if (min_cost[e.to] == -1 || e.cost + p.first < min_cost[e.to]) {
        min_cost[e.to] = p.first + e.cost;
        que.push(Pi(min_cost[e.to], e.to));
      }
    }
  }
  return -1;
}

int main() {

  int n, k, sele, a, b, c, d, e;
  while (cin >> n >> k, n) { // nが0以外の間
    Graph info;
    info.resize(k);
    for (int i = 0; i < k; i++) {
      cin >> sele;
      if (sele == 0) {
        cin >> a >> b;
        // aからbへの経路探索

        cout << Dijkstr(info, a, b) << endl;
      } else if (sele == 1) {
        cin >> c >> d >> e;
        info[c].push_back((edge){d, e});
        info[d].push_back((edge){c, e});
        // cout << "ehehe" << endl;
      }
    }
  }
}
Problem p00449 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define INF 1000000000
using namespace std;
int dis[5010][5010];
int gedi(int x, int y) {
  if (x > y)
    return dis[y][x];
  else
    return dis[x][y];
}

void sedi(int x, int y, int v) {
  if (x > y)
    dis[y][x] = v;
  else
    dis[x][y] = v;
}
int N;
void wf() {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        sedi(i, j, min(gedi(i, j), gedi(i, k) + gedi(k, j)));
      }
    }
  }
}

int main() {
  int K, hog, bak, aho, kso;
  while (1) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dis[i][j] = INF;
      }
    }
    for (int i = 0; i < K; i++) {
      ////
      // for(int i=0;i<N;i++){
      //	for(int j=0;j<N;j++){
      //		cout<<"dis["<<i<<"]["<<j<<"]="<<gedi(i,j)<<" ";
      //	}
      //	cout<<endl;
      // }
      ////
      cin >> hog;
      if (hog == 0) {
        cin >> bak >> aho;
        bak--;
        aho--;
        if (gedi(bak, aho) != INF) {
          cout << gedi(bak, aho) << endl;
        } else {
          cout << -1 << endl;
        }
      } else {
        cin >> bak >> aho >> kso;
        bak--;
        aho--;
        if (gedi(bak, aho) > kso) {
          sedi(bak, aho, kso);
          wf();
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#define INF 1000000000
using namespace std;
int dis[5010][5010];
int gedi(int x, int y) {
  if (x > y)
    return dis[y][x];
  else
    return dis[x][y];
}

void sedi(int x, int y, int v) {
  if (x > y)
    dis[y][x] = v;
  else
    dis[x][y] = v;
}
int N;
void wf() {
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < N; i++) {
      for (int j = i; j < N; j++) {
        sedi(i, j, min(gedi(i, j), gedi(i, k) + gedi(k, j)));
      }
    }
  }
}

int main() {
  int K, hog, bak, aho, kso;
  while (1) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        dis[i][j] = INF;
      }
    }
    for (int i = 0; i < K; i++) {
      ////
      // for(int i=0;i<N;i++){
      //	for(int j=0;j<N;j++){
      //		cout<<"dis["<<i<<"]["<<j<<"]="<<gedi(i,j)<<" ";
      //	}
      //	cout<<endl;
      // }
      ////
      cin >> hog;
      if (hog == 0) {
        cin >> bak >> aho;
        bak--;
        aho--;
        if (gedi(bak, aho) != INF) {
          cout << gedi(bak, aho) << endl;
        } else {
          cout << -1 << endl;
        }
      } else {
        cin >> bak >> aho >> kso;
        bak--;
        aho--;
        if (gedi(bak, aho) > kso) {
          sedi(bak, aho, kso);
          wf();
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;

#define INF 11451419
#define MAX_V 1010

int cost[MAX_V][MAX_V];
int V;

int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;

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

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

int main() {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    V = N * (N - 1) / 2;
    rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
    rep(i, K) {
      int op;
      cin >> op;
      if (op == 0) {
        int a, b;
        cin >> a >> b;
        dijkstra(a - 1);

        int x = d[b - 1];
        cout << (x == INF ? -1 : x) << "\n";
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
        cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

int N, K;

#define INF 11451419
#define MAX_V 1010

int cost[MAX_V][MAX_V];
int V;

int d[MAX_V];
bool used[MAX_V];
void dijkstra(int s) {
  fill(d, d + V, INF);
  fill(used, used + V, false);
  d[s] = 0;

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

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

int main() {
  while (true) {
    cin >> N >> K;
    if (N == 0 && K == 0)
      break;
    V = N;
    rep(u, MAX_V) rep(v, MAX_V) cost[v][u] = INF;
    rep(i, K) {
      int op;
      cin >> op;
      if (op == 0) {
        int a, b;
        cin >> a >> b;
        dijkstra(a - 1);

        int x = d[b - 1];
        cout << (x == INF ? -1 : x) << "\n";
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        cost[c - 1][d - 1] = min(cost[c - 1][d - 1], e);
        cost[d - 1][c - 1] = min(cost[d - 1][c - 1], e);
      }
    }
  }
  return 0;
}
Problem p00449 (C++)

Buggy submission (status: Time Limit Exceeded):
#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;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    vector<vector<int>> diss(N, vector<int>(N, 1e9));
    for (int i = 0; i < N; ++i)
      diss[i][i] = 0;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      if (a) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        diss[c][d] = min(diss[c][d], e);
        diss[d][c] = min(diss[d][c], e);
        for (int j = 0; j < N; ++j) {
          for (int k = 0; k < N; ++k) {
            diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
            diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
          }
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (diss[a][b] >= 1e9) {
          cout << -1 << endl;
        } else {
          cout << diss[a][b] << endl;
        }
      }
    }
  }
  return 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;

//// < "D:\D_Download\Visual Studio
///2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
  while (1) {
    int N, K;
    cin >> N >> K;
    if (!N)
      break;
    vector<vector<int>> diss(N, vector<int>(N, 1e9));
    for (int i = 0; i < N; ++i)
      diss[i][i] = 0;
    for (int i = 0; i < K; ++i) {
      int a;
      cin >> a;
      if (a) {
        int c, d, e;
        cin >> c >> d >> e;
        c--;
        d--;
        diss[c][d] = min(diss[c][d], e);
        diss[d][c] = min(diss[d][c], e);
        for (int j = 0; j < N; ++j) {
          for (int k = 0; k < N; ++k) {
            diss[j][k] = min(diss[j][k], diss[j][c] + diss[c][d] + diss[d][k]);
            diss[j][k] = min(diss[j][k], diss[j][d] + diss[d][c] + diss[c][k]);
          }
        }
      } else {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if (diss[a][b] >= 1e9) {
          cout << -1 << endl;
        } else {
          cout << diss[a][b] << endl;
        }
      }
    }
  }
  return 0;
}
Problem p00449 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0)
      break;
    int route[100][100];
    REP(i, n) REP(j, n) route[i][j] = INF;
    int t, a, b, c;
    REP(i, k) {
      cin >> t;
      if (t == 1) {
        cin >> a >> b >> c;
        a--;
        b--;
        route[a][b] = min(route[a][b], c);
        route[b][a] = min(route[b][a], c);
        REP(i, n) REP(j, n) REP(k, n) {
          route[i][j] = min(route[i][j], route[i][k] + route[k][j]);
        }
      } else {
        cin >> a >> b;
        a--;
        b--;
        if (route[a][b] == INF)
          cout << -1 << endl;
        else
          cout << route[a][b] << endl;
      }
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> P;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
static const int INF = 1e9;
int main(void) {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0)
      break;
    int route[100][100];
    REP(i, n) REP(j, n) route[i][j] = INF;
    int t, a, b, c;
    REP(i, k) {
      cin >> t;
      if (t == 1) {
        cin >> a >> b >> c;
        a--;
        b--;
        route[a][b] = min(route[a][b], c);
        route[b][a] = min(route[b][a], c);
        REP(i, n) REP(j, n) {
          route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
          route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
        }
        REP(i, n) REP(j, n) {
          route[i][j] = min(route[i][j], route[i][a] + route[a][j]);
          route[i][j] = min(route[i][j], route[i][b] + route[b][j]);
        }
      } else {
        cin >> a >> b;
        a--;
        b--;
        if (route[a][b] == INF)
          cout << -1 << endl;
        else
          cout << route[a][b] << endl;
      }
    }
  }
  return 0;
}
Problem p00449 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b;
  while (cin >> a >> b, a | b) {
    int c[100][100];
    for (int x = 0; x < a; x++) {
      for (int y = 0; y < a; y++) {
        c[x][y] = 1 << 29;
      }
    }
    for (int e = 0; e < b; e++) {
      int f;
      cin >> f;
      if (f == 0) {
        int g, h;
        cin >> g >> h;
        g--;
        h--;
        if (c[g][h] != 1 << 29)
          cout << c[g][h] << endl;
        else
          cout << -1 << endl;
      } else {
        int g, h, i;
        cin >> g >> h >> i;
        g--;
        h--;
        c[g][h] = c[h][g] = i;
        for (int j = 0; j < a; j++) {
          for (int k = 0; k < a; k++) {
            for (int l = 0; l < a; l++) {
              if (k == l)
                c[k][l] = 0;
              else
                c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
            }
          }
        }
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int a, b;
  while (cin >> a >> b, a | b) {
    int c[100][100];
    for (int x = 0; x < a; x++) {
      for (int y = 0; y < a; y++) {
        c[x][y] = 1 << 29;
      }
    }
    for (int e = 0; e < b; e++) {
      int f;
      cin >> f;
      if (f == 0) {
        int g, h;
        cin >> g >> h;
        g--;
        h--;
        if (c[g][h] != 1 << 29)
          cout << c[g][h] << endl;
        else
          cout << -1 << endl;
      } else {
        int g, h, i;
        cin >> g >> h >> i;
        g--;
        h--;
        if (c[g][h] > i) {
          c[g][h] = c[h][g] = i;
          for (int j = 0; j < a; j++) {
            for (int k = 0; k < a; k++) {
              for (int l = 0; l < a; l++) {
                if (k == l)
                  c[k][l] = 0;
                else
                  c[k][l] = min(c[k][l], c[k][j] + c[j][l]);
              }
            }
          }
        }
      }
    }
  }
}
Problem p00449 (C++)

Buggy submission (status: Runtime Error):
#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  const int INF = 10000000;
  int n, k, map[100][100], a, b, c;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;

  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j)
        map[i][j] = INF;
    }

    for (int g = 0; g < k; ++g) {
      scanf("%d", &a);
      if (a == 0) {
        // vZ
        int ans = -1;
        bool flag[100] = {};

        scanf("%d%d", &a, &b);
        q.push(pair<int, int>(0, a - 1));

        while (!q.empty()) {
          pair<int, int> p = q.top();
          q.pop();
          int &cost = p.first, &num = p.second;
          if (flag[num])
            continue;
          flag[num] = true;

          if (num == b - 1) {
            ans = cost;
            for (int i = q.size(); i > 0; ++i)
              q.pop();
            break;
          }

          for (int i = 0; i < n; ++i) {
            if (map[num][i] == INF || flag[i])
              continue;
            q.push(pair<int, int>(cost + map[num][i], i));
          }
        }

        printf("%d\n", ans);
      } else {
        // ÇÁ
        scanf("%d%d%d", &a, &b, &c);
        if (map[a - 1][b - 1] > c)
          map[a - 1][b - 1] = map[b - 1][a - 1] = c;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <queue>
#include <stdio.h>
using namespace std;

int main() {
  const int INF = 10000000;
  int n, k, map[100][100], a, b, c;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;

  while (true) {
    scanf("%d%d", &n, &k);
    if (n == 0 && k == 0)
      break;

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j)
        map[i][j] = INF;
    }

    for (int g = 0; g < k; ++g) {
      scanf("%d", &a);
      if (a == 0) {
        // vZ
        int ans = -1;
        bool flag[100] = {};

        scanf("%d%d", &a, &b);
        q.push(pair<int, int>(0, a - 1));

        while (!q.empty()) {
          pair<int, int> p = q.top();
          q.pop();
          int &cost = p.first, &num = p.second;
          if (flag[num])
            continue;
          flag[num] = true;

          if (num == b - 1) {
            ans = cost;
            for (int i = q.size(); i > 0; --i)
              q.pop();
            break;
          }

          for (int i = 0; i < n; ++i) {
            if (map[num][i] == INF || flag[i])
              continue;
            q.push(pair<int, int>(cost + map[num][i], i));
          }
        }

        printf("%d\n", ans);
      } else {
        // ÇÁ
        scanf("%d%d%d", &a, &b, &c);
        if (map[a - 1][b - 1] > c)
          map[a - 1][b - 1] = map[b - 1][a - 1] = c;
      }
    }
  }

  return 0;
}
Problem p00450 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<P> mp;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (i == 0)
        mp.push_back(P(a, 1));
      else if (i % 2 == 0) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else
          mp.push_back(P(a, 1));
      } else if (i % 2 == 1) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else if (i == 1)
          mp[0].val = a, mp[0].num++;
        else
          mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
      }
    }

    int ans = 0;
    for (int i = 0; i < mp.size(); i++)
      if (mp[i].val == 0)
        ans += mp[i].num;
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
#define val first
#define num second

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<P> mp;
    for (int i = 0; i < n; i++) {
      int a;
      cin >> a;
      if (i == 0)
        mp.push_back(P(a, 1));
      else if (i % 2 == 0) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else
          mp.push_back(P(a, 1));
      } else if (i % 2 == 1) {
        if (mp.back().val == a)
          mp[mp.size() - 1].num++;
        else if (mp.size() == 1)
          mp[0].val = a, mp[0].num++;
        else
          mp[mp.size() - 2].num += mp[mp.size() - 1].num + 1, mp.pop_back();
      }
    }

    int ans = 0;
    for (int i = 0; i < mp.size(); i++)
      if (mp[i].val == 0)
        ans += mp[i].num;
    cout << ans << endl;
  }
  return 0;
}
Problem p00450 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
  {
    while (1)
      cin >> n;
    if (!n)
      return 0;
    v.clear();
    for (ll i = 1; i <= n; i++) {
      cin >> x;
      if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
        v.push_back(P(i, x));
      } else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
        if (v.size() == 1) {
          v[0].second = x;
        } else {
          v.erase(v.end() - 1);
        }
      }
    }
    ll ans = 0;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i].second == 0)
        ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef double db;
ll n;
vector<P> v; // first ???????????? second: 0or1
ll x;
int main() {
  while (1) {
    cin >> n;
    if (!n)
      return 0;
    v.clear();
    for (ll i = 1; i <= n; i++) {
      cin >> x;
      if (i == 1 || (i % 2 == 1 && v[v.size() - 1].second != x)) { //??°??????
        v.push_back(P(i, x));
      } else if (i % 2 == 0 && v[v.size() - 1].second != x) { //???????¶????
        if (v.size() == 1) {
          v[0].second = x;
        } else {
          v.erase(v.end() - 1);
        }
      }
    }
    ll ans = 0;
    for (ll i = 0; i < v.size(); i++) {
      if (v[i].second == 0)
        ans += (i == v.size() - 1 ? n + 1 : v[i + 1].first) - v[i].first;
    }
    cout << ans << endl;
  }
}
Problem p00450 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    priority_queue<int> q[2];
    int s[2] = {0};
    int prev = -1;
    for (int i = 0; i < n; ++i) {
      int c, d;
      scanf("%d", &c);
      d = (c + 1) % 2;
      if (i % 2 == 0) {
        if (prev != c)
          q[c].push(i);
      } else {
        if (prev != c) {
          int l = q[d].top();
          q[d].pop();
          int re = i - l;
          s[c] += re;
          s[d] -= re;
        }
      }
      s[c] += 1;
      prev = c;
    }
    printf("%d\n", s[0]);
  }
  return 0;
}

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

int main() {
  while (true) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;
    priority_queue<int> q[2];
    int s[2] = {0};
    int prev = -1;
    for (int i = 0; i < n; ++i) {
      int c, d;
      scanf("%d", &c);
      d = (c + 1) % 2;
      if (i % 2 == 0) {
        if (prev != c)
          q[c].push(i);
      } else {
        if (prev != c) {
          int l = q[d].top();
          q[d].pop();
          int re = i - l;
          s[c] += re;
          s[d] -= re;
          if (l == 0)
            q[c].push(0);
        }
      }
      s[c] += 1;
      prev = c;
    }
    printf("%d\n", s[0]);
  }
  return 0;
}
Problem p00450 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int st[100002] = {0};

int main() {
  int j;
  int n;
  int cnt = 0;
  cin >> n;
  while (n != 0) {
    for (int i = 0; i < n; i += 2) {
      cin >> st[i];
      if (n - i != 1) {
        cin >> st[i + 1];
        if (st[i] != st[i + 1]) {
          j = i;
          while (st[j] != st[j + 1]) {
            st[j] = st[j + 1];
            j--;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (st[i] == 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
    cnt = 0;
    cin >> n;
  }
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int st[100002] = {0};

int main() {
  int j;
  int n;
  int cnt = 0;
  cin >> n;
  while (n != 0) {
    for (int i = 0; i < n; i += 2) {
      cin >> st[i];
      if (n - i != 1) {
        cin >> st[i + 1];
        if (st[i] != st[i + 1]) {
          j = i;
          while (st[j] != st[j + 1]) {
            st[j] = st[j + 1];
            j--;
            if (j < 0) {
              break;
            }
          }
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (st[i] == 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
    cnt = 0;
    cin >> n;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#define N 4000
using namespace std;
int n, m, dp[N][N], ans;
int main() {
  string A, B;
  while (cin >> A >> B) {
    n = A.size();
    m = B.size();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        ans = max(ans, dp[i + 1][j + 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#define N 4001
using namespace std;
int n, m, dp[N][N], ans;
int main() {
  string A, B;
  while (cin >> A >> B) {
    n = A.size();
    m = B.size();
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        dp[i][j] = 0;
    ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (A[i] == B[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
        ans = max(ans, dp[i + 1][j + 1]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

string s, t;
int l[100][100];
int m;

void list(string s, string t) {
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          l[i][j] = 1;
        } else {
          l[i][j] = l[i - 1][j - 1] + 1;
        }
      }
    }
  }
}

int search() {
  m = 0;
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (l[i][j] >= m) {
        m = l[i][j];
        l[i][j] = 0;
      } else {
        l[i][j] = 0;
      }
    }
  }
  return m;
}

int main() {
  for (;;) {
    m = 0;
    if (cin >> s >> t) {
      m = 0;
      list(s, t);
      m = 0;
      cout << search() << endl;
    } else {
      break;
    }
    m = 0;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

string s, t;
int l[4000][4000];
int m;

void list(string s, string t) {
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (s[i] == t[j]) {
        if (i == 0 || j == 0) {
          l[i][j] = 1;
        } else {
          l[i][j] = l[i - 1][j - 1] + 1;
        }
      }
    }
  }
}

int search() {
  m = 0;
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < t.length(); j++) {
      if (l[i][j] >= m) {
        m = l[i][j];
        l[i][j] = 0;
      } else {
        l[i][j] = 0;
      }
    }
  }
  return m;
}

int main() {
  for (;;) {
    m = 0;
    if (cin >> s >> t) {
      m = 0;
      list(s, t);
      m = 0;
      cout << search() << endl;
    } else {
      break;
    }
    m = 0;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N = 4000;
int dp[N][N];

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

  string s, t;
  while (1) {
    cin >> s;
    if (cin.eof() || s == "0") {
      break;
    }
    cin >> t;

    for (size_t i = 0; i < s.size(); ++i) {
      for (size_t j = 0; j < t.size(); ++j) {
        dp[i][j] = 0;
      }
    }

    int ans = 0;
    for (size_t i = 0; i < s.size(); ++i) {
      for (size_t j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else
          dp[i + 1][j + 1] = 0;
      }
    }

    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
const int N = 4001;
int dp[N][N];

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

  string s, t;
  while (1) {
    cin >> s;
    if (cin.eof() || s == "0") {
      break;
    }
    cin >> t;

    for (size_t i = 0; i < s.size(); ++i) {
      for (size_t j = 0; j < t.size(); ++j) {
        dp[i][j] = 0;
      }
    }

    int ans = 0;
    for (size_t i = 0; i < s.size(); ++i) {
      for (size_t j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else
          dp[i + 1][j + 1] = 0;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int dp[4005][4005];

int main() {
  string s;
  while (cin >> s) {
    string sa;
    cin >> sa;
    for (int i = 0; i < 4003; i++)
      for (int j = 0; j < 4003; j++)
        dp[i][j] = 0;
    s.push_back('@');
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < sa.size(); j++) {
        int a = s.size();
        int I = min(a - 1, i + j);
        if (s[I] == sa[j]) {
          dp[I][j + 1] = dp[I - 1][j] + 1;
          ans = max(ans, dp[I][j + 1]);
        } else
          dp[I][j + 1] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int dp[4005][4005];

int main() {
  string s;
  while (cin >> s) {
    string sa;
    cin >> sa;
    for (int i = 0; i < 4003; i++)
      for (int j = 0; j < 4003; j++)
        dp[i][j] = 0;
    s.push_back('@');
    int ans = 0;
    for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < sa.size(); j++) {
        int a = s.size();

        if (s[i] == sa[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s, t;
  while (cin >> s >> t) {
    int ans = 0;
    vector<vector<int>> dp(s.size() + 1, vector<int>(t.size() + 1, 0));
    for (unsigned int i = 0; i < s.size(); ++i) {
      for (unsigned int j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s, t;
  while (cin >> s >> t) {
    short int ans = 0;
    vector<vector<short int>> dp(s.size() + 1,
                                 vector<short int>(t.size() + 1, 0));
    for (unsigned int i = 0; i < s.size(); ++i) {
      for (unsigned int j = 0; j < t.size(); ++j) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i + 1][j + 1]);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int dp[4001][4001];

int main() {
  string s;
  string t;
  int mx;

  while (getline(cin, s)) {
    fill(dp[0], dp[4000] + 4000, 0);
    getline(cin, t);
    int n = s.size(), m = t.size();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }

    mx = 0;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= m + 1; j++)
        mx = max(mx, dp[i][j]);

    cout << mx << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int dp[4002][4002];

int main() {
  string s;
  string t;
  int mx;

  while (getline(cin, s)) {
    fill(dp[0], dp[4000] + 4000, 0);
    getline(cin, t);
    int n = s.size(), m = t.size();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (s[i] == t[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = 0;
        }
      }
    }

    mx = 0;
    for (int i = 0; i <= n + 1; i++)
      for (int j = 0; j <= m + 1; j++)
        mx = max(mx, dp[i][j]);

    cout << mx << endl;
  }

  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>

using namespace std;

short dp[4000][4000];

int main() {
  for (;;) {
    string s, t;

    if (!(cin >> s >> t)) {
      break;
    }

    for (int i = 0; i <= s.size(); i++) {
      dp[i][0] = 0;
    }

    for (int j = 0; j <= t.size(); j++) {
      dp[0][j] = 0;
    }

    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }
    int ans = 0;

    for (int i = 0; i <= s.size(); i++) {
      for (int j = 0; j <= t.size(); j++) {
        if (dp[i][j] > ans) {
          ans = dp[i][j];
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>

using namespace std;

short dp[4001][4001];

int main() {
  for (;;) {
    string s, t;

    if (!(cin >> s >> t)) {
      break;
    }

    for (int i = 0; i <= s.size(); i++) {
      dp[i][0] = 0;
    }

    for (int j = 0; j <= t.size(); j++) {
      dp[0][j] = 0;
    }

    for (int i = 1; i <= s.size(); i++) {
      for (int j = 1; j <= t.size(); j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = 0;
        }
      }
    }
    int ans = 0;

    for (int i = 0; i <= s.size(); i++) {
      for (int j = 0; j <= t.size(); j++) {
        if (dp[i][j] > ans) {
          ans = dp[i][j];
        }
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
string str1;
string str2;
int main() {
  while (cin >> str1 >> str2) {
    int ans = 0;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str1.length(); j++) {
        int tmp = 0;
        for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
          if (str1[i + k] != str2[j + k]) {
            break;
          }
          tmp++;
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
string str1;
string str2;
int main() {
  while (cin >> str1 >> str2) {
    int ans = 0;
    for (int i = 0; i < str1.length(); i++) {
      for (int j = 0; j < str1.length(); j++) {
        if (i > 0 && j > 0 && str1[i - 1] == str2[j - 1])
          continue;
        int tmp = 0;
        for (int k = 0; k + i < str1.length() && k + j < str2.length(); k++) {
          if (str1[i + k] != str2[j + k]) {
            break;
          }
          tmp++;
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <stdio.h>
#include <string.h>
#define max(i, j) (i < j ? j : i)
int dp[4001][4001];
int main(void) {
  char s[4001], t[4001];
  //	printf("%d\n",4000*4000);
  while (scanf("%s", s) != EOF) {
    scanf("%s", t);
    //		dp[0][0]=s[0]==t[0];
    for (int i = 0; i < 4001; i++)
      dp[i][0] = dp[0][i] = 0;
    int sl = strlen(s), tl = strlen(t);
    int ans = 0;
    for (int i = 1; i <= sl; i++) {
      for (int j = 1; j <= tl; j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
          ans = max(ans, dp[i][j]);
        } else
          dp[i][j] = 0;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
#define max(i, j) (i < j ? j : i)
short dp[4001][4001];
int main(void) {
  char s[4001], t[4001];
  //	printf("%d\n",4000*4000);
  while (scanf("%s", s) != EOF) {
    scanf("%s", t);
    //		dp[0][0]=s[0]==t[0];
    for (int i = 0; i < 4001; i++)
      dp[i][0] = dp[0][i] = 0;
    int sl = strlen(s), tl = strlen(t);
    int ans = 0;
    for (int i = 1; i <= sl; i++) {
      for (int j = 1; j <= tl; j++) {
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
          ans = max(ans, dp[i][j]);
        } else
          dp[i][j] = 0;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a, b;
  while (cin >> a >> b) {
    int n = 0;
    int la = a.size();
    int lb = b.size();
    for (int i = 0; i < la; i++) {
      for (int j = 0; j < lb; j++) {
        int k = 0, m = 0;
        do {
          if (a[i + k] == b[j + k]) {
            k++;
            m = 1;
            if (i + k > la - 1 || j + k > lb - 1) {
              m = 0;
            }
          } else {
            m = 0;
          }
        } while (m == 1);
        n = max(n, k);
      }
    }
    cout << n << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a, b;
  while (cin >> a >> b) {
    int n = 0;
    int la = a.size();
    int lb = b.size();
    for (int i = 1 - la; i < lb; i++) {
      int k = 0;
      for (int j = 0; j < la; j++) {
        if (j + i < 0 || j + i > lb - 1) {
          k = 0;
        } else if (a[j] == b[j + i]) {
          k++;
          n = max(n, k);
        } else {
          k = 0;
        }
      }
    }
    cout << n << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int DP[4096][4096];
int main() {
  string s, t;
  while (cin >> s >> t) {
    memset(DP, 0, sizeof(DP));
    int n = s.size(), m = t.size(), ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (s[i] == t[j]) {
          DP[i + 1][j + 1] = DP[i][j] + 1;
          ans = max(DP[i + 1][j + 1], ans);
        } else {
          DP[i + 1][j + 1] = 0;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int DP[4001][4001];

int main() {
  string s, t;
  while (cin >> s >> t) {
    memset(DP, 0, sizeof(DP));
    int n = s.size(), m = t.size(), ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (s[i] == t[j]) {
          DP[i + 1][j + 1] = DP[i][j] + 1;
          ans = max(DP[i + 1][j + 1], ans);
        } else {
          DP[i + 1][j + 1] = 0;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00451 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

#define MAX_S 4000
#define MAX_T 4000
template <typename T> int longest_common_substring(T s, T t) {
  int lens = s.size(), lent = t.size();
  int dp[MAX_S + 1][MAX_T + 1] = {};
  int maxi = 0;

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = 0;
      maxi = max(maxi, dp[i + 1][j + 1]);
    }
  }

  return maxi;
}

int main() {
  string a, b;
  while (cin >> a >> b) {
    cout << longest_common_substring(a, b) << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999

#define MAX_S 4000
#define MAX_T 4000
template <typename T> int longest_common_substring(T s, T t) {
  int lens = s.size(), lent = t.size();
  static int dp[MAX_S + 1][MAX_T + 1];
  rep(i, MAX_S + 1) rep(j, MAX_T + 1) dp[i][j] = 0;

  int maxi = 0;

  rep(i, s.size()) {
    rep(j, t.size()) {
      if (s[i] == t[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = 0;
      maxi = max(maxi, dp[i + 1][j + 1]);
    }
  }

  return maxi;
}

int main() {
  string a, b;
  while (cin >> a >> b) {
    cout << longest_common_substring(a, b) << endl;
  }
}
Problem p00451 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <stdio.h>
#include <string.h>
char a[4001];
char b[4001];
int dp[4001][4001];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  while (scanf(" %s", a) != EOF) {
    scanf(" %s", b);
    int n = strlen(a);
    int m = strlen(b);
    int i, j;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= m; j++)
        dp[i][j] = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (a[i] == b[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
    int ans = 0;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= m; j++)
        ans = max(ans, dp[i][j]);
    printf("%d\n", ans);
  }
}

Fixed submission:
#include <stdio.h>
#include <string.h>
char a[4001];
char b[4001];
short dp[4001][4001];
int max(int a, int b) { return a > b ? a : b; }
int main() {
  while (scanf(" %s", a) != EOF) {
    scanf(" %s", b);
    int n = strlen(a);
    int m = strlen(b);
    int i, j;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= m; j++)
        dp[i][j] = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (a[i] == b[j])
          dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
    int ans = 0;
    for (i = 0; i <= n; i++)
      for (j = 0; j <= m; j++)
        ans = max(ans, dp[i][j]);
    printf("%d\n", ans);
  }
}