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

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main() {
  rep(i, 2, 1150) {
    int j = i;
    while (j < 1322500) {
      j += i;
      p[j]++;
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    if (!p[n])
      cout << 0 << endl;
    else {
      int l = n, r = n;
      while (p[l])
        l--;
      while (p[r])
        r++;
      cout << r - l << endl;
    }
  }
  return 0;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS // #pragma warning(disable:4996)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define all(a) a.begin(), a.end()
#define len(x) ((int)(x).size())
#define tmax(a, b, c) max((a), max((b), (c)))
#define tmin(a, b, c) min((a), min((b), (c)))
#define debug(x) cerr << #x << " is " << x << endl;

typedef pair<int, int> Pii;
typedef map<int, int> Mii;
typedef vector<int> Vi;
typedef long long ll;
const int inf = 2e9;
const ll ll_inf = 1e17;
const int mod = 1e9 + 7;
const long double eps = 1e-10;

int p[1322501];

int main() {
  rep(i, 2, 1150) {
    int j = i;
    while (j + i < 1322500) {
      j += i;
      p[j]++;
    }
  }

  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    if (!p[n])
      cout << 0 << endl;
    else {
      int l = n, r = n;
      while (p[l])
        l--;
      while (p[r])
        r++;
      cout << r - l << endl;
    }
  }
  return 0;
}
Problem p00855 (C++)

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

#define MAX 1000001

bool p[MAX];

int main() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for (int i = 0; i < MAX; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int next, prev;
    if (p[n] == 1)
      cout << 0 << endl;

    else {
      for (int i = n;; i++) {
        if (p[i] == 1) {
          next = i;
          break;
        }
      }
      for (int i = n;; i--) {
        if (p[i] == 1) {
          prev = i;
          break;
        }
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}


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

#define MAX 10000001

bool p[MAX];

int main() {
  memset(p, 1, sizeof(p));
  p[0] = p[1] = 0;
  for (int i = 0; i < MAX; i++) {
    if (p[i]) {
      for (int j = 2 * i; j < MAX; j += i) {
        p[j] = 0;
      }
    }
  }

  int n;
  while (cin >> n, n) {
    int next, prev;
    if (p[n] == 1)
      cout << 0 << endl;

    else {
      for (int i = n;; i++) {
        if (p[i] == 1) {
          next = i;
          break;
        }
      }
      for (int i = n;; i--) {
        if (p[i] == 1) {
          prev = i;
          break;
        }
      }
      cout << next - prev << endl;
    }
  }
  return 0;
}
Problem p00855 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
#define N 1000000
bool prime[N + 1];
int main() {
  for (int i = 2; i <= N; ++i)
    prime[i] = true;
  for (int i = 2; i <= N; ++i)
    if (prime[i])
      for (int j = 2; i * j <= N; ++j)
        prime[i * j] = false;

  int k;
  while (cin >> k, k) {
    int m = k, M = k;
    while (!prime[m])
      --m;
    while (!prime[M])
      ++M;
    cout << M - m << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
#define N 2000000
bool prime[N + 1];
int main() {
  for (int i = 2; i <= N; ++i)
    prime[i] = true;
  for (int i = 2; i <= N; ++i)
    if (prime[i])
      for (int j = 2; i * j <= N; ++j)
        prime[i * j] = false;

  int k;
  while (cin >> k, k) {
    int m = k, M = k;
    while (!prime[m])
      --m;
    while (!prime[M])
      ++M;
    cout << M - m << endl;
  }
  return 0;
}
Problem p00855 (C++)

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

int main() {
  vector<bool> prime(1000001, 1);
  prime[1] = 0;
  for (int i = 2; i < prime.size(); i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < prime.size(); j += i)
        prime[j] = 0;
    }
  }
  // for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n + i] == 1)
        break;
      ans++;
    }
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n - i] == 1)
        break;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

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

int main() {
  vector<bool> prime(1299709, 1);
  prime[1] = 0;
  for (int i = 2; i < prime.size(); i++) {
    if (prime[i] == 1) {
      for (int j = i * 2; j < prime.size(); j += i)
        prime[j] = 0;
    }
  }
  // for(int i=0;i<prime.size();i++)if(prime[i]==1)cout<<i<<" ";
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int ans = 0;
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n + i] == 1)
        break;
      ans++;
    }
    for (int i = 0; i < prime.size(); i++) {
      if (prime[n - i] == 1)
        break;
      ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00855 (C++)

Buggy submission (status: Runtime Error):
// Prime Gap
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<ll> prime;

int main() {
  prime.push_back(2);
  for (ll i = 3; i < 1200000; i += 2) {
    bool flag = true;
    for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      prime.push_back(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ll to;
    for (to = 0; n > prime[to]; to++)
      ;
    if (n == prime[to])
      cout << "0" << endl;
    else
      cout << prime[to] - prime[to - 1] << endl;
  }
  return 0;
}

Fixed submission:
// Prime Gap
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

vector<ll> prime;

int main() {
  prime.push_back(2);
  for (ll i = 3; i < 1300000; i += 2) {
    bool flag = true;
    for (ll j = 0; j < prime.size() && i >= prime[j] * prime[j]; j++) {
      if (i % prime[j] == 0) {
        flag = false;
        break;
      }
    }
    if (flag)
      prime.push_back(i);
  }
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    ll to;
    for (to = 0; n > prime[to]; to++)
      ;
    if (n == prime[to])
      cout << "0" << endl;
    else
      cout << prime[to] - prime[to - 1] << endl;
  }
  return 0;
}
Problem p00855 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define N 1200000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
  int i, j, n, s, g;
  for (i = 0; i < N; i++)
    sosu[i] = 1;
  sosu[0] = 0;
  sosu[1] = 0;
  for (i = 0; i < N / 2; i++) {
    if (sosu[i] == 1) {
      for (j = i * 2; j < N; j += i) {
        sosu[j] = 0;
      }
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    if (sosu[n] != 1) {
      s = start(n);
      g = goal(n);
      cout << g - s << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
int start(int n) {
  int i, result;
  if (n % 2 == 0)
    n--;
  else
    n -= 2;
  for (i = n; i >= 2; i -= 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
int goal(int n) {
  int i, result;
  if (n % 2 == 0)
    n++;
  else
    n += 2;
  for (i = n; i < N; i += 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}

Fixed submission:
#include <iostream>
#define N 1300000
using namespace std;
int start(int);
int goal(int);
int sosu[N];
int main() {
  int i, j, n, s, g;
  for (i = 0; i < N; i++)
    sosu[i] = 1;
  sosu[0] = 0;
  sosu[1] = 0;
  for (i = 0; i < N / 2; i++) {
    if (sosu[i] == 1) {
      for (j = i * 2; j < N; j += i) {
        sosu[j] = 0;
      }
    }
  }
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    if (sosu[n] != 1) {
      s = start(n);
      g = goal(n);
      cout << g - s << endl;
    } else
      cout << 0 << endl;
  }
  return 0;
}
int start(int n) {
  int i, result;
  if (n % 2 == 0)
    n--;
  else
    n -= 2;
  for (i = n; i >= 2; i -= 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
int goal(int n) {
  int i, result;
  if (n % 2 == 0)
    n++;
  else
    n += 2;
  for (i = n; i < N; i += 2) {
    if (sosu[i] == 1) {
      result = i;
      break;
    }
  }
  return result;
}
Problem p00856 (C++)

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

using namespace std;

int N, T, L, B;

int next(int cur, int dice) {
  int ret = cur + dice;
  if (N < ret)
    ret = N - (ret - N);
  return ret;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> N >> T >> L >> B && (N || T || L || B)) {
    string LB(' ', 101);
    int k;
    for (int i = 0; i < L; ++i) {
      cin >> k;
      LB[k] = 'L';
    }
    for (int i = 0; i < B; ++i) {
      cin >> k;
      LB[k] = 'B';
    }

    double p[101][101][2] = {0};
    p[0][0][0] = 1.0;
    for (int i = 0; i < T; ++i) {
      for (int j = 0; j < N; ++j) {
        for (int k = 1; k <= 6; ++k) {
          int ne = next(j, k);
          if (LB[ne] == 'B')
            ne = 0;
          p[i + 1][ne][LB[ne] == 'L'] += p[i][j][0] / 6.0;
        }
        p[i + 1][j][0] += p[i][j][1];
      }
    }

    double ans = 0;
    for (int i = 0; i <= T; ++i)
      ans += p[i][N][1] + p[i][N][0];
    cout << setprecision(6) << fixed << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;

int N, T, L, B;

int next(int cur, int dice) {
  int ret = cur + dice;
  if (N < ret)
    ret = N - (ret - N);
  return ret;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> N >> T >> L >> B && (N || T || L || B)) {
    string LB = "                                                              "
                "                                       ";
    int k;
    for (int i = 0; i < L; ++i) {
      cin >> k;
      LB[k] = 'L';
    }
    for (int i = 0; i < B; ++i) {
      cin >> k;
      LB[k] = 'B';
    }

    double p[101][101][2] = {0};
    p[0][0][0] = 1.0;
    for (int i = 0; i < T; ++i) {
      for (int j = 0; j < N; ++j) {
        for (int k = 1; k <= 6; ++k) {
          int ne = next(j, k);
          if (LB[ne] == 'B')
            ne = 0;
          p[i + 1][ne][LB[ne] == 'L'] += p[i][j][0] / 6.0;
        }
        p[i + 1][j][0] += p[i][j][1];
      }
    }

    double ans = 0;
    for (int i = 0; i <= T; ++i)
      ans += p[i][N][1] + p[i][N][0];
    cout << setprecision(6) << fixed << ans << endl;
  }
  return 0;
}
Problem p00856 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define MAX 101
typedef pair<int, int> pii;

int N, T, L, B;
int arr[MAX];

double solve() {
  double dp[MAX][MAX]; // dp[ターン数][どのマスにいるか]
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 1; i <= T; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < 6; k++) {
        if (dp[i - 1][j] == 0)
          continue;
        int next = j + (k + 1);
        if (next > N) {
          next = next - 2 * (next - N);
        }
        switch (arr[next]) {
        case 0:
          dp[i][next] += dp[i - 1][j] / 6;
          break;
        case 1:
          dp[i + 1][next] += dp[i - 1][j] / 6;
          break;
        case 2:
          dp[i][0] += dp[i - 1][j] / 6;
          break;
        }
      }
    }
  }
  double res = 0;
  for (int i = 1; i <= T; i++) {
    res += dp[i][N];
  }
  return res;
}

int main() {
  int in;
  while (cin >> N >> T >> L >> B, N) {
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < L; i++) {
      cin >> in;
      arr[in] = 1;
    }
    for (int i = 0; i < B; i++) {
      cin >> in;
      arr[in] = 2;
    }
    printf("%.8f\n", solve());
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define MAX 501
typedef pair<int, int> pii;

int N, T, L, B;
int arr[MAX];

double solve() {
  double dp[MAX][MAX]; // dp[ターン数][どのマスにいるか]
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 1; i <= T; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < 6; k++) {
        if (dp[i - 1][j] == 0)
          continue;
        int next = j + (k + 1);
        if (next > N) {
          next = next - 2 * (next - N);
        }
        switch (arr[next]) {
        case 0:
          dp[i][next] += dp[i - 1][j] / 6;
          break;
        case 1:
          dp[i + 1][next] += dp[i - 1][j] / 6;
          break;
        case 2:
          dp[i][0] += dp[i - 1][j] / 6;
          break;
        }
      }
    }
  }
  double res = 0;
  for (int i = 1; i <= T; i++) {
    res += dp[i][N];
  }
  return res;
}

int main() {
  int in;
  while (cin >> N >> T >> L >> B, N) {
    memset(arr, 0, sizeof(arr));
    for (int i = 0; i < L; i++) {
      cin >> in;
      arr[in] = 1;
    }
    for (int i = 0; i < B; i++) {
      cin >> in;
      arr[in] = 2;
    }
    printf("%.8f\n", solve());
  }
  return 0;
}
Problem p00859 (C++)

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

typedef pair<int, int> P;

int n, m;
int a[110], b[110], w[110];
bool use[110];
vector<P> g[110];

int dfs(int x, int low, int up) {
  if (use[x])
    return 0;
  use[x] = true;

  int res = 0;
  for (int i = 0; i < g[x].size(); i++) {
    if (g[x][i].first >= low && g[x][i].first <= up)
      res += dfs(g[x][i].second, low, up);
  }
  return res + 1;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      g[i].clear();
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> w[i];
      a[i]--;
      b[i]--;
      g[a[i]].push_back(P(w[i], b[i]));
      g[b[i]].push_back(P(w[i], a[i]));
    }

    int l = -1, r = 10001;
    for (int i = 0; i < m; i++) {
      while (r - l > 1) {
        int mid = (l + r) / 2;
        for (int j = 0; j < n; j++)
          use[j] = false;
        int tmp = dfs(a[i], w[i] - mid, w[i]);
        if (tmp == n)
          r = mid;
        else
          l = mid;
      }
      l = -1;
    }
    if (r == 10001)
      cout << -1 << endl;
    else
      cout << r << endl;
  }
}


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

typedef pair<int, int> P;

int n, m;
int a[11000], b[11000], w[11000];
bool use[110];
vector<P> g[110];

int dfs(int x, int low, int up) {
  if (use[x])
    return 0;
  use[x] = true;

  int res = 0;
  for (int i = 0; i < g[x].size(); i++) {
    if (g[x][i].first >= low && g[x][i].first <= up)
      res += dfs(g[x][i].second, low, up);
  }
  return res + 1;
}

int main() {
  while (cin >> n >> m, n || m) {
    for (int i = 0; i < n; i++)
      g[i].clear();
    for (int i = 0; i < m; i++) {
      cin >> a[i] >> b[i] >> w[i];
      a[i]--;
      b[i]--;
      g[a[i]].push_back(P(w[i], b[i]));
      g[b[i]].push_back(P(w[i], a[i]));
    }

    int l = -1, r = 10001;
    for (int i = 0; i < m; i++) {
      while (r - l > 1) {
        int mid = (l + r) / 2;
        for (int j = 0; j < n; j++)
          use[j] = false;
        int tmp = dfs(a[i], w[i] - mid, w[i]);
        if (tmp == n)
          r = mid;
        else
          l = mid;
      }
      l = -1;
    }
    if (r == 10001)
      cout << -1 << endl;
    else
      cout << r << endl;
  }
}
Problem p00861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

map<char, int> declaration;
map<pair<char, int>, int> assignment;

int expression(string &s, int &idx, int now) {
  char array_name = s[idx];
  if (isalpha(array_name)) {
    ++idx;
    ++idx;
    int get = expression(s, idx, now);
    if (declaration[array_name] <= get)
      throw(now);
    if (!assignment.count({array_name, get}))
      throw(now);
    ++idx;
    return (assignment[{array_name, get}]);
  } else {
    int ret = 0;
    while (isdigit(s[idx])) {
      ret = ret * 10 - '0' + s[idx++];
    }
    return (ret);
  }
}

void exec(string &s, int now) {
  char array_name = s[0];

  if (s.find('=') == string::npos) {
    int number = 0, ptr = 2;
    while (isdigit(s[ptr])) {
      number = number * 10 - '0' + s[ptr++];
    }

    declaration.emplace(array_name, number);
  } else {
    int ptr = 2;
    int number = expression(s, ptr, now);
    if (declaration[array_name] <= number)
      throw(now);
    ++ptr;
    ++ptr;
    int get = expression(s, ptr, now);
    assignment[{array_name, number}] = get;
  }
}

int main() {
  string S[1000];
  while (cin >> S[0], S[0] != ".") {
    for (int i = 1; S[i - 1] != "."; ++i) {
      cin >> S[i];
    }

    try {
      for (int i = 0; S[i] != "."; i++) {
        exec(S[i], i + 1);
      }
      throw 0;
    } catch (int x) {
      cout << x << endl;
    }

    declaration.clear();
    assignment.clear();
  }
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

map<char, int> declaration;
map<pair<char, int>, int> assignment;

int expression(string &s, int &idx, int now) {
  char array_name = s[idx];
  if (isalpha(array_name)) {
    ++idx;
    ++idx;
    int get = expression(s, idx, now);
    if (declaration[array_name] <= get)
      throw(now);
    if (!assignment.count({array_name, get}))
      throw(now);
    ++idx;
    return (assignment[{array_name, get}]);
  } else {
    int ret = 0;
    while (isdigit(s[idx])) {
      ret = ret * 10 - '0' + s[idx++];
    }
    return (ret);
  }
}

void exec(string &s, int now) {
  char array_name = s[0];

  if (s.find('=') == string::npos) {
    int number = 0, ptr = 2;
    while (isdigit(s[ptr])) {
      number = number * 10 - '0' + s[ptr++];
    }

    declaration.emplace(array_name, number);
  } else {
    int ptr = 2;
    int number = expression(s, ptr, now);
    if (declaration[array_name] <= number)
      throw(now);
    ++ptr;
    ++ptr;
    int get = expression(s, ptr, now);
    assignment[{array_name, number}] = get;
  }
}

int main() {
  string S[1005];
  while (cin >> S[0], S[0] != ".") {
    for (int i = 1; S[i - 1] != "."; ++i) {
      cin >> S[i];
    }

    try {
      for (int i = 0; S[i] != "."; i++) {
        exec(S[i], i + 1);
      }
      throw 0;
    } catch (int x) {
      cout << x << endl;
    }

    declaration.clear();
    assignment.clear();
  }
}
Problem p00864 (C++)

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

int n, w;
int dat[100];
int maxi;
int l, r;
double calc() {
  double ans = 0;
  l = 0;
  for (int i = l; i <= r; ++i) {
    ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
  }
  return ans + 0.01;
}

int main() {
  while ((cin >> n >> w) && (n || w)) {
    fill(dat, dat + 101, 0);
    for (int i = 0; i < n; ++i) {
      int v;
      cin >> v;
      v /= w;
      ++dat[v];
      l = i == 0 ? v : min(l, v);
      r = i == 0 ? v : max(r, v);
      maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
    }
    cout << calc() << endl;
  }
  return 0;
}

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

int n, w;
int dat[101];
int maxi;
int l, r;
double calc() {
  double ans = 0;
  l = 0;
  for (int i = l; i <= r; ++i) {
    ans += ((double)(r - i) / (r - l)) * ((double)dat[i] / maxi);
  }
  return ans + 0.01;
}

int main() {
  while ((cin >> n >> w) && (n || w)) {
    fill(dat, dat + 101, 0);
    for (int i = 0; i < n; ++i) {
      int v;
      cin >> v;
      v /= w;
      ++dat[v];
      l = i == 0 ? v : min(l, v);
      r = i == 0 ? v : max(r, v);
      maxi = i == 0 ? dat[v] : max(maxi, dat[v]);
    }
    cout << calc() << endl;
  }
  return 0;
}
Problem p00864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>
#define max(a, b) a > b ? a : b

int main(void) {
  int n, w, v[100], i, j, count[11], num, max;
  double sum;
  while (1) {
    scanf("%d%d", &n, &w);
    max = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &v[i]);
      max = max(max, v[i]);
    }
    num = max / w + 1;
    memset(count, 0, sizeof(count));
    max = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < num; j++) {
        if (j * w <= v[i] && v[i] < (j + 1) * w)
          count[j]++;
      }
    }

    for (i = 0; i < num; i++)
      max = max(max, count[i]);
    sum = 0;
    if (num != 1) {
      for (i = 0; i < num; i++) {
        sum += ((double)(num - 1 - i) / (num - 1)) * ((double)count[i] / max);
      }
      printf("%.16f\n", sum + 0.01);
    } else
      printf("1.01\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>
#define max(a, b) a > b ? a : b

int main(void) {
  int n, w, v[100], i, j, count[11], num, max;
  double sum;
  while (1) {
    scanf("%d%d", &n, &w);
    if (n == 0 && w == 0)
      break;
    max = 0;
    for (i = 0; i < n; i++) {
      scanf("%d", &v[i]);
      max = max(max, v[i]);
    }
    num = max / w + 1;
    memset(count, 0, sizeof(count));
    max = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < num; j++) {
        if (j * w <= v[i] && v[i] < (j + 1) * w)
          count[j]++;
      }
    }

    for (i = 0; i < num; i++)
      max = max(max, count[i]);
    sum = 0;
    if (num != 1) {
      for (i = 0; i < num; i++) {
        sum += ((double)(num - 1 - i) / (num - 1)) * ((double)count[i] / max);
      }
      printf("%.16f\n", sum + 0.01);
    } else
      printf("1.01\n");
  }
  return 0;
}
Problem p00864 (C++)

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

#define LOG(x) // cerr << #x << " = " << x << endl

int main() {
  while (true) {
    int N, W;
    cin >> N >> W;
    if (N == 0 && W == 0)
      break;
    map<int, int> occ;
    for (int i = 0; i < N; ++i) {
      int v;
      cin >> v;
      occ[v / W]++;
    }
    int maxOcc = 0;
    int numBars = 0;
    for (auto &&iter : occ) {
      cerr << "bar[" << iter.first << "] = " << iter.second << endl;
      maxOcc = max(maxOcc, iter.second);
      numBars = max(numBars, iter.first + 1);
    }
    LOG(maxOcc);
    LOG(numBars);
    double ans = 0.01;
    for (auto &&iter : occ) {
      ans += (1.0 - (1.0 * iter.first / (numBars - 1))) *
             (1.0 * iter.second / maxOcc);
    }
    cout.setf(ios::fixed);
    cout.precision(6);
    cout << ans << endl;
  }
}

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

#define LOG(x) // cerr << #x << " = " << x << endl

int main() {
  while (true) {
    int N, W;
    cin >> N >> W;
    if (N == 0 && W == 0)
      break;
    map<int, int> occ;
    for (int i = 0; i < N; ++i) {
      int v;
      cin >> v;
      occ[v / W]++;
    }
    int maxOcc = 0;
    int numBars = 0;
    for (auto &&iter : occ) {
      maxOcc = max(maxOcc, iter.second);
      numBars = max(numBars, iter.first + 1);
    }
    LOG(maxOcc);
    LOG(numBars);
    double ans = 0.01;
    for (auto &&iter : occ) {
      ans += (1.0 - (1.0 * iter.first / (numBars - 1))) *
             (1.0 * iter.second / maxOcc);
    }
    cout.setf(ios::fixed);
    cout.precision(6);
    cout << ans << endl;
  }
}
Problem p00865 (C++)

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

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_SIZE = 1000006;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
  rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;

  rep(i, m) p[0][i + 1] = 1.0 / m;
  rep(i, n - 1) {
    rep(j, MAX_SIZE) {
      rep(k, m) {
        if (k + j + 1 >= MAX_SIZE)
          continue;
        p[1][k + j + 1] += p[0][j] / m;
      }
    }
    rep(j, MAX_SIZE) {
      p[0][j] = p[1][j];
      p[1][j] = 0.0;
    }
  }
  double ans = 0.0;

  rep(i, m * n) {
    int b = max(1, i - k + 1);
    // cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
    ans += p[0][i + 1] * b;
  }

  printf("%.8f\n", ans);
}

int main() {
  while (cin >> n >> m >> k) {
    if (n + m + k == 0)
      break;
    solve();
  }
  return 0;
}

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

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_SIZE = 100005;
int n, m, k;
double p[2][MAX_SIZE];
void solve() {
  rep(i, MAX_SIZE) p[0][i] = p[1][i] = 0.0;

  rep(i, m) p[0][i + 1] = 1.0 / m;
  rep(i, n - 1) {
    rep(j, MAX_SIZE) {
      rep(k, m) {
        if (k + j + 1 >= MAX_SIZE)
          continue;
        p[1][k + j + 1] += p[0][j] / m;
      }
    }
    rep(j, MAX_SIZE) {
      p[0][j] = p[1][j];
      p[1][j] = 0.0;
    }
  }
  double ans = 0.0;

  rep(i, m * n) {
    int b = max(1, i - k + 1);
    // cout<<i+1<<" : "<<p[0][i+1]<<","<<b<<endl;
    ans += p[0][i + 1] * b;
  }

  printf("%.8f\n", ans);
}

int main() {
  while (cin >> n >> m >> k) {
    if (n + m + k == 0)
      break;
    solve();
  }
  return 0;
}
Problem p00865 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 1000
#define MAXM 10000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率

int n, m, k;

int main(void) {
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 && k == 0)
      break;
    for (int i = 0; i < MAXN; i++)
      for (int j = 0; j < MAXM; j++)
        dp[i][j] = 0;

    dp[0][0] = 1;
    int ma = n * m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= ma; j++) {
        for (int k = 1; k <= m; k++) {
          if (j - k >= 0) {
            dp[i][j] += dp[i - 1][j - k] / m;
          }
        }
      }
    }
    // for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
    double ans = 0;
    for (int i = 1; i <= ma; i++) {
      if (i <= k)
        ans += 1.0 * dp[n][i];
      else
        ans += (i - k) * dp[n][i];
    }
    printf("%.10f\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAXN 100
#define MAXM 10000

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

double dp[MAXN][MAXM]; // dp[n][m]はn回サイコロをふったとき合計値がmになる確率

int n, m, k;

int main(void) {
  while (cin >> n >> m >> k) {
    if (n == 0 && m == 0 && k == 0)
      break;
    for (int i = 0; i < MAXN; i++)
      for (int j = 0; j < MAXM; j++)
        dp[i][j] = 0;

    dp[0][0] = 1;
    int ma = n * m;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= ma; j++) {
        for (int k = 1; k <= m; k++) {
          if (j - k >= 0) {
            dp[i][j] += dp[i - 1][j - k] / m;
          }
        }
      }
    }
    // for (int i = 0; i <= ma; i++) cout << dp[n][i] << endl;
    double ans = 0;
    for (int i = 1; i <= ma; i++) {
      if (i <= k)
        ans += 1.0 * dp[n][i];
      else
        ans += (i - k) * dp[n][i];
    }
    printf("%.10f\n", ans);
  }
  return 0;
}
Problem p00865 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 5000000;
double dp[N], dp2[N];

int main() {
  int n, m, k;
  while (cin >> n >> m >> k, n | m | k) {
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    rep(i, n) {
      memset(dp2, 0, sizeof(dp2));

      for (int j = i; j <= m * i; ++j) {
        for (int k = 1; k <= m; ++k) {
          if (j + k >= N)
            break;
          dp2[j + k] += dp[j] / m;
        }
      }

      rep(j, N) dp[j] = dp2[j];
    }

    double ans = 0;
    rep(i, N) ans += max(1, i - k) * dp[i];

    printf("%.10lf\n", ans);
  }
  return 0;
}

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

typedef long long ll;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define each(itr, c)                                                           \
  for (__typeof(c.begin()) itr = c.begin(); itr != c.end(); ++itr)
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

const int N = 1000000;
double dp[N], dp2[N];

int main() {
  int n, m, k;
  while (cin >> n >> m >> k, n | m | k) {
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    rep(i, n) {
      memset(dp2, 0, sizeof(dp2));

      for (int j = i; j <= m * i; ++j) {
        for (int k = 1; k <= m; ++k) {
          if (j + k >= N)
            break;
          dp2[j + k] += dp[j] / m;
        }
      }

      rep(j, N) dp[j] = dp2[j];
    }

    double ans = 0;
    rep(i, N) ans += max(1, i - k) * dp[i];

    printf("%.10lf\n", ans);
  }
  return 0;
}
Problem p00865 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define first fi
#define second se
using namespace std;
typedef pair<int, int> P;

int d[30][300010];

signed main() {
  int n, m, k;
  while (1) {
    cin >> n >> m >> k;
    if (n + m + k == 0)
      break;
    rep(i, 0, 30) rep(j, 0, 300010) d[i][j] = 0;
    d[0][0] = 1;
    rep(i, 0, n) {
      rep(j, i, 300000) {
        if (d[i][j] == 0)
          break;
        rep(l, 1, m + 1) { d[i + 1][j + l] += d[i][j]; }
      }
    }
    double sum = 0;
    rep(i, 0, 300000) {
      if (i <= k)
        sum += d[n][i];
      else
        sum += d[n][i] * (i - k);
    }
    rep(i, 0, n) sum /= (double)m;
    printf("%.14lf\n", sum);
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
// #define int long long
#define first fi
#define second se
using namespace std;
typedef pair<int, int> P;

int d[30][300010];

signed main() {
  int n, m, k;
  while (1) {
    cin >> n >> m >> k;
    if (n + m + k == 0)
      break;
    rep(i, 0, 30) rep(j, 0, 300010) d[i][j] = 0;
    d[0][0] = 1;
    rep(i, 0, n) {
      rep(j, i, 300000) {
        if (d[i][j] == 0)
          break;
        rep(l, 1, m + 1) { d[i + 1][j + l] += d[i][j]; }
      }
    }
    double sum = 0;
    rep(i, 0, 300000) {
      if (i <= k)
        sum += d[n][i];
      else
        sum += d[n][i] * (i - k);
    }
    rep(i, 0, n) sum /= (double)m;
    printf("%.14lf\n", sum);
  }
}
Problem p00865 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using LL = long long;

int main(void) {
  cout.precision(15);
  cout << fixed;
  for (;;) {
    int n, m, k;
    cin >> n >> m >> k;
    if (!n)
      break;
    // dp(i,j):=i??????m??¢???????????§j?????????????????°
    // dp[i][j] = dp[i-1][j-k](k=1~m)
    map<pair<int, int>, LL> dp;
    dp[{0, 0}] = 1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j <= i * m; ++j) {
        for (int kk = 1; kk <= m; ++kk)
          dp[{i, j}] += dp[{i - 1, j - kk}];
      }
    }
    LL ans = 0;
    LL div = 0;
    for (int j = 0; j <= n * m; ++j) {
      int score = max(1, j - k);
      ans += dp[{n, j}] * score;
      div += dp[{n, j}];
    }
    cout << (double)ans / div << endl;
  }
  return 0;
}

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

using LL = long long;

int main(void) {
  cout.precision(15);
  cout << fixed;
  for (;;) {
    int n, m, k;
    cin >> n >> m >> k;
    if (!n)
      break;
    // dp(i,j):=i??????m??¢???????????§j?????????????????°
    // dp[i][j] = dp[i-1][j-k](k=1~m)
    map<pair<int, int>, LL> dp;
    if (n == 1) {
      for (int kk = 1; kk <= m; ++kk) {
        dp[{1, kk}] = 1;
      }
    } else {
      dp[{0, 0}] = 1;
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j <= i * m; ++j) {
          for (int kk = 1; kk <= m; ++kk)
            dp[{i, j}] += dp[{i - 1, j - kk}];
        }
      }
    }
    LL ans = 0;
    LL div = 0;
    for (int j = 0; j <= n * m; ++j) {
      int score = max(1, j - k);
      ans += dp[{n, j}] * score;
      div += dp[{n, j}];
    }
    cout << (double)ans / div << endl;
  }
  return 0;
}
Problem p00870 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;

bool dp[(1 << N)][S];
int n, m;
string s[N], t;

ull d1[N], d2[S], A[30];

int main() {

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> s[i];

    t = "";

    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      t += tmp;
    }

    for (int i = 0; i < n; i++) {
      ull hash = s[i][0];
      for (int j = 1; j < s[i].size(); j++)
        hash = hash * M + s[i][j];
      d1[i] = hash;
    }

    d2[0] = t[0];
    for (int i = 1; i < t.size(); i++)
      d2[i] = d2[i - 1] * M + t[i];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < t.size(); i++)
      dp[0][i] = true;

    A[0] = 1;
    for (int i = 1; i < 30; i++)
      A[i] = A[i - 1] * M;

    for (int i = 0; i < (1 << n); i++) {

      for (int j = 0; j < t.size(); j++) {

        if (!dp[i][j])
          continue;

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

          if (i & (1 << k))
            continue;

          int slen = s[k].size();

          if (t.size() < j + slen)
            continue;

          ull x = d2[j + A[slen] - 1];
          if (j)
            x -= d2[j - 1] * A[slen];

          if (d1[k] != x)
            continue;

          dp[i | (1 << k)][j + slen] = true;
        }
      }
    }

    int ans = 0;

    for (int i = 0; i <= t.size(); i++)
      ans += dp[(1 << n) - 1][i];

    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define N 12
#define S 5005
#define M 1777771
using namespace std;
typedef unsigned long long ull;

bool dp[(1 << N)][S];
int n, m;
string s[N], t;

ull d1[N], d2[S], A[30];

int main() {

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    for (int i = 0; i < n; i++)
      cin >> s[i];

    t = "";

    for (int i = 0; i < m; i++) {
      string tmp;
      cin >> tmp;
      t += tmp;
    }

    for (int i = 0; i < n; i++) {
      ull hash = s[i][0];
      for (int j = 1; j < s[i].size(); j++)
        hash = hash * M + s[i][j];
      d1[i] = hash;
    }

    d2[0] = t[0];
    for (int i = 1; i < t.size(); i++)
      d2[i] = d2[i - 1] * M + t[i];

    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < t.size(); i++)
      dp[0][i] = true;

    A[0] = 1;
    for (int i = 1; i < 30; i++)
      A[i] = A[i - 1] * M;

    for (int i = 0; i < (1 << n); i++) {

      for (int j = 0; j < t.size(); j++) {

        if (!dp[i][j])
          continue;

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

          if (i & (1 << k))
            continue;

          int slen = s[k].size();

          if (t.size() < j + slen)
            continue;

          ull x = d2[j + slen - 1];
          if (j)
            x -= d2[j - 1] * A[slen];

          if (d1[k] != x)
            continue;

          dp[i | (1 << k)][j + slen] = true;
        }
      }
    }

    int ans = 0;

    for (int i = 0; i <= t.size(); i++)
      ans += dp[(1 << n) - 1][i];

    cout << ans << endl;
  }

  return 0;
}
Problem p00875 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
  // cout << v << endl;
  if (v == t) {
    res = min(res, c);
    return;
  }
  if (v.size() >= t.size() || c >= res)
    return;
  for (int i = 0; i < n; i++) {
    memset(used, 0, sizeof(used));
    bool flag = false;
    for (int j = 0; j <= v.size() - a[i].size(); j++) {
      if (v.substr(j, a[i].size()) == a[i]) {
        used[j] = true;
        j += a[i].size() - 1;
        flag = true;
      }
    }
    if (!flag)
      continue;
    string next = "";
    for (int j = 0; j < v.size(); j++) {
      if (!used[j])
        next += v[j];
      else {
        next += b[i];
        j += a[i].size() - 1;
      }
    }
    dfs(next, c + 1);
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> f;
    cin >> t;
    res = t.size() - f.size() + 1;
    dfs(f, 0);
    printf("%d\n", res == t.size() - f.size() + 1 ? -1 : res);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int n;
string a[21], b[21];
string f, t;
bool used[21];
int res;
void dfs(string v, int c) {
  // cout << v << endl;
  if (v == t) {
    res = min(res, c);
    return;
  }
  if (v.size() >= t.size() || c >= res)
    return;
  for (int i = 0; i < n; i++) {
    memset(used, 0, sizeof(used));
    bool flag = false;
    if (v.size() < a[i].size())
      continue;
    for (int j = 0; j <= v.size() - a[i].size(); j++) {
      if (v.substr(j, a[i].size()) == a[i]) {
        used[j] = true;
        j += a[i].size() - 1;
        flag = true;
      }
    }
    if (!flag)
      continue;
    string next = "";
    for (int j = 0; j < v.size(); j++) {
      if (!used[j])
        next += v[j];
      else {
        next += b[i];
        j += a[i].size() - 1;
      }
    }
    dfs(next, c + 1);
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> f;
    cin >> t;
    res = t.size() - f.size() + 1;
    dfs(f, 0);
    printf("%d\n", res == t.size() - f.size() + 1 ? -1 : res);
  }
  return 0;
}
Problem p00875 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
  string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;

void saiki(string y, int kai) {
  cnt++;
  // cout << y << endl;
  if (y == goal && ans > kai)
    ans = kai;

  if (ans > kai && y.size() < 25)
    for (int i = 0; i < n; i++) {
      string ima = y;
      for (int j = 0; j < ima.size(); j++) {
        if (str[i].a.size() > ima.size() - j)
          break;
        int flg = 0;
        for (int k = 0; k < str[i].a.size(); k++) {
          if (str[i].a[k] != ima[j + k]) {
            flg = 1;
            break;
          }
        }
        if (flg == 0) {
          string nx;
          int nj;
          for (int k = 0; k < ima.size(); k++) {
            if (k == j)
              nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
            else
              nx += ima[k];
          }
          j = nj;
          ima = nx;
        }
      }
      if (y != ima && memo.count(ima) == 0) {
        memo.insert(ima);
        saiki(ima, kai + 1);
      }
    }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;

    str.resize(n);
    for (int i = 0; i < n; i++)
      cin >> str[i].a >> str[i].b;

    memo.clear();
    string start;
    cin >> start;
    cin >> goal;

    ans = 10000000;
    saiki(start, 0);
    if (ans == 10000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
    // cout <<cnt <<endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct po {
  string a, b;
};
int n;
vector<po> str;
int ans;
string goal;
int cnt = 0;
set<string> memo;

void saiki(string y, int kai) {
  cnt++;
  // cout << y << endl;
  if (y == goal && ans > kai)
    ans = kai;

  if (ans > kai && y.size() < 11)
    for (int i = 0; i < n; i++) {
      string ima = y;
      for (int j = 0; j < ima.size(); j++) {
        if (str[i].a.size() > ima.size() - j)
          break;
        int flg = 0;
        for (int k = 0; k < str[i].a.size(); k++) {
          if (str[i].a[k] != ima[j + k]) {
            flg = 1;
            break;
          }
        }
        if (flg == 0) {
          string nx;
          int nj;
          for (int k = 0; k < ima.size(); k++) {
            if (k == j)
              nx += str[i].b, k += str[i].a.size() - 1, nj = nx.size() - 1;
            else
              nx += ima[k];
          }
          j = nj;
          ima = nx;
        }
      }
      if (y != ima && memo.count(ima) == 0) {
        memo.insert(ima);
        saiki(ima, kai + 1);
      }
    }
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;

    str.resize(n);
    for (int i = 0; i < n; i++)
      cin >> str[i].a >> str[i].b;

    memo.clear();
    string start;
    cin >> start;
    cin >> goal;

    ans = 10000000;
    saiki(start, 0);
    if (ans == 10000000)
      cout << -1 << endl;
    else
      cout << ans << endl;
    // cout <<cnt <<endl;
  }

  return 0;
}
Problem p00875 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string sed(string s, string a, string b) {
  int n = s.size();
  int m = a.size();
  string ret;

  for (int i = 0; i < n; ++i) {
    if (i + m <= n && s.substr(i, m) == a) {
      ret += b;
      i += m - 1;
    } else {
      ret += s[i];
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    string s, t;
    cin >> s >> t;

    int ret = 0;
    set<string> curr;
    curr.insert(s);
    bool ng = true;
    while (!curr.empty()) {
      set<string> next;
      set<string>::iterator it;
      for (it = curr.begin(); it != curr.end(); ++it) {
        if (*it == t) {
          next.clear();
          ng = false;
          cout << ret << endl;
          break;
        }
        for (int i = 0; i < n; ++i) {
          string x = sed(*it, a[i], b[i]);
          if (x.size() <= t.size())
            next.insert(x);
        }
      }
      curr.swap(next);
      ++ret;
    }

    if (ng)
      cout << -1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string sed(string s, string a, string b) {
  int n = s.size();
  int m = a.size();
  string ret;

  for (int i = 0; i < n; ++i) {
    if (i + m <= n && s.substr(i, m) == a) {
      ret += b;
      i += m - 1;
    } else {
      ret += s[i];
    }
  }

  return ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<string> a(n), b(n);
    for (int i = 0; i < n; ++i)
      cin >> a[i] >> b[i];

    string s, t;
    cin >> s >> t;

    int ret = 0;
    set<string> curr;
    curr.insert(s);
    bool ng = true;
    while (!curr.empty()) {
      set<string> next;
      set<string>::iterator it;
      for (it = curr.begin(); it != curr.end(); ++it) {
        if (*it == t) {
          next.clear();
          ng = false;
          cout << ret << endl;
          break;
        }
        for (int i = 0; i < n; ++i) {
          string x = sed(*it, a[i], b[i]);
          if (x != *it && x.size() <= t.size())
            next.insert(x);
        }
      }
      curr.swap(next);
      ++ret;
    }

    if (ng)
      cout << -1 << endl;
  }
}