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

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> S >> T;
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(S, 0));
    while (!Q.empty()) {
      pair<string, int> U = Q.front();
      Q.pop();
      string W = U.first;
      if (T == W) {
        cout << U.second << endl;
        goto E;
      }
      for (int i = 0; i < n; i++) {
        string V = W;
        for (int j = 0; j <= V.size() - a[i].size(); j++) {
          if (V.substr(j, a[i].size()) == a[i]) {
            V = V.substr(0, j) + b[i] +
                V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
            j += b[i].size() - 1;
          }
        }
        if (W != V && V.size() <= T.size()) {
          Q.push(make_pair(V, U.second + 1));
        }
      }
    }
    cout << "-1" << endl;
  E:;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int n;
string a[100], b[100], S, T;
queue<pair<string, int>> Q;
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> b[i];
    cin >> S >> T;
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(S, 0));
    while (!Q.empty()) {
      pair<string, int> U = Q.front();
      Q.pop();
      string W = U.first;
      if (T == W) {
        cout << U.second << endl;
        goto E;
      }
      for (int i = 0; i < n; i++) {
        string V = W;
        for (int j = 0; j <= (int)V.size() - (int)a[i].size(); j++) {
          if (V.substr(j, a[i].size()) == a[i]) {
            V = V.substr(0, j) + b[i] +
                V.substr(j + a[i].size(), V.size() - (j + a[i].size()));
            j += b[i].size() - 1;
          }
        }
        if (W != V && V.size() <= T.size()) {
          Q.push(make_pair(V, U.second + 1));
        }
      }
    }
    cout << "-1" << endl;
  E:;
  }
  return 0;
}
Problem p00881 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
vi v;
int m, n;
int dp[1 << 12][1 << 12];

int solve(int asked, int resp) {
  if (dp[asked][resp] >= 0)
    return dp[asked][resp];
  int cnt = 0;
  REP(i, n) {
    if ((v[i] & asked) == resp)
      cnt++;
  }
  int res = m;
  if (cnt <= 1) {
    res = 0;
  } else {
    REP(i, m) {
      int nx = asked | (1 << i);
      if (nx == asked)
        continue;
      chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
    }
  }
  return dp[asked][resp] = res;
}

int tonum(string s) {
  int res = 0;
  REP(i, s.size()) {
    res *= 2;
    res += s[i] - '0';
  }
  return res;
}

int main() {
  while (cin >> m >> n, m) {
    MS(dp, -1);
    v.resize(n);
    REP(i, n) {
      string s;
      cin >> s;
      v[i] = tonum(s);
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
vi v;
int m, n;
short dp[1 << 12][1 << 12];

int solve(int asked, int resp) {
  if (dp[asked][resp] >= 0)
    return dp[asked][resp];
  int cnt = 0;
  REP(i, n) {
    if ((v[i] & asked) == resp)
      cnt++;
  }
  int res = m;
  if (cnt <= 1) {
    res = 0;
  } else {
    REP(i, m) {
      int nx = asked | (1 << i);
      if (nx == asked)
        continue;
      chmin(res, max(solve(nx, resp | (1 << i)), solve(nx, resp)) + 1);
    }
  }
  return dp[asked][resp] = res;
}

int tonum(string s) {
  int res = 0;
  REP(i, s.size()) {
    res *= 2;
    res += s[i] - '0';
  }
  return res;
}

int main() {
  while (cin >> m >> n, m) {
    MS(dp, -1);
    v.resize(n);
    REP(i, n) {
      string s;
      cin >> s;
      v[i] = tonum(s);
    }
    cout << solve(0, 0) << endl;
  }
  return 0;
}
Problem p00881 (C++)

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

int w, h, dp[3000][3000];
int bit[200];
bool t[200][200];

int solve(int q, int a) {
  if (dp[q][a] != -1)
    return dp[q][a];

  int cnt = 0;

  for (int i = 0; i < h; i++) {
    if ((bit[i] & q) == a) {
      cnt++;
      if (cnt > 1)
        break;
    }
  }

  if (cnt == 1) {
    return 0;
  }

  int res = INT_MAX;

  for (int i = 0; i < w; i++) {
    if (!(q & (1 << i))) {
      int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
      res = min(res, tmp);
    }
  }

  return dp[q][a] = res + 1;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      bit[i] = 0;

      for (int j = 0; j < w; j++) {
        char ch;
        cin >> ch;
        t[i][j] = ch - '0';
        bit[i] = (bit[i] << 1) + t[i][j];
      }
    }

    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0) << endl;
  }
}

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

int w, h;
short dp[1 << 11][1 << 11];
int bit[200];
bool t[200][200];

int solve(int q, int a) {
  if (dp[q][a] != -1)
    return dp[q][a];

  int cnt = 0;

  for (int i = 0; i < h; i++) {
    if ((bit[i] & q) == a) {
      cnt++;
      if (cnt > 1)
        break;
    }
  }

  if (cnt == 1) {
    return 0;
  }

  int res = INT_MAX;

  for (int i = 0; i < w; i++) {
    if (!(q & (1 << i))) {
      int tmp = max(solve(q | (1 << i), a), solve(q | (1 << i), a | (1 << i)));
      res = min(res, tmp);
    }
  }

  return dp[q][a] = res + 1;
}

int main() {
  while (cin >> w >> h, w || h) {
    for (int i = 0; i < h; i++) {
      bit[i] = 0;

      for (int j = 0; j < w; j++) {
        char ch;
        cin >> ch;
        t[i][j] = ch - '0';
        bit[i] = (bit[i] << 1) + t[i][j];
      }
    }

    memset(dp, -1, sizeof(dp));
    cout << solve(0, 0) << endl;
  }
}
Problem p00881 (C++)

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

int m, n;
int ans[15];

int dp[2050][2050]; // S?????????????????£??????

using Set = bitset<12>;

int rec(int asked,
        int answer) { // ??????????????????aked???answer??¨??????????????????
  int &res = dp[asked][answer];
  if (res != -1)
    return res;
  res = m;

  // vector<int> a;
  {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((ans[i] & asked) == answer) {
        ++cnt;
      }
    }
    if (cnt <= 1)
      return res = 0;
  }

  // cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
  // cout << "B ";
  // for(int &x : a) cout << Set(ans[x]) << ' ';
  // cout << endl;

  for (int i = 0; i < m; ++i) {
    if (asked >> i & 1)
      continue;
    int cnt[2] = {};
    for (int j = 0; j < n; ++j) {
      if ((ans[j] & asked) == answer) {
        ++cnt[ans[i] >> j & 1];
      }
    }
    // if(cnt[0] == 0 || cnt[1] == 0) continue;
    int tmp = 0;
    for (int j = 0; j < 2; ++j) {
      tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
    }
    res = min(res, tmp);
  }

  // assert(res != 1e9);
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> m >> n && m) {
    for (int i = 0; i < n; ++i) {
      char buf[100];
      cin >> buf;
      ans[i] = 0;
      for (int j = 0; j < m; ++j) {
        if (buf[j] - '0')
          ans[i] |= 1 << j;
      }
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0) << endl;
    // cout << "=================" << endl;
  }
}

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

int m, n;
int ans[150];

int dp[2050][2050]; // S?????????????????£??????

using Set = bitset<12>;

int rec(int asked,
        int answer) { // ??????????????????aked???answer??¨??????????????????
  int &res = dp[asked][answer];
  if (res != -1)
    return res;
  res = m;

  // vector<int> a;
  {
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
      if ((ans[i] & asked) == answer) {
        ++cnt;
      }
    }
    if (cnt <= 1)
      return res = 0;
  }

  // cout << "A " << Set(asked) << ' ' << Set(answer) << endl;
  // cout << "B ";
  // for(int &x : a) cout << Set(ans[x]) << ' ';
  // cout << endl;

  for (int i = 0; i < m; ++i) {
    if (asked >> i & 1)
      continue;
    int cnt[2] = {};
    for (int j = 0; j < n; ++j) {
      if ((ans[j] & asked) == answer) {
        ++cnt[ans[i] >> j & 1];
      }
    }
    // if(cnt[0] == 0 || cnt[1] == 0) continue;
    int tmp = 0;
    for (int j = 0; j < 2; ++j) {
      tmp = max(tmp, 1 + rec(asked | 1 << i, answer | (j << i)));
    }
    res = min(res, tmp);
  }

  // assert(res != 1e9);
  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  while (cin >> m >> n && m) {
    for (int i = 0; i < n; ++i) {
      char buf[100];
      cin >> buf;
      ans[i] = 0;
      for (int j = 0; j < m; ++j) {
        if (buf[j] - '0')
          ans[i] |= 1 << j;
      }
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0) << endl;
    // cout << "=================" << endl;
  }
}
Problem p00881 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;

bool issame(string a, string b) {
  for (int i = 0; i < (int)a.length(); i++) {
    if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

int solve(string &str, vector<string> &s, map<string, int> &mincost) {
  if (mincost.count(str) != 0) {
    return mincost[str];
  }
  int n = s.size();
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (issame(s[i], str))
      count++;
  }
  if (count <= 1) {
    mincost[str] = 0;
    return 0;
  }

  int m = str.length();
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (str[i] == 'x') {
      string tmpstr[2] = {str, str};
      tmpstr[0][i] = '0';
      tmpstr[1][i] = '1';
      res = min(
          res,
          max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
    }
  }
  mincost[str] = res;
  return res;
}

int main() {
  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      break;

    vector<string> s(n);
    for (int i = 0; i < n; i++)
      cin >> s[i];
    map<string, int> mincost;
    string ini = string(m, 'x');
    cout << solve(ini, s, mincost) << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;

bool issame(string &a, string &b) {
  for (int i = 0; i < (int)a.length(); i++) {
    if (a[i] != 'x' && b[i] != 'x' && a[i] != b[i]) {
      return false;
    }
  }
  return true;
}

int solve(string &str, vector<string> &s, map<string, int> &mincost) {
  if (mincost.count(str) != 0) {
    return mincost[str];
  }
  int n = s.size();
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (issame(s[i], str))
      count++;
  }
  if (count <= 1) {
    mincost[str] = 0;
    return 0;
  }

  int m = str.length();
  int res = inf;
  for (int i = 0; i < m; i++) {
    if (str[i] == 'x') {
      string tmpstr[2] = {str, str};
      tmpstr[0][i] = '0';
      tmpstr[1][i] = '1';
      res = min(
          res,
          max(solve(tmpstr[0], s, mincost), solve(tmpstr[1], s, mincost)) + 1);
    }
  }
  mincost[str] = res;
  return res;
}

int main() {
  while (1) {
    int m, n;
    cin >> m >> n;
    if (m == 0)
      break;

    vector<string> s(n);
    for (int i = 0; i < n; i++)
      cin >> s[i];
    map<string, int> mincost;
    string ini = string(m, 'x');
    cout << solve(ini, s, mincost) << endl;
  }
  return 0;
}
Problem p00881 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int m, n, in[100];
int dp[1600000], pw[13];

int rec(int bit) {
  int &res = dp[bit];
  if (res >= 0)
    return res;

  int t[13] = {}, cnt = 0;
  rep(j, m) t[j] = bit / pw[j] % 3;
  rep(i, n) {
    bool ok = 1;
    rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
      ok = 0;
      break;
    }
    if (ok)
      cnt++;
  }
  if (cnt <= 1)
    return res = 0;
  res = inf;
  rep(i, m) if (t[i] == 2) {
    res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
  }
  return res;
}
int main() {
  pw[0] = 1;
  rep(i, 12) pw[i + 1] = pw[i] * 3;
  while (cin >> m >> n, m) {
    rep(i, n) {
      char buf[20];
      cin >> buf;
      in[i] = 0;
      rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(pw[m] - 1) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

int m, n, in[128];
int dp[1600000], pw[13];

int rec(int bit) {
  int &res = dp[bit];
  if (res >= 0)
    return res;

  int t[13] = {}, cnt = 0;
  rep(j, m) t[j] = bit / pw[j] % 3;
  rep(i, n) {
    bool ok = 1;
    rep(j, m) if (t[j] < 2 && (in[i] >> j & 1) != t[j]) {
      ok = 0;
      break;
    }
    if (ok)
      cnt++;
  }
  if (cnt <= 1)
    return res = 0;
  res = inf;
  rep(i, m) if (t[i] == 2) {
    res = min(res, 1 + max(rec(bit - 2 * pw[i]), rec(bit - pw[i])));
  }
  return res;
}
int main() {
  pw[0] = 1;
  rep(i, 12) pw[i + 1] = pw[i] * 3;
  while (cin >> m >> n, m) {
    rep(i, n) {
      char buf[20];
      cin >> buf;
      in[i] = 0;
      rep(j, m) if (buf[j] == '1') in[i] |= 1 << j;
    }
    memset(dp, -1, sizeof(dp));
    cout << rec(pw[m] - 1) << endl;
  }
  return 0;
}
Problem p00884 (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;

int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;

void solve(string s) {
  int k = find(group.begin(), group.end(), s) - group.begin();
  if (k == n) {
    ret.insert(s);
    return;
  }

  for (unsigned i = 0; i < member[k].size(); ++i)
    solve(member[k][i]);
}

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

    group.assign(n, "");
    member.assign(n, vector<string>(0));

    for (int i = 0; i < n; ++i) {
      char c;
      for (;;) {
        cin >> c;
        if (c == ':')
          break;
        group[i] += c;
      }
      string s;
      for (;;) {
        cin >> c;
        if (c == '.') {
          member[i].push_back(s);
          break;
        }
        if (c == ',') {
          member[i].push_back(s);
          s.clear();
        } else {
          s += c;
        }
      }
    }

    ret.clear();
    solve(group[0]);
    cout << ret.size() << 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;

int n;
vector<string> group;
vector<vector<string>> member;
set<string> ret;

void solve(string s) {
  int k = find(group.begin(), group.end(), s) - group.begin();
  if (k == n) {
    ret.insert(s);
    return;
  }

  for (unsigned i = 0; i < member[k].size(); ++i)
    solve(member[k][i]);
  member[k].clear();
}

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

    group.assign(n, "");
    member.assign(n, vector<string>(0));

    for (int i = 0; i < n; ++i) {
      char c;
      for (;;) {
        cin >> c;
        if (c == ':')
          break;
        group[i] += c;
      }
      string s;
      for (;;) {
        cin >> c;
        if (c == '.') {
          member[i].push_back(s);
          break;
        }
        if (c == ',') {
          member[i].push_back(s);
          s.clear();
        } else {
          s += c;
        }
      }
    }

    ret.clear();
    solve(group[0]);
    cout << ret.size() << endl;
  }
}
Problem p00884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
  vector<string> vec;
  string s;

  for (int i = 0; i < str.size(); i++) {
    s = "";
    while (i < str.size() && isalpha(str[i]))
      s += str[i++];

    if (s.size())
      vec.push_back(s);
  }

  return vec;
}

int search(string tg) {
  int ln = 0;
  vector<string> vec = grp[tg];
  prn[tg] = false;

  for (int i = 0; i < vec.size(); i++) {
    if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
      ln += search(vec[i]);
    else if (!prn[vec[i]]) {
      ln++;
      prn[vec[i]] = true;
    }
  }

  return ln;
}

int main() {
  int n;
  string str, tg;

  while (cin >> n && n) {
    bool f = true;
    while (n--) {
      cin >> str;
      vector<string> vec = split(str);
      str = vec[0];
      vec.erase(vec.begin());
      grp[str] = vec;

      if (f) {
        tg = str;
        f = false;
      }
    }

    cout << search(tg) << endl;
    grp.clear();
    prn.clear();
  }

  return 0;
}

Fixed submission:
#include <cctype>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, vector<string>> grp;
map<string, bool> prn;
vector<string> split(string str) {
  vector<string> vec;
  string s;

  for (int i = 0; i < str.size(); i++) {
    s = "";
    while (i < str.size() && isalpha(str[i]))
      s += str[i++];

    if (s.size())
      vec.push_back(s);
  }

  return vec;
}

int search(string tg) {
  int ln = 0;
  vector<string> vec = grp[tg];
  prn[tg] = true;

  for (int i = 0; i < vec.size(); i++) {
    if (!prn[vec[i]] && grp.find(vec[i]) != grp.end())
      ln += search(vec[i]);
    else if (!prn[vec[i]]) {
      ln++;
      prn[vec[i]] = true;
    }
  }

  return ln;
}

int main() {
  int n;
  string str, tg;

  while (cin >> n && n) {
    bool f = true;
    while (n--) {
      cin >> str;
      vector<string> vec = split(str);
      str = vec[0];
      vec.erase(vec.begin());
      grp[str] = vec;

      if (f) {
        tg = str;
        f = false;
      }
    }

    cout << search(tg) << endl;
    grp.clear();
    prn.clear();
  }

  return 0;
}
Problem p00884 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn = 110;
int n, cnt, ans;
bool vis[maxn];
map<string, int> mp;
vector<int> G[maxn];
void init() {
  for (int i = 0; i < maxn; i++)
    G[i].clear();
  mp.clear();
  memset(vis, 0, sizeof(vis));
  cnt = ans = 0;
}
int get_ID(string s) {
  if (!mp.count(s))
    return mp[s] = ++cnt;
  else
    return mp[s];
}
void dfs(int u) {
  vis[u] = 1;
  if (G[u].size() == 0)
    ans++;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  string s;
  while (~scanf("%d", &n) && n) {
    init();
    for (int i = 0; i < n; i++) {
      cin >> s;
      int last = 0, now = 0;
      while (now < s.size()) {
        int t, v;
        if (s[now] == ':') {
          v = get_ID(s.substr(last, now - last));
          last = ++now;
        }
        if (s[now] == ',' || s[now] == '.') {
          t = get_ID(s.substr(last, now - last));
          G[v].push_back(t);
          last = ++now;
        }
        now++;
      }
    }
    dfs(1);
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int maxn = 112345;
int n, cnt, ans;
bool vis[maxn];
map<string, int> mp;
vector<int> G[maxn];
void init() {
  for (int i = 0; i < maxn; i++)
    G[i].clear();
  mp.clear();
  memset(vis, 0, sizeof(vis));
  cnt = ans = 0;
}
int get_ID(string s) {
  if (!mp.count(s))
    return mp[s] = ++cnt;
  else
    return mp[s];
}
void dfs(int u) {
  vis[u] = 1;
  if (G[u].size() == 0)
    ans++;
  for (int i = 0; i < G[u].size(); i++) {
    int v = G[u][i];
    if (!vis[v]) {
      dfs(v);
    }
  }
}
int main() {
  string s;
  while (~scanf("%d", &n) && n) {
    init();
    for (int i = 0; i < n; i++) {
      cin >> s;
      int last = 0, now = 0;
      while (now < s.size()) {
        int t, v;
        if (s[now] == ':') {
          v = get_ID(s.substr(last, now - last));
          last = ++now;
        }
        if (s[now] == ',' || s[now] == '.') {
          t = get_ID(s.substr(last, now - last));
          G[v].push_back(t);
          last = ++now;
        }
        now++;
      }
    }
    dfs(1);
    printf("%d\n", ans);
  }
  return 0;
}
Problem p00884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

struct Edge {
  int src, dst;
  Edge() {}
  Edge(int s, int d) : src(s), dst(d) {}
};
typedef vector<vector<Edge>> Graph;

void dfs(const Graph &g, int v, vector<int> &flg) {
  flg[v] = 1;
  for (Edge e : g[v])
    dfs(g, e.dst, flg);
}

int main() {
  for (int n; cin >> n, n;) {
    map<string, int> ntoi;
    Graph g;
    for (int i = 0; i < n; i++) {
      string line;
      cin >> line;
      for (char &c : line)
        if (c == ':' || c == ',' || c == '.')
          c = ' ';
      istringstream iss(line);
      vector<int> temp;
      for (string name; iss >> name;) {
        if (!ntoi.count(name)) {
          ntoi.insert(make_pair(name, ntoi.size()));
          g.resize(g.size() + 1);
        }
        temp.push_back(ntoi[name]);
      }
      for (int i = 1; i < temp.size(); i++)
        g[temp[0]].emplace_back(temp[0], temp[i]);
    }

    int m = g.size();
    vector<int> flg(m);
    dfs(g, 0, flg);

    int res = 0;
    for (int i = 0; i < m; i++)
      if (flg[i] && g[i].size() == 0)
        res++;
    cout << res << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

struct Edge {
  int src, dst;
  Edge() {}
  Edge(int s, int d) : src(s), dst(d) {}
};
typedef vector<vector<Edge>> Graph;

void dfs(const Graph &g, int v, vector<int> &flg) {
  if (flg[v])
    return;
  flg[v] = 1;
  for (Edge e : g[v])
    dfs(g, e.dst, flg);
}

int main() {
  for (int n; cin >> n, n;) {
    map<string, int> ntoi;
    Graph g;
    for (int i = 0; i < n; i++) {
      string line;
      cin >> line;
      for (char &c : line)
        if (c == ':' || c == ',' || c == '.')
          c = ' ';
      istringstream iss(line);
      vector<int> temp;
      for (string name; iss >> name;) {
        if (!ntoi.count(name)) {
          ntoi.insert(make_pair(name, ntoi.size()));
          g.resize(g.size() + 1);
        }
        temp.push_back(ntoi[name]);
      }
      for (int i = 1; i < temp.size(); i++)
        g[temp[0]].emplace_back(temp[0], temp[i]);
    }

    int m = g.size();
    vector<int> flg(m);
    dfs(g, 0, flg);

    int res = 0;
    for (int i = 0; i < m; i++)
      if (flg[i] && g[i].size() == 0)
        res++;
    cout << res << endl;
  }
}
Problem p00884 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
set<string> S;
map<string, int> M;
int n;
string str[101];

int saiki(int ima) {
  int sum = 0;
  string a;
  for (int i = 0; i < str[ima].size(); i++) {
    if (str[ima][i] == ',') {
      if (M.count(a) == 1)
        S.insert(a), sum += saiki(M[a]);
      else if (S.count(a) == 0)
        sum++;
      S.insert(a);
      a = "";
      continue;
    }
    a += str[ima][i];
  }
  return sum;
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    S.clear(), M.clear();
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      string a, b;
      for (int j = 0; j < str[i].size(); j++) {
        if (str[i][j] == ':')
          M[a] = i, a = "";
        if (str[i][j] != ':')
          a += str[i][j];
      }
      str[i] = a;
      str[i][str[i].size() - 1] = ',';
    }
    cout << saiki(0) << endl;
    ;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
set<string> S;
map<string, int> M;
int n;
string str[101];

int saiki(int ima) {
  int sum = 0;
  string a;
  for (int i = 0; i < str[ima].size(); i++) {
    if (str[ima][i] == ',') {
      if (M.count(a) == 1 && S.count(a) == 0)
        S.insert(a), sum += saiki(M[a]);
      else if (S.count(a) == 0)
        sum++;
      S.insert(a);
      a = "";
      continue;
    }
    a += str[ima][i];
  }
  return sum;
}

int main() {

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    S.clear(), M.clear();
    for (int i = 0; i < n; i++) {
      cin >> str[i];
      string a, b;
      for (int j = 0; j < str[i].size(); j++) {
        if (str[i][j] == ':')
          M[a] = i, a = "";
        if (str[i][j] != ':')
          a += str[i][j];
      }
      str[i] = a;
      str[i][str[i].size() - 1] = ',';
    }
    cout << saiki(0) << endl;
    ;
  }
  return 0;
}
Problem p00885 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF (1 << 28)

int n, p[100], t[100], dp[100][4];

void solve() {
  rep(i, 100) rep(j, 4) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n - 1) rep(j, 4) {
    const int dt = t[i + 1] - t[i];
    if ((j + 1) * p[i] + p[i + 1] <= dt) {
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + p[i] + p[i + 1]);
    }
    if (j < 3 && (j + 1) * abs(p[i] - p[i + 1]) <= dt) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(p[i] - p[i + 1]));
    }
  }
  rep(i, n) {
    bool ok = false;
    rep(j, 4) if (dp[i][j] < INF) ok = true;
    if (!ok) {
      printf("NG %d\n", i);
      return;
    }
  }
  int ans = INF;
  rep(j, 4) ans = min(ans, dp[n - 1][j] + p[n - 1]);
  printf("OK %d\n", ans);
}

int main() {
  for (;;) {
    scanf("%d", &n);
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;
    solve();
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF (1 << 28)

int n, p[100], t[100], dp[100][4];

void solve() {
  rep(i, 100) rep(j, 4) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, n - 1) rep(j, 4) {
    const int dt = t[i + 1] - t[i];
    if ((j + 1) * p[i] + p[i + 1] <= dt) {
      dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + p[i] + p[i + 1]);
    }
    if (j < 3 && (j + 1) * abs(p[i] - p[i + 1]) <= dt) {
      dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(p[i] - p[i + 1]));
    }
  }
  rep(i, n) {
    bool ok = false;
    rep(j, 4) if (dp[i][j] < INF) ok = true;
    if (!ok) {
      printf("NG %d\n", i);
      return;
    }
  }
  int ans = INF;
  rep(j, 4) ans = min(ans, dp[n - 1][j] + p[n - 1]);
  printf("OK %d\n", ans);
}

int main() {
  for (;;) {
    scanf("%d", &n);
    if (n == 0)
      return 0;
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;
    solve();
  }
}
Problem p00885 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const ll INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int p[50], t[50], dp[50][5];
int n;

int solve(int i, int j) {
  if (i == n)
    return p[n];
  int res = INF;
  if (p[i] * (j + 1) + p[i + 1] <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, 1) + p[i] + p[i + 1]);
  if (j < 3 && abs(p[i] - p[i + 1]) * (j + 1) <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, j + 1) + abs(p[i] - p[i + 1]));
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    MS(dp, -1);
    FOR(i, 1, n + 1) cin >> p[i] >> t[i];
    int ans = solve(0, 0);
    if (ans == INF) {
      while (ans == INF) {
        MS(dp, -1);
        n--;
        ans = solve(0, 0);
      }
      cout << "NG " << n + 1 << endl;
    } else
      cout << "OK " << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const ll INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int p[50], t[50], dp[50][5];
int n;

int solve(int i, int j) {
  if (i == n)
    return p[n];
  if (dp[i][j] >= 0)
    return dp[i][j];
  int res = INF;
  if (p[i] * (j + 1) + p[i + 1] <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, 1) + p[i] + p[i + 1]);
  if (j < 3 && abs(p[i] - p[i + 1]) * (j + 1) <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, j + 1) + abs(p[i] - p[i + 1]));
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    MS(dp, -1);
    FOR(i, 1, n + 1) cin >> p[i] >> t[i];
    int ans = solve(0, 0);
    if (ans == INF) {
      while (ans == INF) {
        MS(dp, -1);
        n--;
        ans = solve(0, 0);
      }
      cout << "NG " << n + 1 << endl;
    } else
      cout << "OK " << ans << endl;
  }
  return 0;
}
Problem p00885 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;
int n, dis[41], t[41], maxball, ans;

void saiki(int x, int now, int ball, int sum, int kai) {
  maxball = max(maxball, kai);
  if (kai == n && !ball)
    ans = min(ans, sum);
  if (ball)
    saiki(0, now + x * (1 + ball), 0, sum + x, kai);
  if (ball == 3 || kai == n)
    return;

  int nx = dis[kai];
  int nsum = sum + abs(x - dis[kai]);
  int nnow = now + abs(x - dis[kai]) * (1 + ball);
  if (nnow <= t[kai])
    saiki(nx, t[kai], ball + 1, nsum, kai + 1);
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> dis[i] >> t[i];
    sort(t, t + n);
    maxball = 0, ans = 100000000;
    saiki(0, 0, 0, 0, 0);
    if (maxball == n)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << maxball + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int n, dis[41], t[41], maxball, ans;

void saiki(int x, int now, int ball, int sum, int kai) {
  if (ans < sum)
    return;
  maxball = max(maxball, kai);
  if (kai == n && !ball)
    ans = min(ans, sum);
  if (ball)
    saiki(0, now + x * (1 + ball), 0, sum + x, kai);
  if (ball == 3 || kai == n)
    return;

  int nx = dis[kai];
  int nsum = sum + abs(x - dis[kai]);
  int nnow = now + abs(x - dis[kai]) * (1 + ball);
  if (nnow <= t[kai])
    saiki(nx, t[kai], ball + 1, nsum, kai + 1);
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> dis[i] >> t[i];
    sort(t, t + n);
    maxball = 0, ans = 100000000;
    saiki(0, 0, 0, 0, 0);
    if (maxball == n)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << maxball + 1 << endl;
  }
  return 0;
}
Problem p00890 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define clr clear()
#define pb push_back
#define sz size()
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;
const int INF = 1e8;

int n, m, c;
int f, t, cost;
vector<P> g[101];
int d[101][100001];

int main() {
  while (cin >> n >> m >> c, n) {
    rep(i, n) {
      g[i].clr;
      rep(j, c + 1) d[i][j] = INF;
    }

    rep(i, m) {
      cin >> f >> t >> cost;
      f--;
      t--;
      g[f].pb(P(cost, t));
    }

    priority_queue<P2, vector<P2>, greater<P2>> q;
    q.push(P2(0, P(0, 0)));
    d[0][0] = 0;

    while (q.sz) {
      P2 p = q.top();
      q.pop();
      int change = p.fs, v = p.sc.fs, dis = p.sc.sc;

      rep(i, g[v].sz) {
        int nd = dis + g[v][i].fs, nv = g[v][i].sc;
        if (d[nv][dis] > change + 1) {
          d[nv][dis] = change + 1;
          q.push(P2(d[nv][dis], P(nv, dis)));
        }

        if (nd > c)
          continue;
        if (d[nv][nd] > change) {
          d[nv][nd] = change;
          q.push(P2(d[nv][nd], P(nv, nd)));
        }
      }
    }

    int ans = INF;
    rep(i, c + 1) ans = min(ans, d[n - 1][i]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (int)(a); i++)
#define clr clear()
#define pb push_back
#define sz size()
#define fs first
#define sc second
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P2;
const int INF = 1e8;

int n, m, c;
int f, t, cost;
vector<P> g[101];
int d[101][100001];

int main() {
  while (cin >> n >> m >> c, n) {
    rep(i, n) {
      g[i].clr;
      rep(j, c + 1) d[i][j] = INF;
    }

    rep(i, m) {
      cin >> f >> t >> cost;
      f--;
      t--;
      g[f].pb(P(cost, t));
    }

    priority_queue<P2, vector<P2>, greater<P2>> q;
    q.push(P2(0, P(0, 0)));
    d[0][0] = 0;

    while (q.sz) {
      P2 p = q.top();
      q.pop();
      int change = p.fs, v = p.sc.fs, dis = p.sc.sc;
      if (change > d[v][dis])
        continue;
      if (v == n - 1)
        break;

      rep(i, g[v].sz) {
        int nd = dis + g[v][i].fs, nv = g[v][i].sc;
        if (d[nv][dis] > change + 1) {
          d[nv][dis] = change + 1;
          q.push(P2(d[nv][dis], P(nv, dis)));
        }

        if (nd > c)
          continue;
        if (d[nv][nd] > change) {
          d[nv][nd] = change;
          q.push(P2(d[nv][nd], P(nv, nd)));
        }
      }
    }

    int ans = INF;
    rep(i, c + 1) ans = min(ans, d[n - 1][i]);
    cout << ans << endl;
  }
}
Problem p00890 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF (1e9)
#define N 105
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int n, m, C, f, t, c;
vector<P> G[N];

int dijkstra() {
  int d[N][N];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j] = INF;
  priority_queue<P1, vector<P1>, greater<P1>> q;
  d[0][0] = 0;
  q.push(P1(0, P(0, 0)));
  while (!q.empty()) {
    P1 a = q.top();
    q.pop();
    int cost = a.first;
    int cnt = a.second.first;
    int node = a.second.second;
    if (d[cnt][node] < cost)
      continue;
    for (int i = 0; i < G[node].size(); i++) {
      int nnode = G[node][i].first;
      int ncost = cost + G[node][i].second;
      if (d[cnt][nnode] > ncost) {
        d[cnt][nnode] = ncost;
        q.push(P1(ncost, P(cnt, nnode)));
      }
      if (d[cnt + 1][nnode] > cost) {
        d[cnt + 1][nnode] = cost;
        q.push(P1(cost, P(cnt + 1, nnode)));
      }
    }
  }
  for (int i = 0; i <= n; i++)
    if (C >= d[i][n - 1])
      return i;
}

int main() {
  while (1) {
    cin >> n >> m >> C;
    if (!n && !m && !C)
      break;
    for (int i = 0; i < m; i++) {
      cin >> f >> t >> c;
      f--, t--;
      G[f].push_back(P(t, c));
    }
    cout << dijkstra() << endl;
    for (int i = 0; i < n; i++)
      G[i].clear();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF (1e9)
#define N 105
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> P1;
int n, m, C, f, t, c;
vector<P> G[N];

int dijkstra() {
  int d[N][N];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j] = INF;
  priority_queue<P1, vector<P1>, greater<P1>> q;
  d[0][0] = 0;
  q.push(P1(0, P(0, 0)));
  while (!q.empty()) {
    P1 a = q.top();
    q.pop();
    int cost = a.first;
    int cnt = a.second.first;
    int node = a.second.second;
    if (d[cnt][node] < cost)
      continue;
    for (int i = 0; i < G[node].size(); i++) {
      int nnode = G[node][i].first;
      int ncost = cost + G[node][i].second;
      if (d[cnt][nnode] > ncost) {
        d[cnt][nnode] = ncost;
        q.push(P1(ncost, P(cnt, nnode)));
      }
      if (cnt < n && d[cnt + 1][nnode] > cost) {
        d[cnt + 1][nnode] = cost;
        q.push(P1(cost, P(cnt + 1, nnode)));
      }
    }
  }
  for (int i = 0; i <= n; i++)
    if (C >= d[i][n - 1])
      return i;
}

int main() {
  while (1) {
    cin >> n >> m >> C;
    if (!n && !m && !C)
      break;
    for (int i = 0; i < m; i++) {
      cin >> f >> t >> c;
      f--, t--;
      G[f].push_back(P(t, c));
    }
    cout << dijkstra() << endl;
    for (int i = 0; i < n; i++)
      G[i].clear();
  }
  return 0;
}
Problem p00895 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
using namespace std;
string mp[11];
int w, h;
set<string> cnt;
string ans;
void check(int x, int y) {
  string str;
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++) {
      if (i == 0 && j == 0)
        continue;
      str = mp[y][x];
      int nx = (x + j + w) % w, ny = (y + i + h) % h;
      while (ny != y || nx != x) {
        str += mp[ny][nx], nx = (nx + j + w) % w, ny = (ny + i + h) % h;
        if (cnt.count(str)) {
          if (str.size() > ans.size())
            ans = str;
          else if (str.size() == ans.size())
            ans = min(str, ans);
        } else
          cnt.insert(str);
      }
    }
}

int main() {
  while (1) {
    cin >> h >> w;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    ans = "";
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        check(j, i);
    if (ans.size() == 0)
      ans = "0";
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
using namespace std;
string mp[11];
int w, h;
set<string> cnt;
string ans;
void check(int x, int y) {
  string str;
  for (int i = -1; i <= 1; i++)
    for (int j = -1; j <= 1; j++) {
      if (i == 0 && j == 0)
        continue;
      str = mp[y][x];
      int nx = (x + j + w) % w, ny = (y + i + h) % h;
      while (ny != y || nx != x) {
        str += mp[ny][nx], nx = (nx + j + w) % w, ny = (ny + i + h) % h;
        if (cnt.count(str)) {
          if (str.size() > ans.size())
            ans = str;
          else if (str.size() == ans.size())
            ans = min(str, ans);
        } else
          cnt.insert(str);
      }
    }
}

int main() {
  while (1) {
    cin >> h >> w;
    cnt.clear();
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++)
      cin >> mp[i];

    ans = "";
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        check(j, i);
    if (ans.size() == 0)
      ans = "0";
    cout << ans << endl;
  }
  return 0;
}
Problem p00896 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

string t, ans;
vector<string> words, latte[21];

bool rec(int idx, vector<char> &add) {
  if (idx == words.size()) {
    if (!ans.empty())
      return (false);
    ans = t;
    for (auto &c : ans)
      if (isalpha(c))
        c = add[c - 'A'];
    return (true);
  }

  const string &beet = words[idx];
  for (auto &str : latte[beet.size()]) {
    auto temp(add);
    bool flag = true;
    for (int i = 0; i < beet.size() && flag; i++) {
      if (temp[beet[i] - 'A'] != str[i] &&
          (temp[beet[i] - 'A'] != -1 || temp[str[i] - 'A'] != -1))
        flag = false;
      else {
        temp[beet[i] - 'A'] = str[i];
        temp[str[i] - 'A'] = beet[i];
      }
    }
    if (!flag)
      continue;
    if (!rec(idx + 1, temp))
      return (false);
  }
  return (true);
}

int main() {
  int N;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      latte[s.size()].emplace_back(s);
    }
    cin.ignore();
    getline(cin, t);

    istringstream sss(t);
    string q;
    while (sss >> q) {
      if (q.back() == '.')
        q.pop_back();
      words.emplace_back(q);
    }
    sort(begin(words), end(words), [&](const string &a, const string &b) {
      return (a.size() < b.size());
    });
    vector<char> add(26, -1);
    if (rec(0, add))
      cout << ans << endl;
    else
      cout << "-.\n";
    for (int i = 0; i < 21; i++)
      latte[i].clear();
    words.clear();
    ans.clear();
  }
}

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

using namespace std;

string t, ans;
vector<string> words, latte[21];

bool rec(int idx, vector<char> &add) {
  if (idx == words.size()) {
    if (!ans.empty())
      return (false);
    ans = t;
    for (auto &c : ans)
      if (isalpha(c))
        c = add[c - 'A'];
    return (true);
  }

  const string &beet = words[idx];
  for (auto &str : latte[beet.size()]) {
    auto temp(add);
    bool flag = true;
    for (int i = 0; i < beet.size() && flag; i++) {
      if (temp[beet[i] - 'A'] != str[i] &&
          (temp[beet[i] - 'A'] != -1 || temp[str[i] - 'A'] != -1))
        flag = false;
      else {
        temp[beet[i] - 'A'] = str[i];
        temp[str[i] - 'A'] = beet[i];
      }
    }
    if (!flag)
      continue;
    if (!rec(idx + 1, temp))
      return (false);
  }
  return (true);
}

int main() {
  int N;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      string s;
      cin >> s;
      latte[s.size()].emplace_back(s);
    }
    cin.ignore();
    getline(cin, t);

    istringstream sss(t);
    string q;
    while (sss >> q) {
      if (q.back() == '.')
        q.pop_back();
      words.emplace_back(q);
    }
    sort(begin(words), end(words), [&](const string &a, const string &b) {
      return (set<char>(begin(a), end(a)).size() >
              set<char>(begin(b), end(b)).size());
    });
    vector<char> add(26, -1);
    if (rec(0, add))
      cout << ans << endl;
    else
      cout << "-.\n";
    for (int i = 0; i < 21; i++)
      latte[i].clear();
    words.clear();
    ans.clear();
  }
}
Problem p00897 (C++)

Buggy submission (status: Time Limit Exceeded):
// 17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct E {
  string t;
  int d;
};

struct S {
  string v;
  int r, d;
  bool operator<(S a) const { return d > a.d; }
};

int main() {
  for (int n, mm, cap; cin >> n >> mm >> cap, n | mm | cap;) {
    cap *= 10;
    string src, dest;
    cin >> src >> dest;
    map<string, vector<E>> m;
    for (int i = 0; i < n; i++) {
      string c[2];
      int d;
      cin >> c[0] >> c[1] >> d;
      for (int j = 0; j < 2; j++) {
        E ce = {c[j ^ 1], d};
        m[c[j]].push_back(ce);
      }
    }
    set<string> gs;
    for (int i = 0; i < mm; i++) {
      string s;
      cin >> s;
      gs.insert(s);
    }
    priority_queue<S> que;
    S is = {src, cap, 0};
    que.push(is);
    map<string, map<int, int>> p;
    while (!que.empty()) {
      S c = que.top();
      if (c.v == dest)
        break;
      que.pop();
      map<int, int>::iterator it = p[c.v].upper_bound(c.r);
      if (it != p[c.v].end() && it->second <= c.d)
        continue;
      p[c.v][c.r] = c.d;
      for (int i = 0; i < m[c.v].size(); i++) {
        if (m[c.v][i].d <= c.r) {
          S n = {m[c.v][i].t, c.r - m[c.v][i].d, c.d + m[c.v][i].d};
          if (gs.count(n.v)) {
            n.r = cap;
          }
          que.push(n);
        }
      }
    }
    cout << (que.empty() ? -1 : que.top().d) << endl;
  }
  return 0;
}

Fixed submission:
// 17
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

struct E {
  string t;
  int d;
};

struct S {
  string v;
  int r, d;
  bool operator<(S a) const { return d > a.d; }
};

int main() {
  for (int n, mm, cap; cin >> n >> mm >> cap, n | mm | cap;) {
    cap *= 10;
    string src, dest;
    cin >> src >> dest;
    map<string, vector<E>> m;
    for (int i = 0; i < n; i++) {
      string c[2];
      int d;
      cin >> c[0] >> c[1] >> d;
      for (int j = 0; j < 2; j++) {
        E ce = {c[j ^ 1], d};
        m[c[j]].push_back(ce);
      }
    }
    set<string> gs;
    for (int i = 0; i < mm; i++) {
      string s;
      cin >> s;
      gs.insert(s);
    }
    priority_queue<S> que;
    S is = {src, cap, 0};
    que.push(is);
    map<string, map<int, int>> p;
    while (!que.empty()) {
      S c = que.top();
      if (c.v == dest)
        break;
      que.pop();
      map<int, int>::iterator it = p[c.v].upper_bound(c.r - 1);
      if (it != p[c.v].end() && it->second <= c.d)
        continue;
      p[c.v][c.r] = c.d;
      for (int i = 0; i < m[c.v].size(); i++) {
        if (m[c.v][i].d <= c.r) {
          S n = {m[c.v][i].t, c.r - m[c.v][i].d, c.d + m[c.v][i].d};
          if (gs.count(n.v)) {
            n.r = cap;
          }
          que.push(n);
        }
      }
    }
    cout << (que.empty() ? -1 : que.top().d) << endl;
  }
  return 0;
}