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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

const int N = 1010;

int n;
int stay[N];
set<int> st;

void work() {
  memset(stay, 0, sizeof(stay));
  char day[100] = "";
  int now = 0;
  int T = 60 * 24;
  for (int i = 0; i < n; i++) {
    char td[100];
    scanf("%s", td);
    int hour, minu;
    scanf("%d:%d", &hour, &minu);
    int next = hour * 60 + minu;
    int ed = next;
    if (strcmp(td, day) != 0)
      ed = T;
    if (st.count(0)) {
      for (set<int>::iterator it = st.begin(); it != st.end(); it++)
        stay[*it] += ed - now;
    }
    if (strcmp(td, day) != 0) {
      st.clear();
      strcpy(day, td);
    }
    now = next;
    char op[2];
    int p;
    scanf("%s%d", op, &p);
    if (*op == 'I')
      st.insert(p);
    else
      st.erase(p);
  }
  int ans = 0;
  for (int i = 1; i < 1000; i++)
    ans = max(ans, stay[i]);
  printf("%d\\n", ans);
}
int main() {
  // freopen("data.in", "r", stdin);
  while (scanf("%d", &n), n) {
    work();
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int maxn = 5e5;
vector<int> g[maxn];
int etr[1100], res[1100];
vector<int> god;
int d[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int trans(int M, int D, int h, int m) {
  int ans = 0;
  for (int i = 1; i < M; i++)
    ans += d[i] * 24 * 60;
  ans += (D - 1) * 24 * 60 + h * 60 + m;
  return ans;
}
int main() {
  int n;
  int M, D, h, m, p;
  char op[5];
  while (~scanf("%d", &n), n) {
    for (int i = 0; i < 1000; i++)
      g[i].clear();
    god.clear();
    memset(res, 0, sizeof(res));
    while (n--) {
      scanf("%d/%d %d:%d %s %d", &M, &D, &h, &m, op, &p);
      if (op[0] == 'O') {
        int l = trans(M, D, h, m);
        if (p) {
          for (int i = etr[p]; i < l; i++)
            g[i].push_back(p);
        } else {
          for (int i = etr[p]; i < l; i++)
            god.push_back(i);
        }
      } else {
        etr[p] = trans(M, D, h, m);
      }
    }

    int sz = god.size();
    for (int i = 0; i < sz; i++) {
      int t = god[i];
      int l = g[t].size();
      for (int j = 0; j < l; j++)
        res[g[t][j]]++;
    }
    int _max = 0;
    for (int i = 1; i < 1000; i++)
      if (_max < res[i])
        _max = res[i];

    printf("%d\\n", _max);
  }
}

/*
14
04/21 09:00 I 000
04/21 09:00 I 001
04/21 09:15 I 002
04/21 09:30 O 001
04/21 09:45 O 000
04/21 10:00 O 002
04/28 09:00 I 003
04/28 09:15 I 000
04/28 09:30 I 004
04/28 09:45 O 004
04/28 10:00 O 000
04/28 10:15 O 003
04/29 20:00 I 002
04/29 21:30 O 002
20
06/01 09:00 I 001
06/01 09:15 I 002
06/01 09:15 I 003
06/01 09:30 O 002
06/01 10:00 I 000
06/01 10:15 O 001
06/01 10:30 I 002
06/01 10:45 O 002
06/01 11:00 I 001
06/01 11:15 O 000
06/01 11:30 I 002
06/01 11:45 O 001
06/01 12:00 O 002
06/01 12:15 I 000
06/01 12:30 I 002
06/01 12:45 O 000
06/01 13:00 I 000
06/01 13:15 O 000
06/01 13:30 O 002
06/01 13:45 O 003
0
*/
Problem p00894 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

using namespace std;

inline int timeOf(int h, int m) { return h * 60 + m; }

int main() {
  while (int n = getInt()) {
    vector<int> ans(1000, 0);
    vector<int> in(1000, -1);

    REP(i, n) {
      int M, D;
      int h, m;
      char s[2];
      int id;

      scanf("%02d/%02d %02d:%02d %s %03d", &M, &D, &h, &m, s, &id);

      if (s[0] == 'I') {
        in[id] = timeOf(h, m);
      } else {
        int t = timeOf(h, m);
        if (id == 0) {
          for (int j = 1; j < 1000; j++) {
            if (in[j] != -1) {
              ans[j] += t - max(in[j], in[0]);
            }
          }
        } else {
          if (in[0] != -1) {
            ans[id] += t - max(in[id], in[0]);
          }
        }
        in[id] = -1;
      }
    }

    printf("%d\\n", *max_element(ans.begin() + 1, ans.end()));
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <set>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int a[100];
int b[100];

int main() {
  int n;
  while (scanf("%d", &n), n) {
    char message[110];
    scanf("%s", message);

    REP(i, n) {
      scanf("%d%d", &a[i], &b[i]);
      a[i]--;
      b[i]--;
    }

    for (int i = n - 1; i >= 0; i--) {
      swap(message[a[i]], message[b[i]]);
      int diff = b[i] - a[i];
      int all = 'z' - 'a' + 1;

      message[a[i]] = (message[a[i]] - 'a' + diff) % all + 'a';
      message[b[i]] = (message[b[i]] - 'a' + diff) % all + 'a';
    }
    puts(message);
  }
  return 0;
}
Problem p00894 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define MAX_NUM 1005

typedef vector<int> vi;

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

    vi rec(MAX_NUM, -1);
    vi sum(MAX_NUM, 0);

    rep(i, 0, n) {
      string s;

      cin >> s;
      int d = (s[3] - '0') * 10 + (s[4] - '0');

      cin >> s;
      int h = (s[0] - '0') * 10 + (s[1] - '0');
      int m = (s[3] - '0') * 10 + (s[4] - '0');

      int time = d * 60 * 60 + h * 60 + m;

      char e;
      cin >> e;
      int p;
      cin >> p;

      if (e == 'I') {
        rec[p] = time;
      } else if (e == 'O') {
        if (p == 0) {
          rep(j, 1, MAX_NUM) {
            if (rec[j] >= 0) {
              sum[j] += time - max(rec[j], rec[0]);
            }
          }

          rec[0] = -1;
        } else {
          if (rec[0] >= 0) {
            sum[p] += time - max(rec[0], rec[p]);
          }

          rec[p] = -1;
        }
      }
    }

    int ans = 0;
    rep(i, 0, MAX_NUM) ans = max(ans, sum[i]);

    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

#define rep(i, a, b) for (int i = a; i < b; i++)
#define MAX_NUM 1005

typedef vector<int> vi;
typedef vector<vi> vvi;

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

    vvi imos(MAX_NUM, vi(31 * 60 * 60 + 5, 0));
    rep(i, 0, n) {
      string md;
      cin >> md;
      int d = (md[3] - '0') * 10 + (md[4] - '0');
      string hm;
      cin >> hm;
      int h = (hm[0] - '0') * 10 + (hm[1] - '0');
      int m = (hm[3] - '0') * 10 + (hm[4] - '0');

      char e;
      cin >> e;
      int p;
      cin >> p;
      if (e == 'I')
        imos[p][d * 60 * 60 + h * 60 + m]++;
      else
        imos[p][d * 60 * 60 + h * 60 + m + 1]--;
    }

    rep(i, 0, MAX_NUM) rep(j, 1, 31 * 60 * 60 + 5) imos[i][j] += imos[i][j - 1];
    rep(i, 1, MAX_NUM) rep(j, 0, 31 * 60 * 60 + 5) imos[i][j] *= imos[0][j];

    int ans = 0;
    rep(i, 1, MAX_NUM) {
      int left = -1;
      if (imos[i][0] > 0)
        left = 0;
      int sum = 0;
      rep(j, 1, 31 * 60 * 60 + 5) {
        if (imos[i][j] > 0) {
          if (left < 0)
            left = j;
        } else {
          if (left >= 0) {
            sum += j - left - 1;
            left = -1;
          }
        }
      }

      ans = max(ans, sum);
    }

    cout << ans << endl;
  }
}
Problem p00895 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, x) for (int i = 0; i < x; i++)

string res;
int H, W, dx, dy, min_len;
char s[200];
char MAP[11][22];
bool visit[20][20];
vector<string> ss;
void dfs(int x, int y, int k) {
  if (visit[y][x]) {
    ss.push_back(string(s));
    return;
  }
  s[k] = MAP[y][x];
  visit[y][x] = 1;
  dfs((x + dx + W) % W, (y + dy + H) % H, k + 1);
  s[k] = 0;
  visit[y][x] = 0;
}
int main() {
  while (scanf("%d%d", &H, &W), H) {
    REP(i, H) scanf("%s", MAP[i]);
    ss.clear();
    min_len = 1;
    res = "0";
    REP(y, H) REP(x, W) {
      for (dx = -1; dx <= 1; dx++) {
        for (dy = -1; dy <= 1; dy++) {
          if (dx | dy)
            dfs(x, y, 0);
        }
      }
    }
    string res = "0";
    sort(ss.begin(), ss.end());
    REP(i, ss.size() - 1) {
      int len = 0;
      REP(k, min(ss[i].size(), ss[i + 1].size())) {
        if (ss[i][k] == ss[i + 1][k])
          len++;
        else
          break;
      }
      if (len < res.size())
        continue;
      if (res.size() == len) {
        res = min(res, ss[i].substr(0, len));
      } else {
        res = ss[i].substr(0, len);
      }
    }
    puts(res.c_str());
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, x) for (int i = 0; i < x; i++)

string res;
int H, W, dx, dy, min_len;
char s[200];
char MAP[11][22];
bool visit[20][20];
vector<string> ss;
void dfs(int x, int y, int k) {
  if (visit[y][x]) {
    ss.push_back(string(s));
    return;
  }
  s[k] = MAP[y][x];
  visit[y][x] = 1;
  dfs((x + dx + W) % W, (y + dy + H) % H, k + 1);
  s[k] = 0;
  visit[y][x] = 0;
}
int main() {
  while (scanf("%d%d", &W, &H), H) {
    REP(i, H) scanf("%s", MAP[i]);
    ss.clear();
    min_len = 1;
    res = "0";
    REP(y, H) REP(x, W) {
      for (dx = -1; dx <= 1; dx++) {
        for (dy = -1; dy <= 1; dy++) {
          if (dx | dy)
            dfs(x, y, 0);
        }
      }
    }
    string res = "0";
    sort(ss.begin(), ss.end());
    REP(i, ss.size() - 1) {
      int len = 0;
      REP(k, min(ss[i].size(), ss[i + 1].size())) {
        if (ss[i][k] == ss[i + 1][k])
          len++;
      }
      if (len < res.size())
        continue;
      if (res.size() == len) {
        res = min(res, ss[i].substr(0, len));
      } else {
        res = ss[i].substr(0, len);
      }
    }
    puts(res.c_str());
  }
}
Problem p00895 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vec;
char c[20][20];
int H, W;
bool used[20][20];
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int main() {
  while (true) {
    cin >> H >> W;
    vec.clear();
    if (H == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 0; k < 8; k++) {
          for (int l = 0; l < 400; l++)
            used[l / 20][l % 20] = false;
          int cx = i, cy = j;
          string S;
          S += c[cx][cy];
          used[cx][cy] = true;
          while (true) {
            cx += dx[k];
            cy += dy[k];
            if (cx < 0)
              cx += H;
            if (cx >= H)
              cx -= H;
            if (cy < 0)
              cy += W;
            if (cy >= W)
              cy -= W;
            if (used[cx][cy] == true)
              break;
            used[cx][cy] = true;
            S += c[cx][cy];
            vec.push_back(S);
          }
        }
      }
    }
    sort(vec.begin(), vec.end());
    string T = "0";
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i - 1] == vec[i]) {
        if (vec[i].size() > T.size())
          T = vec[i];
        else if (vec[i].size() == T.size())
          T = min(T, vec[i]);
      }
    }
    cout << T << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> vec;
char c[20][20];
int H, W;
bool used[20][20];
int dx[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, 0};
int main() {
  while (true) {
    cin >> H >> W;
    vec.clear();
    if (H == 0)
      break;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> c[i][j];
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        for (int k = 0; k < 8; k++) {
          for (int l = 0; l < 400; l++)
            used[i / 20][i % 20] = 0;
          int cx = i, cy = j;
          string S;
          S += c[cx][cy];
          used[cx][cy] = 1;
          while (true) {
            cx += dx[k];
            cy += dy[k];
            if (used[cx][cy] == 1)
              break;
            used[cx][cy] = 1;
            S += c[cx][cy];
            vec.push_back(S);
          }
        }
      }
    }
    sort(vec.begin(), vec.end());
    string T = "0";
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i - 1] == vec[i]) {
        if (vec[i].size() > T.size())
          T = vec[i];
        else if (vec[i].size() == T.size())
          T = min(T, vec[i]);
      }
    }
    cout << T << endl;
  }
  return 0;
}
Problem p00895 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define ll long long
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

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

set<string> Set;
int Maxlen = 0;
string ans;
vector<string> donut;
vector<vector<int>> usage;

void solve(int i, int j) {
  for (int k = 0; k < 8; k++) {
    usage.clear();
    usage.resize(h, vector<int>(w, 0));
    string ret = "";
    int y = i, x = j;
    while (usage[y][x] == 0) {
      ret += donut[y][x];

      if (Set.find(ret) != Set.end()) {
        if (ret.length() > Maxlen) {
          Maxlen = ret.length();
          ans = ret;
        } else if (ret.length() == Maxlen && ret < ans) {
          ans = ret;
        }
      } else {
        Set.insert(ret);
      }

      usage[y][x] = 1;
      y = (y + dy[k] + h) % h;
      x = (x + dx[k] + w) % w;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    Maxlen = 0;
    ans = "";
    donut.clear();
    donut.resize(h);
    Set.clear();
    for (int i = 0; i < h; i++) {
      cin >> donut[i];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        solve(i, j);
      }
    }
    if (ans.length() < 2)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define ll long long
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

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

set<string> Set;
int Maxlen = 0;
string ans;
vector<vector<char>> donut;
vector<vector<int>> usage;

void solve(int i, int j) {
  for (int k = 0; k < 8; k++) {
    usage.clear();
    usage.resize(h, vector<int>(w, 0));
    string ret = "";
    int y = i, x = j;
    while (usage[y][x] == 0) {
      ret += donut[y][x];

      if (Set.find(ret) != Set.end()) {
        if (ret.length() > Maxlen) {
          Maxlen = ret.length();
          ans = ret;
        } else if (ret.length() == Maxlen && ret < ans) {
          ans = ret;
        }
      } else {
        Set.insert(ret);
      }

      usage[y][x] = 1;
      y = (y + dy[k] + h) % h;
      x = (x + dx[k] + w) % w;
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;
    Maxlen = 0;
    ans = "";
    donut.clear();
    donut.resize(h, vector<char>(w));
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> donut[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        solve(i, j);
      }
    }
    if (ans.length() < 2)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}
Problem p00895 (C++) — buggy submission, verdict: 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;
    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;
}

Error: MLE

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;
    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, verdict: 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 (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();
  }
}

Error: TLE

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 (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();
  }
}
Problem p00897 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

struct NODE {
  int src, dst, cap, weight;
};
bool operator<(const NODE &a, const NODE &b) { return a.weight > b.weight; }
map<string, int> dic;
int get(string s) {
  if (dic.count(s))
    return dic[s];
  int k = dic.size();
  return dic[s] = k;
}

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap && n) {
    vector<NODE> g[6010];
    int LPG[6010] = {};
    dic.clear();
    string s_, g_;
    cin >> s_ >> g_;
    int s = get(s_);
    int gg = get(g_);
    for (int i = 0; i < n; i++) {
      string a_, b_;
      cin >> a_ >> b_;
      int a = get(a_);
      int b = get(b_);
      int c;
      cin >> c;
      g[a].push_back({a, b, 0, c});
      g[b].push_back({b, a, 0, c});
    }
    for (int i = 0; i < m; i++) {
      string x_;
      cin >> x_;
      int x = get(x_);
      LPG[x] = true;
    }
    priority_queue<NODE> Q;
    int done[6010] = {};
    for (int i = 0; i < 6010; i++)
      done[i] = -1;
    Q.push({s, -114514, cap * 10, 0});
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (q.cap < 0)
        continue;
      if (q.src == gg) {
        cout << q.weight << endl;
        goto ok;
      }
      if (done[q.src] < q.cap) {
        done[q.src] = q.cap;
      } else
        continue;
      if (cap * 10 != q.cap && LPG[q.src]) {
        Q.push({q.src, -114514, cap * 10, q.weight});
      } else {
        for (auto e : g[q.src]) {
          Q.push({e.dst, -114514, q.cap - e.weight, q.weight + e.weight});
        }
      }
    }
    cout << -1 << endl;
  ok:;
  }
}

Error: MLE

Fixed submission:

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

struct NODE {
  int src, dst, cap, weight;
};
bool operator<(const NODE &a, const NODE &b) { return a.weight > b.weight; }
map<string, int> dic;
int get(string s) {
  if (dic.count(s))
    return dic[s];
  int k = dic.size();
  return dic[s] = k;
}

int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap && n) {
    vector<NODE> g[6010];
    int LPG[6010] = {};
    dic.clear();
    string s_, g_;
    cin >> s_ >> g_;
    int s = get(s_);
    int gg = get(g_);
    for (int i = 0; i < n; i++) {
      string a_, b_;
      cin >> a_ >> b_;
      int a = get(a_);
      int b = get(b_);
      int c;
      cin >> c;
      g[a].push_back({a, b, 0, c});
      g[b].push_back({b, a, 0, c});
    }
    for (int i = 0; i < m; i++) {
      string x_;
      cin >> x_;
      int x = get(x_);
      LPG[x] = true;
    }
    priority_queue<NODE> Q;
    set<pair<int, int>> done;
    Q.push({s, -114514, cap * 10, 0});
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (q.cap < 0)
        continue;
      if (q.src == gg) {
        cout << q.weight << endl;
        goto ok;
      }
      if (done.count(make_pair(q.src, q.cap)))
        continue;
      else
        done.insert(make_pair(q.src, q.cap));
      if (LPG[q.src]) {
        Q.push({q.src, -114514, cap * 10, q.weight});
      }
      for (auto e : g[q.src]) {
        Q.push({e.dst, -114514, q.cap - e.weight, q.weight + e.weight});
      }
    }
    cout << -1 << endl;
  ok:;
  }
}