alexjercan/bugnet huggingface dataset:
Problem p00991 (C++) — buggy submission, verdict: Runtime Error

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

int d[1000][1000], ans[1000][1000];
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int r, c, x1, y1, x2, y2;
  cin >> r >> c >> x1 >> y1 >> x2 >> y2;
  queue<P> que;
  memset(d, -1, sizeof(d));
  ans[x1][y1] = 1;
  d[x1][y1] = 0;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      int nx = p.first + dx[i] + r, ny = p.second + dy[i] + c;
      while (nx >= r)
        nx -= r;
      while (ny >= c)
        ny -= c;
      if (d[nx][ny] == -1) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
      if (d[nx][ny] == d[p.first][p.second] + 1)
        (ans[nx][ny] += ans[p.first][p.second]) %= MOD;
    }
  }
  printf("%d\\n", ans[x2][y2]);
}

Error: 0

Fixed submission:

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

int d[1000][1000], ans[1000][1000];
int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1};
int main() {
  int r, c, x1, y1, x2, y2;
  cin >> r >> c >> x1 >> y1 >> x2 >> y2;
  queue<P> que;
  memset(d, -1, sizeof(d));
  ans[x1][y1] = 1;
  d[x1][y1] = 0;
  que.push(P(x1, y1));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    rep(i, 4) {
      int nx = p.first + dx[i] + r, ny = p.second + dy[i] + c;
      if (nx >= r)
        nx -= r;
      if (ny >= c)
        ny -= c;
      if (d[nx][ny] == -1) {
        d[nx][ny] = d[p.first][p.second] + 1;
        que.push(P(nx, ny));
      }
      if (d[nx][ny] == d[p.first][p.second] + 1)
        (ans[nx][ny] += ans[p.first][p.second]) %= MOD;
    }
  }
  printf("%d\\n", ans[x2][y2]);
}
Problem p00991 (C++) — buggy submission, verdict: Time Limit Exceeded

// Name: Grid
// Level: 2
// Category: 数学,動的計画法,DP
// Note:

/*
 */
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MOD = 100000007;

int memo[1001][1001];
int comb(int n, int r) {
  if (r == 0)
    return 1;
  if (n == r)
    return 1;
  if (n < r)
    return 0;
  if (memo[n][r] != 0)
    return memo[n][r];
  return memo[n][r] = (comb(n - 1, r) + comb(n - 1, r - 1)) % MOD;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int R, C;
  pair<int, int> start, goal;
  while (cin >> R >> C >> start.first >> start.second >> goal.first >>
         goal.second) {
    const int left = (start.second - goal.second + C) % C;
    const int right = (goal.second - start.second + C) % C;
    const int up = (start.first - goal.first + R) % R;
    const int down = (goal.first - start.first + R) % R;
    const int mc = min(left, right);
    const int mr = min(up, down);
    int pat = comb(mc + mr, mc);
    if (left != 0 && left == right)
      pat = (pat * 2) % MOD;
    if (up != 0 && up == down)
      pat = (pat * 2) % MOD;
    cout << pat << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// Name: Grid
// Level: 2
// Category: 動的計画法,DP
// Note:

/*
 */
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

const int MOD = 100000007;

int memo[1000][1000];
int buf[1000][1000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int R, C;
  pair<int, int> start, goal;
  while (cin >> R >> C >> start.first >> start.second >> goal.first >>
         goal.second) {
    fill_n((int *)memo, sizeof(memo) / sizeof(int), -1);
    fill_n((int *)buf, sizeof(buf) / sizeof(int), 0);
    vector<pair<int, int>> q;
    q.push_back(start);
    memo[start.first][start.second] = 1;
    while (q.size() > 0) {
      vector<pair<int, int>> next;
      next.reserve(q.size() * 4);
      bool goaled = false;
      /*
      for(int i = 0; i < R; ++i) {
          for(int j = 0; j < C; ++j) {
              buf[i][j] = 0;
          }
      }
      */
      for (vector<pair<int, int>>::iterator it = q.begin(); it != q.end();
           ++it) {
        const pair<int, int> pos = *it;
        const int dr[] = {0, -1, 0, 1};
        const int dc[] = {1, 0, -1, 0};
        for (int i = 0; i < 4; ++i) {
          const int nr = (pos.first + dr[i] + R) % R;
          const int nc = (pos.second + dc[i] + C) % C;
          if (memo[nr][nc] == -1) {
            memo[nr][nc] = 0;
            next.push_back(make_pair(nr, nc));
            if (next.back() == goal)
              goaled = true;
          }
          buf[nr][nc] += memo[pos.first][pos.second];
          buf[nr][nc] %= MOD;
        }
      }
      for (int i = 0; i < R; ++i) {
        for (int j = 0; j < C; ++j) {
          memo[i][j] += buf[i][j];
          memo[i][j] %= MOD;
          buf[i][j] = 0;
        }
      }
      if (goaled)
        break;
      q.swap(next);
    }
    cout << memo[goal.first][goal.second] << endl;
  }
  return 0;
}
Problem p00993 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
using namespace std;
#define MAX_P 20000
int n;
int dp[5010][MAX_P];
void fact(int n) {
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < MAX_P; j++) {
      dp[i][j] += dp[i - 1][j] * i;
      if (dp[i][j] >= 10) {
        dp[i][j + 1] += dp[i][j] / 10;
        dp[i][j] %= 10;
      }
    }
  }
  dp[n][0] += 2;
  for (int j = 0; j < MAX_P; j++) {
    if (dp[n][j] >= 10) {
      dp[n][j + 1] += dp[n][j] / 10;
      dp[n][j] %= 10;
    }
  }
}
int main() {
  cin >> n;
  fact(n + 1);
  int ok = 0;
  for (int i = MAX_P - 1; i >= 0; i--) {
    if (dp[n + 1][i] >= 1) {
      ok = 1;
    }
    if (ok == 1) {
      cout << dp[n + 1][i];
    }
  }
  cout << endl;
  for (int i = 2; i < n + 2; i++) {
    cout << i << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;
#define MAX_P 50000
int n;
int dp[5010][MAX_P];
void fact(int n) {
  dp[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < MAX_P; j++) {
      dp[i][j] += dp[i - 1][j] * i;
      if (dp[i][j] >= 10) {
        dp[i][j + 1] += dp[i][j] / 10;
        dp[i][j] %= 10;
      }
    }
  }
  dp[n][0] += 2;
  for (int j = 0; j < MAX_P; j++) {
    if (dp[n][j] >= 10) {
      dp[n][j + 1] += dp[n][j] / 10;
      dp[n][j] %= 10;
    }
  }
}
int main() {
  cin >> n;
  fact(n + 1);
  int ok = 0;
  for (int i = MAX_P - 1; i >= 0; i--) {
    if (dp[n + 1][i] >= 1) {
      ok = 1;
    }
    if (ok == 1) {
      cout << dp[n + 1][i];
    }
  }
  cout << endl;
  for (int i = 2; i < n + 2; i++) {
    cout << i << endl;
  }
  return 0;
}
Problem p00997 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MAX 252521

class Union_Find {
public:
  int par[MAX], rnk[MAX], sz[MAX], gnum;

  Union_Find(int N) {
    gnum = N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rnk[i] = 0;
      sz[i] = 1;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rnk[x] < rnk[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
      if (rnk[x] == rnk[y]) {
        rnk[x]++;
      }
    }
    gnum--;
  }

  bool same(int x, int y) { return (find(x) == find(y)); }

  int size(int x) { return sz[find(x)]; }

  int groups() { return gnum; }
};

struct edge {
  int u, v, cost;
};

bool comp(const edge &a, const edge &b) { return a.cost < b.cost; }

using ll = long long;

edge es[MAX];

ll solve(int N) {
  Union_Find uf(N);
  sort(es, es + N - 1, comp);

  ll res = 0;
  for (int i = 0; i < N - 1; i++) {
    edge &e = es[i];
    if (!uf.same(e.u, e.v)) {
      res += (ll)uf.size(e.u) * uf.size(e.v) * e.cost;
      uf.unite(e.u, e.v);
    }
  }
  return res;
}

int main() {
  int N, a, b, c;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b >> c;
    es[i] = {a, b, c};
  }
  cout << solve(N) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 100000

class Union_Find {
public:
  int par[MAX], rnk[MAX], sz[MAX], gnum;

  Union_Find(int N) {
    gnum = N;
    for (int i = 0; i < N; i++) {
      par[i] = i;
      rnk[i] = 0;
      sz[i] = 1;
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    }
    return par[x] = find(par[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rnk[x] < rnk[y]) {
      par[x] = y;
      sz[y] += sz[x];
    } else {
      par[y] = x;
      sz[x] += sz[y];
      if (rnk[x] == rnk[y]) {
        rnk[x]++;
      }
    }
    gnum--;
  }

  bool same(int x, int y) { return (find(x) == find(y)); }

  int size(int x) { return sz[find(x)]; }

  int groups() { return gnum; }
};

struct edge {
  int u, v, cost;
};

bool comp(const edge &a, const edge &b) { return a.cost < b.cost; }

using ll = long long;

edge es[MAX];

ll solve(int N) {
  Union_Find uf(N);
  sort(es, es + N - 1, comp);

  ll res = 0;
  for (int i = 0; i < N - 1; i++) {
    edge &e = es[i];
    if (!uf.same(e.u, e.v)) {
      res += (ll)uf.size(e.u) * uf.size(e.v) * e.cost;
      uf.unite(e.u, e.v);
    }
  }
  return res;
}

int main() {
  int N, a, b, c;
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    cin >> a >> b >> c;
    es[i] = {a, b, c};
  }
  cout << solve(N) << endl;
  return 0;
}
Problem p00997 (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 long long ll;

struct st {
  int a, b, c;
};
int s[200000], par[200000];
st v[200000];
ll sum = 0;
int find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y, int c) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  sum += c * (ll)s[x] * s[y];
  par[y] = x;
  s[x] += s[y];
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    s[i] = 1;
    par[i] = i;
  }
  rep(i, n - 1) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    v[i] = {a, b, c};
  }
  sort(v, v + n - 1, [](st &a, st &b) { return a.c < b.c; });
  rep(i, n - 1) { unite(v[i].a, v[i].b, v[i].c); }
  printf("%lld\\n", sum);
}

Error: TLE

Fixed submission:

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

struct st {
  int a, b, c;
};
int s[200000], par[200000], pr[200000];
st v[200000];
int find(int x) {
  if (par[x] == x)
    return x;
  return par[x] = find(par[x]);
}
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  par[y] = x;
  s[x] += s[y];
}
int main() {
  int n;
  scanf("%d", &n);
  rep(i, n) {
    s[i] = 1;
    par[i] = i;
  }
  ll sum = 0;
  rep(i, n - 1) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    v[i] = {a, b, c};
  }
  sort(v, v + n - 1, [](st &a, st &b) { return a.c < b.c; });
  rep(i, n - 1) {
    rep(i, n) pr[i] = s[find(i)];
    unite(v[i].a, v[i].b);
    rep(j, n) { sum += v[i].c * (s[find(j)] - pr[j]); }
  }
  printf("%lld\\n", sum / 2);
}
Problem p00999 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define INF (1LL << 62)

using namespace std;

typedef long long ll;

int main(void) {

  int a, b, c, d, e;
  while (cin >> a >> b >> c >> d >> e, (a || b || c || d || e)) {
    ll na, nb, nc;
    cin >> na >> nb >> nc;

    ll ans = INF, n = na + nb + nc, tna = na, tnb = nb, tnc = nc;
    ll acnt = 0, bcnt = 0, ccnt = 0;

    for (int r = 0; r <= n; r++) {

      if (ccnt < nc && ccnt < r && tnc)
        tnc--, ccnt++;
      else if (bcnt < nb && bcnt + ccnt < r && tnb)
        tnb--, bcnt++;
      else if (acnt < na && acnt + bcnt + ccnt < r && tna)
        tna--, acnt++;

      if (r >= d)
        ans = min(ans, tna * a + tnb * b + tnc * c + r * e);
      else if (r < d && d * e < (a * acnt + b * bcnt + c * ccnt))
        ans = min(ans, tna * a + tnb * b + tnc * c + d * e);
      else
        ans = min(ans, na * a + nb * b + nc * c);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main(void) {

  int a, b, c, d, e;
  while (cin >> a >> b >> c >> d >> e, (a || b || c || d || e)) {
    int na, nb, nc;
    cin >> na >> nb >> nc;

    int ans = 100000000;
    int n = na + nb + nc;
    int pre = 100000001;

    for (int r = nc; r <= n; r++) {
      int tmp = c * nc, tna = na, tnb = nb, tnc = 0;
      int acnt = 0, bcnt = 0, ccnt = nc;

      while (bcnt < nb && bcnt + ccnt < r && tnb)
        tnb--, bcnt++;
      while (acnt < na && acnt + bcnt + ccnt < r && tna)
        tna--, acnt++;

      if (r >= d)
        ans = min(ans, tna * a + tnb * b + tnc * c + r * e);
      else if (r < d && d * e < (a * acnt + b * bcnt + c * ccnt))
        ans = min(ans, tna * a + tnb * b + tnc * c + d * e);
      else
        ans = min(ans, na * a + nb * b + nc * c);
      if (ans > pre) {
        ans = pre;
        break;
      }
      pre = ans;
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int INF = 1e+9;
const int MAX_N = 1000001;
int s[MAX_N] = {0};

int sum(int i, int j) { return s[i] - s[j]; }

int main() {
  int a, b, c, d, e, na, nb, nc;
  while (cin >> a >> b >> c >> d >> e >> na >> nb >> nc, a) {
    vector<int> v;
    for (int i = 0; i < na; i++)
      v.push_back(a);
    for (int i = 0; i < nb; i++)
      v.push_back(b);
    for (int i = 0; i < nc; i++)
      v.push_back(c);
    sort(v.rbegin(), v.rend());

    fill(s, s + MAX_N, 0);
    s[0] = a * na + b * nb + c * nc;
    for (int i = 0; i < v.size(); i++)
      s[i + 1] = s[i] - v[i];

    // for(int i = 0; i < v.size(); i++) cout << s[i] << endl;

    int ans = s[0];
    for (int i = 0; i <= v.size(); i++) {
      int res = 0;
      // cout << "k:" << i;
      if (d <= i) {
        int add = sum(0, i);
        if (i * e < add)
          add = i * e;
        res += add;
        // cout << ", " << add;
      } else {
        int add = sum(0, i);
        if (d * e < add)
          add = d * e;
        res += add;
        // cout << ", " << add;
      }
      res += s[i];
      // cout << " + " << s[i] << endl;
      ans = min(ans, res);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

const int INF = 1e+9;
int s[100010] = {0};

int sum(int i, int j) { return s[i] - s[j]; }

int main() {
  int a, b, c, d, e, na, nb, nc;
  while (cin >> a >> b >> c >> d >> e >> na >> nb >> nc, a) {
    vector<int> v;
    for (int i = 0; i < na; i++)
      v.push_back(a);
    for (int i = 0; i < nb; i++)
      v.push_back(b);
    for (int i = 0; i < nc; i++)
      v.push_back(c);
    sort(v.rbegin(), v.rend());

    fill(s, s + 100010, 0);
    s[0] = a * na + b * nb + c * nc;
    for (int i = 0; i < v.size(); i++)
      s[i + 1] = s[i] - v[i];

    // for(int i = 0; i < v.size(); i++) cout << s[i] << endl;

    int ans = s[0];
    for (int i = 0; i <= v.size(); i++) {
      int res = 0;
      // cout << "k:" << i;
      if (d <= i) {
        int add = sum(0, i);
        if (i * e < add)
          add = i * e;
        res += add;
        // cout << ", " << add;
      } else {
        int add = sum(0, i);
        if (d * e < add)
          add = d * e;
        res += add;
        // cout << ", " << add;
      }
      res += s[i];
      // cout << " + " << s[i] << endl;
      ans = min(ans, res);
    }
    cout << ans << endl;
  }
}
Problem p00999 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
long long a, b, c, d, e, na, nb, nc;
long long F[500000];
long long price(long long L) {
  if (L < nc)
    return c;
  if (L < nb + nc)
    return b;
  return a;
}
long long ruisekiwa(long long L, long long R) {
  if (L >= R)
    return 0;
  if (L == 0)
    return F[R - 1];
  return F[R - 1] - F[L - 1];
}
int main() {
  while (true) {
    cin >> a >> b >> c >> d >> e;
    if (a == 0)
      break;
    cin >> na >> nb >> nc;
    long long n = na + nb + nc, minx = na * a + nb * b + nc * c;
    for (long long i = 0; i < 500000; i++)
      F[i] = 0;
    for (long long i = 0; i < n; i++)
      F[i] = price(i);
    for (long long i = 1; i < n; i++)
      F[i] += F[i - 1];

    for (long long i = 0; i <= n; i++) {
      long long set1 = ruisekiwa(0, i);
      long long set2 = ruisekiwa(i, min(n, i + d));
      long long set3 = ruisekiwa(min(n, i + d), n);
      if (i >= d) {
        if (set1 >= i * e)
          set1 = i * e;
      }
      if (i < d) {
        if (set1 >= d * e)
          set1 = d * e;
      }
      if (set2 >= d * e)
        set2 = d * e;
      minx = min(minx, set1 + set2 + set3);
    }
    cout << minx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int a, b, c, d, e, na, nb, nc;
int price(int L) {
  if (L < nc)
    return c;
  if (L < nb + nc)
    return b;
  return a;
}
int main() {
  while (true) {
    cin >> a >> b >> c >> d >> e;
    if (a == 0)
      break;
    cin >> na >> nb >> nc;
    int n = na + nb + nc, minx = na * a + nb * b + nc * c;
    for (int i = d; i <= n; i++) {
      int set1 = 0;
      for (int j = 0; j < i; j++)
        set1 += e;
      int set2 = 0;
      for (int j = i; j < min(n, i + d); j++)
        set2 += price(j);
      int set3 = 0;
      for (int j = min(n, i + d); j < n; j++)
        set3 += price(j);
      if (set2 >= d * e)
        set2 = d * e;
      minx = min(minx, set1 + set2 + set3);
    }
    cout << minx << endl;
  }
  return 0;
}
Problem p01006 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

string s;
vector<string> vs;

string table[3] = {"ABC", "DEF", "GHI"};

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  REP(i, 1000) {
    cin >> s;
    bool f = 0;
    REP(j, s.size() - 1) {
      REP(k, 3) {
        REP(l, 3) {
          if (s[j] == table[k][l]) {
            REP(m, 4) {
              if (k + dx[m] >= 0 && k + dx[m] <= 2 && l + dy[m] >= 0 &&
                  l + dy[m] <= 2) {
                if (s[j + 1] == table[k + dx[m]][l + dy[m]]) {
                  break;
                }
              }
              if (m == 3)
                f = 1;
            }
          }
        }
      }
    }
    if (!f)
      vs.push_back(s);
  }
  REP(i, vs.size()) { cout << vs[i] << endl; }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

string s;
vector<string> vs;

string table[3] = {"ABC", "DEF", "GHI"};

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

int main() {
  REP(i, 5) {
    cin >> s;
    bool f = 0;
    REP(j, s.size() - 1) {
      REP(k, 3) {
        REP(l, 3) {
          if (s[j] == table[k][l]) {
            REP(m, 4) {
              if (k + dx[m] >= 0 && k + dx[m] <= 3 && l + dy[m] >= 0 &&
                  l + dy[m] <= 3) {
                if (s[j + 1] == table[k + dx[m]][l + dy[m]]) {
                  break;
                }
              }
              if (m == 3)
                f = 1;
            }
          }
        }
      }
    }
    if (!f)
      vs.push_back(s);
  }
  REP(i, vs.size()) { cout << vs[i] << endl; }
}
Problem p01006 (C++) — buggy submission, verdict: Runtime Error

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

string s[3] = {"ABC", "DEF", "GHI"}, pass;
int vx[4] = {-1, 0, 1, 0}, vy[4] = {0, -1, 0, 1};

bool search(int cnt, int n) {
  if (cnt == pass.size() - 1)
    return true;

  bool ret = false;

  for (int i = 0; i < 4; ++i) {
    int nx = n / 3 + vx[i], ny = n % 3 + vy[i];

    if (0 <= nx && nx <= 2 && 0 <= ny && ny <= 2) {
      if (pass[cnt + 1] == s[nx][ny]) {
        ret = search(cnt + 1, pass[cnt + 1] - 'A');
        if (ret)
          return ret;
      }
    }
  }

  return false;
}

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

  for (int i = 0; i < 1000; ++i) {
    cin >> pass;
    int num = pass[0] - 'A';
    if (search(0, num))
      cout << pass << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

string s[3] = {"ABC", "DEF", "GHI"}, pass;
int vx[4] = {-1, 0, 1, 0}, vy[4] = {0, -1, 0, 1};

bool search(int cnt, int n) {
  if (cnt == pass.size() - 1)
    return true;

  bool ret = false;

  for (int i = 0; i < 4; ++i) {
    int nx = n / 3 + vx[i], ny = n % 3 + vy[i];

    if (0 <= nx && nx <= 3 && 0 <= ny && ny <= 3) {
      if (pass[cnt + 1] == s[nx][ny]) {
        ret = search(cnt + 1, pass[cnt + 1] - 'A');
        if (ret)
          return ret;
      }
    }
  }

  return false;
}

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

  for (int i = 0; i < 1000; ++i) {
    cin >> pass;
    int num = pass[0] - 'A';
    if (search(0, num))
      cout << pass << endl;
  }

  return 0;
}