alexjercan/bugnet huggingface dataset:
Problem p00750 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

int u[400], v[400];
string w[400];

int main() {
  int n, m, s, e;
  char str[7];
  string dist[40];
  bool visited[40], change;

  while (true) {
    scanf("%d %d %d %d", &n, &m, &s, &e);

    if (n == 0)
      break;

    for (int i = 0; i < m; ++i) {
      scanf("%d %d %s", &u[i], &v[i], str);
      w[i] = string(str);
    }

    memset(visited, 0, sizeof visited);

    dist[e] = "";
    visited[e] = true;
    change = false;

    for (int it = 0; it <= 6 * n; ++it) {
      for (int i = 0; i < m; ++i) {
        if (visited[v[i]]) {
          string aux = w[i] + dist[v[i]];

          if (!visited[u[i]] || aux < dist[u[i]]) {
            dist[u[i]] = aux;
            if (it >= n - 1 && u[i] == s)
              change = true;
          }

          visited[u[i]] = true;
        }
      }
    }

    if (change || !visited[s])
      puts("NO");
    else
      puts(dist[s].c_str());
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

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

  int n, m, s, e;
  bool can[40][40];
  string M[40][40], str;
  string loop[40];

  while (true) {
    cin >> n >> m >> s >> e;

    if (n == 0)
      break;

    for (int i = 0; i < n; ++i)
      loop[i] = "zzzzzzz";

    memset(can, 0, sizeof can);

    for (int i = 0, u, v; i < m; ++i) {
      cin >> u >> v >> str;

      if (u == v)
        loop[u] = min(loop[u], str);
      else {
        if (!can[u][v])
          M[u][v] = str;
        else
          M[u][v] = min(M[u][v], str);

        can[u][v] = true;
      }
    }

    for (int k = 0; k < n; ++k)
      for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j)
          if (can[i][k] && can[k][j]) {
            if (!can[i][j])
              M[i][j] = M[i][k] + M[k][j];
            else
              M[i][j] = min(M[i][j], M[i][k] + M[k][j]);

            can[i][j] = true;
          }

    if (!can[s][e])
      cout << "NO\\n";
    else {
      bool ok = true;

      if (loop[s] + M[s][e] < M[s][e])
        ok = false;

      for (int i = 0; i < n; ++i)
        if (i != s && i != e && M[s][i] + loop[i] + M[i][e] < M[s][e])
          ok = false;

      if (!ok)
        cout << "NO\\n";
      else
        cout << M[s][e] << '\\n';
    }
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
typedef pair<string, int> P;
struct edge {
  int to;
  string cost;
  edge() {}
  edge(int to, string cost) : to(to), cost(cost){};
};
vector<vector<edge>> info;
int n, a, s, g;
string d[40][400];
string Dijkstra() {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 400; j++)
      d[i][j] = "";
  que.push(P("", s));
  d[s][0] = "";
  while (!que.empty()) {
    P p = que.top();
    int now = p.sc;
    string nowstr = p.fr;
    que.pop();
    if (now == g)
      return (nowstr.size() >= 300 ? "NO" : p.fr);
    if (d[now][nowstr.size()] != "" && d[now][nowstr.size()] < nowstr)
      continue;
    for (int i = 0; i < info[now].size(); i++) {
      edge e = info[now][i];
      string next_str = nowstr + e.cost;
      if (next_str.size() >= 400)
        continue;
      if (d[e.to][next_str.size()] == "" ||
          next_str < d[e.to][next_str.size()]) {
        d[e.to][next_str.size()] = next_str;
        que.push(P(next_str, e.to));
      }
    }
  }
  return "NO";
}
int main() {
  while (cin >> n >> a >> s >> g, n) {
    info.resize(n);
    for (int i = 0, x, y; i < a; i++) {
      string s;
      cin >> x >> y >> s;
      info[x].push_back(edge(y, s));
    }
    cout << Dijkstra() << endl;
    info.clear();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define fr first
#define sc second
typedef pair<string, int> P;
struct edge {
  int to;
  string cost;
  edge() {}
  edge(int to, string cost) : to(to), cost(cost){};
};
vector<vector<edge>> info;
int n, a, s, g;
string d[40][400];
string Dijkstra() {
  priority_queue<P, vector<P>, greater<P>> que;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 400; j++)
      d[i][j] = "";
  que.push(P("", s));
  d[s][0] = "";
  while (!que.empty()) {
    P p = que.top();
    int now = p.sc;
    string nowstr = p.fr;
    que.pop();
    if (now == g)
      return (nowstr.size() >= 300 ? "NO" : p.fr);
    if (nowstr.size() >= 400)
      continue;
    if (d[now][nowstr.size()] != "" && d[now][nowstr.size()] < nowstr)
      continue;
    for (int i = 0; i < info[now].size(); i++) {
      edge e = info[now][i];
      string next_str = nowstr + e.cost;
      if (d[e.to][next_str.size()] == "" ||
          next_str < d[e.to][next_str.size()]) {
        d[e.to][next_str.size()] = next_str;
        que.push(P(next_str, e.to));
      }
    }
  }
  return "NO";
}
int main() {
  while (cin >> n >> a >> s >> g, n) {
    info.resize(n);
    for (int i = 0, x, y; i < a; i++) {
      string s;
      cin >> x >> y >> s;
      info[x].push_back(edge(y, s));
    }
    cout << Dijkstra() << endl;
    info.clear();
  }
}
Problem p00760 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int a[3][11][21];
  int n = 0;
  int m = 0;
  int y[10000][3];
  for (int i = 997; i < 1000; i++) {
    for (int j = 1; j < 11; j++) {
      if (j % 2 == 0 && i % 3 > 0) {
        for (int k = 1; k < 20; k++) {
          n++;
          a[i - 997][j][k] = n;
        }
      } else {
        for (int k = 1; k < 21; k++) {
          n++;
          a[i - 997][j][k] = n;
        }
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> y[i][0] >> y[i][1] >> y[i][2];
    cout << (999 - y[i][0]) / 3 * 590 + 591 -
                a[(y[i][0] - 1) % 3][y[i][1]][y[i][2]]
         << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a[10001][11][21];
  int n = 0;
  int m = 0;
  int y[10000][3];
  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 11; j++) {
      if (j % 2 == 0 && i % 3 > 0) {
        for (int k = 1; k < 20; k++) {
          n++;
          a[i][j][k] = n;
        }
      } else {
        for (int k = 1; k < 21; k++) {
          n++;
          a[i][j][k] = n;
        }
      }
    }
  }
  cin >> m;
  for (int i = 0; i < m; i++) {
    cin >> y[i][0] >> y[i][1] >> y[i][2];
    cout << a[1000][1][1] - a[y[i][0]][y[i][1]][y[i][2]] << endl;
  }
  return 0;
}
Problem p00760 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int computeDays(int y, int m, int d) {
  int days = 0;
  for (int nowy = 1; nowy < y; nowy++) {
    if (nowy % 3 == 0) {
      days += 20 * 10;
    } else {
      days += 20 * 5 + 19 * 5;
    }
  }
  for (int nowm = 1; nowm < m; nowm++) {
    if (y % 3 == 0) {
      days += 20;
    } else {
      if (nowm % 2 == 0) {
        days += 19;
      } else {
        days += 20;
      }
    }
  }
  return days + d;
}

int main(void) {
  int n;
  cin >> n;
  while (n--) {
    int y, m, d;
    cin >> y >> m >> d;
    cout << computeDays(1000, 1, 1) - computeDays(y, m, d) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

int main(void) {
  int n;
  int y[100], m[100], d[100];
  int year, month, day;
  int spent[100];
  int mirenium;

  for (int i = 0; i < 100; i++) {
    spent[i] = 0;
  }

  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> y[i];
    std::cin >> m[i];
    std::cin >> d[i];
  }

  for (int i = 0; i < n; i++) {
    year = 1000;
    month = 1;
    day = 1;
    while (1) {
      if (year == y[i] && month == m[i] && day == d[i]) {
        break;
      }

      if (day == 1) {
        if (month == 1) {
          year--;
          month = 10;
          if (year % 3 == 0 || month % 2 == 0) {
            day = 20;
          } else {
            day = 19;
          }
          spent[i]++;
          continue;
        } else {
          month--;
          if (year % 3 == 0 || month % 2 == 0) {
            day = 20;
          } else {
            day = 19;
          }
          spent[i]++;
          continue;
        }
      } else {
        day--;
        spent[i]++;
        continue;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    std::cout << spent[i] << std::endl;
  }
  return 0;
}
Problem p00760 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int a[3], b, c, m, n, x, ans;
  cin >> c;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < 3; j++)
      cin >> a[j];
    n = 333 - (a[0] / 3);
    m = 999 - a[0] - n;
    x = 10 - a[1];
    b = n * 200 + m * 195;

    if (a[0] % 3 != 0 && a[1] % 2 == 0)
      ans = (19 - a[2]) + (x / 2 * 39) + b + 1;
    else if (a[0] % 3 != 0 && a[1] % 2 != 0)
      ans = (20 - a[2]) + (x / 2 * 20) + (x / 2 + 1) * 19 + b + 1;
    else if (a[0] % 3 == 0)
      ans = 20 - a[2] + x * 20 + b + 1;

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

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, a[3], b, b1;

  while (cin >> n) {
    if (n <= 0 && n > 100)
      break;
    for (int i = 0; i < n; i++) {

      for (int j = 0; j < 3; j++)
        cin >> a[j];

      if (a[0] % 3 == 0) {
        a[2] = 20 - a[2];
        a[1] = 10 - a[1];
        b = a[1] * 20 + a[2];
      }

      else {
        if (a[1] % 2 == 0) {
          a[2] = 19 - a[2];
          a[1] = 10 - a[1];
          for (int i = 0; i <= 4; i++)
            if (a[1] == i * 2) {
              break;
              b = a[2] + 20 * i + 19 * i;
            }
        } else {
          a[2] = 20 - a[2];
          a[1] = 10 - a[1];
          for (int i = 1; i <= 9; i + 2)
            if (a[1] == i) {
              break;
              i = i / 2;
              b = a[2] + 20 * i + 19 * (i + 1);
            }
        }
      }
      cout << b << endl;
    }
  }
  return 0;
}
Problem p00760 (C++) — buggy submission, verdict: Runtime Error

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

int Z, Y, M, D;

void solve(void);

inline bool is_leap(int y) { return y % 3 == 0; }
inline bool is_big(int m) { return m % 2 == 1; }

int main() {
  cin >> Z;
  for (int zi = 1; zi <= Z; ++zi) {
    cin >> Y >> M >> D;
    solve();
  }

  return 0;
}

void solve(void) {
  int res = 0;

  while (Y != 1000) {
    ++D;
    if (is_leap(Y)) {
      if (D > 20)
        D = 1, ++M;
    } else {
      if (is_big(M)) {
        if (D > 20)
          D = 1, ++M;
      } else {
        if (D > 19)
          D = 1, ++M;
      }
    }

    if (M > 10)
      M = 1, ++Y;

    ++res;
  }

  cout << res << "\\n";
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::replace: __pos (which is 18446744073709551615) > this->size() (which is 2)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
using namespace std;

int L;
string A;
map<string, int> vst;

void solve(void);

int main() {
  while (cin >> A >> L && (A != "0" || L != 0))
    solve();

  return 0;
}

void solve(void) {
  string A0(L, '0'), pre;
  int mini, maxi, diffi, cnt = 0;

  A0.replace(L - A.length(), A.length(), A);
  vst.clear(), vst[A0] = 0;

  pre = A0;
  do {
    string maxs, mins, diffs;
    mins = pre;
    sort(mins.begin(), mins.end());
    maxs = mins;
    reverse(maxs.begin(), maxs.end());

    istringstream iss1(mins), iss2(maxs);
    ostringstream oss;

    iss1 >> mini, iss2 >> maxi, diffi = maxi - mini;
    oss.fill('0'), oss.width(L), oss << diffi, diffs = oss.str(), ++cnt;
    if (vst.count(diffs) > 0) {
      cout << vst[diffs] << " " << diffi << " " << cnt - vst[diffs] << "\\n";
      break;
    }
    vst[diffs] = cnt, pre = diffs;
  } while (true);
}
Problem p00760 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int sum(int y, int m, int d) {
  int t = 0;
  t += (y - 1) * 195 + (y - 1) / 3 * 5;
  t += (m - 1) * 19 + m / 2;
  if (y % 3 == 0)
    t += (m - 1) / 2;
  t += d - 1;
  return t;
}

int main() {
  int n;
  cin >> n;
  while (n--) {
    int y, m, d;
    cin >> y >> m >> d;

    cout << sum(1000, 1, 1) - sum(y, m, d) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int sum(int y, int m, int d) {
  int t = 0;
  t += (y - 1) * 195 + (y - 1) / 3 * 5;
  t += (m - 1) * 19 + m / 2;
  if (y % 3 == 0)
    t += (m - 1) / 2;
  t += d - 1;
  return t;
}

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

      cout << sum(1000, 1, 1) - sum(y, m, d) << endl;
    }
  }
  return 0;
}
Problem p00760 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

using namespace std;

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

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

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n;
int main() {
  cin >> n;
  int year, m, day;
  vector<int> kai;
  for (int i = 0; i < n; i++) {
    int sum = 1;
    cin >> year >> m >> day;
    if (year % 3 == 0) {
      sum += (10 - m) * 20 + (20 - day);
    } else {
      sum += (5 * 19 + 5 * 20);
      for (int i = 1; i < m; i++) {
        if (i % 2 == 0) {
          sum -= 19;
        } else {
          sum -= 20;
        }
      }
      if (m % 2 == 0) {
        sum -= day;
      } else {
        sum -= day;
      }
    }
    if (i < 999) {
      for (int i = year + 1; i < 1000; i++) {
        if (i % 3 == 0) {
          sum += (10 * 20);
        } else {
          sum += (5 * 19 + 5 * 20);
        }
      }
    }
    kai.push_back(sum);
  }
  for (int i = 0; i < kai.size(); i++) {
    cout << kai[i] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

using namespace std;

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

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

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m, p;
int main() {
  int x;
  while (cin >> n >> m >> p, n || m || p) {
    map<int, int> ma;
    int sum = 0;
    for (int i = 1; i <= n; i++) {
      cin >> x;
      ma[i] += x;
      sum += x * 100;
    }
    sum -= (sum * p) / 100;
    if (ma[m] == 0)
      sum = 0;
    else {
      sum /= ma[m];
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p00760 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int A[1010][12][22];
int main() {
  int a = 1, b = 1, c = 0, t = 0;
  while (a < 1000) {
    ++c;
    if (a % 3 != 0) {
      if (b % 2 == 1) {
        if (c > 20)
          c = 1, ++b;
      } else if (c > 19)
        c = 1, ++b;
    } else if (c > 20)
      c = 1, ++b;

    if (b == 11)
      ++a, b = 1;
    ++t;
    A[a][b][c] = t;
  }
  int N;
  scanf("%d", &N);
  while (N--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\\n", 196471 - A[a][b][c]);
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int A[1010][11][22];
int main() {
  int a = 1, b = 1, c = 0, t = 0;
  while (a < 1000) {
    ++c;
    if (a % 3 != 0)
      if (b % 2 == 1)
        if (c > 20)
          c = 1, ++b;
        else if (c > 19)
          c = 1, ++b;

        else if (c > 20)
          c = 1, ++b;

    if (b == 11)
      ++a, b = 1;
    ++t;
    A[a][b][c] = t;
  }
  int N;
  scanf("%d", &N);
  while (N--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\\n", 196471 - A[a][b][c]);
  }
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

// 18
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  for (int n, l; cin >> n >> l, n | l;) {
    stringstream ss;
    ss << setw(l) << setfill('0') << n;
    string a[21];
    a[0] = ss.str();
    for (int i = 1;; i++) {
      string h = a[i - 1], lo = a[i - 1];
      sort(h.begin(), h.end());
      sort(lo.rbegin(), lo.rend());
      stringstream st;
      st << setw(l) << setfill('0') << atoi(lo.c_str()) - atoi(h.c_str());
      a[i] = st.str();
      string *p = find(a, a + i, a[i]);
      if (p != a + i) {
        cout << p - a << ' ' << atoi(p->c_str()) << ' ' << a + i - p << endl;
        break;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

// 18
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  for (int n, l; cin >> n >> l, n | l;) {
    stringstream ss;
    ss << setw(l) << setfill('0') << n;
    string a[20];
    a[0] = ss.str();
    for (int i = 1;; i++) {
      string h = a[i - 1], lo = a[i - 1];
      sort(h.begin(), h.end());
      sort(lo.rbegin(), lo.rend());
      stringstream st;
      st << setw(l) << setfill('0') << atoi(lo.c_str()) - atoi(h.c_str());
      a[i] = st.str();
      string *p = find(a, a + i, a[i]);
      if (p != a + i) {
        cout << p - a << ' ' << atoi(p->c_str()) << ' ' << a + i - p << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00761 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define pii pair<int, pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x) < 0 ? (-(x)) : (x))
#define xx(x) ((x) * (x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(), (x).end()
#define Max (1 << 30)
#define LLMax (1ll << 60)
template <class T> string ToString(T t) {
  stringstream s;
  s << t;
  return s.str();
}
template <class T> void ToOther(T &t, string a) {
  stringstream s(a);
  s >> t;
}

int n, L;
int ck[11000000];
int TT[11000000];

int NEXT(int x, int L) {
  string r = ToString(x);
  while (r.size() < L)
    r += '0';
  string r2 = r;
  sort(ALL(r), greater<char>());
  sort(ALL(r2));
  int A, B;
  ToOther(A, r);
  ToOther(B, r2);
  return A - B;
}

int main() {
  int CC = 0;
  while (cin >> n >> L) {
    CC++;
    if (n == 0 && L == 0)
      break;

    for (int i = 0;; i++) {
      if (TT[n] == CC) {
        cout << ck[n] << " " << n << " " << i - ck[n] << endl;
        goto E;
      }
      TT[n] = CC;
      ck[n] = i;
      n = NEXT(n, L);
    }
  E:;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define pii pair<int, pi>
#define X first
#define Y second
#define pb push_back
#define ab(x) ((x) < 0 ? (-(x)) : (x))
#define xx(x) ((x) * (x))
#define mp make_pair
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vpi vector<pi>
#define vpll vector<pll>
#define ALL(x) (x).begin(), (x).end()
#define Max (1 << 30)
#define LLMax (1ll << 60)
template <class T> string ToString(T t) {
  stringstream s;
  s << t;
  return s.str();
}
template <class T> void ToOther(T &t, string a) {
  stringstream s(a);
  s >> t;
}

int n, L;
int ck[10000000];

int NEXT(int x, int L) {
  string r = ToString(x);
  while (r.size() < L)
    r += '0';
  string r2 = r;
  sort(ALL(r), greater<char>());
  sort(ALL(r2));
  int A, B;
  ToOther(A, r);
  ToOther(B, r2);
  return A - B;
}

int main() {
  while (cin >> n >> L) {
    if (n == 0 && L == 0)
      break;
    memset(ck, -1, sizeof(ck));

    for (int i = 0;; i++) {
      if (ck[n] != -1) {
        cout << ck[n] << " " << n << " " << i - ck[n] << endl;
        goto E;
      }
      ck[i] = i;
      n = NEXT(n, L);
    }
  E:;
  }
}
Problem p00761 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int L;
int ch(string s) {
  int t = 0;
  for (int i = L - 1, j = 1; i >= 0; i--, j *= 10)
    t += (s[i] - '0') * j;
  return t;
}
int main() {
  string a[32], b, c, z;
  while (cin >> a[0] >> L) {
    if (L == 0)
      break;
    for (int j = 0; j < L - a[0].size(); j++)
      z += '0';
    a[0] = z + a[0];
    z.clear();
    int i = 0, k = 1;
    while (k) {
      b = a[i];
      sort(b.begin(), b.end());
      c = b;
      reverse(c.begin(), c.end());
      i++;
      for (int j = 0; j < L; j++) {
        int t = b[j], u = c[j];
        if (t - u < 0)
          b[j + 1]--, a[i] += t - u + 10 + '0';
        else
          a[i] += t - u + '0';
      }
      reverse(a[i].begin(), a[i].end());
      for (int j = 0; j < i; j++)
        if (a[i] == a[j])
          cout << j << ' ' << ch(a[i]) << ' ' << i - j << endl, k = 0;
      if (k == 0)
        for (int j = 0; j <= i; j++)
          a[j].clear();
    }
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  string a[32], b, c, z;
  int L;
  while (cin >> a[0] >> L) {
    if (L == 0)
      break;
    for (int j = 0; j < L - a[0].size(); j++)
      z += '0';
    a[0] = z + a[0];
    int i = 0, k = 1;
    while (k) {
      b = a[i];
      sort(b.begin(), b.end());
      c = b;
      reverse(c.begin(), c.end());
      i++;
      for (int j = 0; j < L; j++) {
        int t = b[j], u = c[j];
        if (t - u < 0)
          b[j + 1]--, a[i] += t - u + 10 + '0';
        else
          a[i] += t - u + '0';
      }
      reverse(a[i].begin(), a[i].end());
      for (int j = 0; j < i; j++)
        if (a[i] == a[j])
          cout << j << ' ' << a[i] << ' ' << i - j << endl, k = 0;
    }
  }
  return 0;
}