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

#include <stdio.h>
int main(void) {
  int n, k, j, P, Q, R, C, flg;
  int i[1001], p1[101], q1[101], r1[101], c[1701], p[1001], q[1001], r[1001];
  flg = 0;
  scanf("%d", &n);
  while (n != 0) {
    for (j = 1; j <= n; j++) {
      scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
      p1[j] = p[j] * 4;
      q1[j] = q[j] * 9;
      r1[j] = r[j] * 4;
      c[j] = p1[j] + q1[j] + r1[j];
    }
    scanf("%d %d %d %d", &P, &Q, &R, &C);
    for (j = 1; j <= n; j++) {
      if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
        printf("%d\\n", i[j]);
        flg = 1;
      }
    }
    if (flg == 0) {
      printf("NA\\n");
    }
    flg = 0;
    scanf("%d", &n);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
int main(void) {
  int n, k, j, P, Q, R, C, flg;
  int i[1001], p1[101], q1[101], r1[101], c[1701], p[101], q[101], r[101];
  flg = 0;
  scanf("%d", &n);
  while (n != 0) {
    for (j = 1; j <= n; j++) {
      scanf("%d %d %d %d", &i[j], &p[j], &q[j], &r[j]);
      p1[j] = p[j] * 4;
      q1[j] = q[j] * 9;
      r1[j] = r[j] * 4;
      c[j] = p1[j] + q1[j] + r1[j];
    }
    scanf("%d %d %d %d", &P, &Q, &R, &C);
    for (j = 1; j <= n; j++) {
      if (p[j] <= P && q[j] <= Q && r[j] <= R && c[j] <= C) {
        printf("%d\\n", i[j]);
        flg = 1;
      }
    }
    if (flg == 0) {
      printf("NA\\n");
    }
    flg = 0;
    scanf("%d", &n);
  }
  return 0;
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  int n, y, b, r, t;

  while (1) {

    cin >> n;

    if (n == 0)
      break;

    cin >> y;

    int ans = 0;
    double maxg = 0.0;

    for (int i = 0; i < n; i++) {
      double bank;
      cin >> b >> r >> t;
      if (t == 1) {
        bank = 1.0 + (double)(y) * (double)(r) / 100.0;
      } else {
        bank = 1.0;
        double base = 1.0 + (double)(r) / 100.0;
        for (int j = 0; j < y; j++) {
          bank = bank * base;
        }
      }

      if (bank >= maxg) {
        ans = b;
        maxg = bank;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

double bank[50];

int main() {

  int n, y, b, r, t;

  while (1) {

    cin >> n;

    if (n == 0)
      break;

    cin >> y;

    for (int i = 0; i < n; i++) {
      cin >> b >> r >> t;
      b--;
      if (t == 1) {
        bank[b] = 1.0 + (double)(y) * (double)(r) / 100.0;
      } else {
        bank[b] = 1.0;
        double base = 1.0 + (double)(r) / 100.0;
        for (int j = 0; j < y; i++) {
          bank[b] = bank[b] * base;
        }
      }
      // cout << bank[b] << endl;
    }

    int ans = 0;
    long double maxg = -0.1;

    for (int i = 0; i < n; i++) {
      if (maxg < bank[i]) {
        maxg = bank[i];
        ans = i + 1;
      }
    }

    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double y;
    cin >> y;

    pair<int, double> p;
    auto f1 = [](double y, double r) { return 1 + y * r / 100; };
    auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
    rep(i, n) {
      int b, t;
      double r;
      cin >> b >> r >> t;
      if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
        p.first = b;
        p.second = (t == 1 ? f1(y, r) : f2(y, r));
      }
    }
    cout << p.first << endl;
  }
}

Error: 0
t == 1 ? f1(y,r) : f2(y,r) = 1.47746
t == 1 ? f1(y,r) : f2(y,r) = 1.48
t == 1 ? f1(y,r) : f2(y,r) = 1.55133
t == 1 ? f1(y,r) : f2(y,r) = 1.54


Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int main() {
  int n;
  while (cin >> n, n) {
    double y;
    cin >> y;

    pair<int, double> p;
    auto f1 = [](double y, double r) { return 1 + y * r / 100; };
    auto f2 = [](double y, double r) { return pow(1 + r / 100, y); };
    rep(i, n) {
      int b, t;
      double r;
      cin >> b >> r >> t;
      show(t == 1 ? f1(y, r) : f2(y, r));
      if (p.second < (t == 1 ? f1(y, r) : f2(y, r))) {
        p.first = b;
        p.second = (t == 1 ? f1(y, r) : f2(y, r));
      }
    }
    cout << p.first << endl;
  }
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main() {
  int n, y, b, r, t, i, j, a;
  double x, u = 0;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    scanf("%d", &y);
    u = b = 1;
    for (i = 0; i < n; i++) {
      scanf("%d%d%d", &b, &r, &t);
      if (t == 1) {
        x = 1 + y * r * 0.01;
      } else {
        x = 1;
        for (j = 0; j < y; j++) {
          x = x * (1 + r * 0.01);
        }
      }
      if (x > u) {
        u = x;
        a = b;
      }
    }
    printf("%d\\n", a);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int i, j, n, b, t, y, cnt = 0;
  float many[52] = {0}, r, suu, max = 0;
  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    scanf("%d", &y);
    scanf("%d %f %d", &b, &r, &t);
    if (t == 1) {
      many[b] = 100.0 * (1 + y * (float)r / 100);
    } else {
      many[b] = 100.0 * (1 + (float)y / 100);
      suu = many[b];
      for (j = 0; j < y; j++) {
        many[b] = suu * many[b];
      }
    }
    for (i = 0; i < 50; i++) {
      if (many[i] > max) {
        max = many[i];
        cnt = i;
      }
    }
    printf("%d\\n", cnt);
  }
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main(void) {
  int n, gin, kinri, i, m, maxgin;
  double y, nenri, ganri, max, d;
  while (1) {
    m = 10000;
    cin >> n;
    if (n == 0)
      break;
    cin >> y;
    max = -1;
    for (i = 0; i < n; i++) {
      cin >> gin >> nenri >> kinri;
      if (kinri == 1)
        ganri = m * (1 + y * (nenri / 100));
      if (kinri == 2) {
        d = (1 + nenri / 100);
        d = pow(d, y);
        ganri = m * d;
      }
      if (max < ganri) {
        max = ganri;
        maxgin = gin;
      }
    }
    cout << maxgin << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
int main(void) {
  int n, gin, kinri, i, m, maxgin, cpy;
  double y, nenri, ganri, max, d;
  while (1) {
    m = 10000;
    cin >> n;
    if (n == 0)
      break;
    cin >> y;
    max = -1;
    for (i = 0; i < n; i++) {
      cin >> gin >> nenri >> kinri;
      if (kinri == 1)
        ganri = m * (1 + y * (nenri / 100));
      if (kinri == 2) {
        d = (1 + nenri / 100);
        cpy = d;
        for (i = 0; i < y; i++)
          d = d * cpy;
        ganri = m * d;
      }
      if (max < ganri) {
        max = ganri;
        maxgin = gin;
      }
    }
    cout << maxgin << endl;
  }
  return 0;
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int b[50], j, i, x, t, n;
  float k, k2, sum[50], max, syo, y, r;
  while (1) {
    max = -1;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%f", &y);
    for (i = 0; i < n; i++) {
      scanf("%d %f %d", &b[i], &r, &t);
      //			printf("%d\\n",r);
      if (t == 1) {
        syo = r / 100;
        sum[i] = 10000 * (1 + y * syo);
      }
      if (t == 2) {
        syo = r / 100;
        k = 1 + syo;
        k2 = k;
        //				printf("%f\\n",k);
        for (j = 0; j < y - 1; j++) {
          k = k * k2;
        }
        sum[i] = 10000 * k;
      }
    }
    for (i = 0; i < n; i++) {
      //			printf("%f\\n",sum[i]);
      if (max < sum[i]) {
        max = sum[i];
        x = b[i];
      }
    }
    printf("%d\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int n, y, b[50], k, k2, j, x, r, t, sum[50], max, i;
  while (1) {
    max = -1;
    scanf("%d", &n);
    scanf("%d", &y);
    for (i = 0; i < n; i++) {
      scanf("%d %d %d", &b[i], &r, &t);
      if (t == 1) {
        sum[i] = 100 * (1 + y * (r / 100));
      }
      if (t == 2) {
        k = 1 + r / 100;
        k2 = k;
        for (j = 0; j < y; j++) {
          k2 = k2 * k;
        }
        sum[j] = 100 * k;
      }
    }
    for (i = 0; i < n; i++) {
      if (max < sum[i]) {
        max = sum[i];
        x = b[i];
      }
    }
    printf("%d\\n", x);
  }
  return 0;
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int n;
  double y;
  int ansb = 0;
  int b[101], t[101], r[101];
  double ans[101];
  while (1) {
    double res = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    scanf("%lf", &y);
    for (int a = 0; a < n; a++) {
      scanf("%d%d%d", &b[a], &r[a], &t[a]);
      if (t[a] == 1)
        ans[a] = (double)r[a] * y * 0.01 + 1;
      if (t[a] == 2)
        ans[a] = pow(1 + r[a] * 0.01, y);
      if (ans[a] > res)
        ansb = b[a], res = ans[a];
    }
    printf("%d\\n", ansb);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  double y;
  int ansb = 0;
  int b[101], t[101], r[101];
  double ans[101];
  double res = 0;
  while (1) {
    scanf("%d", &n);
    scanf("%lf", &y);
    for (int a = 0; a < n; a++) {
      scanf("%d%d%d", &b[a], &r[a], &t[a]);
      if (t[a] == 1)
        ans[a] = (double)r[a] * y * 0.01 + 1;
      if (t[a] == 2)
        ans[a] = pow(1 + r[a] * 0.01, y);
      if (ans[a] > res)
        ansb = b[a], res = ans[a];
    }
    printf("%d\\n", ansb);
  }
  return 0;
}
Problem p00240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

class Bank {
public:
  int id, r, t;
  Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};

int main() {
  while (true) {
    int n;
    cin >> n;

    if (n == 0)
      break;

    int y;
    cin >> y;

    vector<Bank> vb;
    for (int i = 0; i < n; ++i) {
      int b, r, t;
      cin >> b >> r >> t;

      vb.push_back(Bank(b, r, t));
    }

    // ganpon * (1 + y * r/100)
    // ganpon * (1 + r/100)^y

    double maxi = 0;
    int ans = -1;

    for (int i = 0; i < vb.size(); ++i) {
      double g = 100000;
      double r1 = g * (1. + y * vb[i].r / 100.);
      double r2 = g * pow(1 + vb[i].r / 100., y);
      if (vb[i].t == 1) {
        if (maxi < r1) {
          maxi = r1;
          ans = vb[i].id;
        }
      } else {
        if (maxi < r2) {
          maxi = r2;
          ans = vb[i].id;
        }
      }
    }

    cout << ans << endl;
  }
}

// 1

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

class Bank {
public:
  int id, r, t;
  Bank(int id, int r, int t) : id(id), r(r), t(t) {}
};

int main() {
  while (true) {
    int n;
    cin >> n;
    int y;
    cin >> y;

    vector<Bank> vb;
    for (int i = 0; i < n; ++i) {
      int b, r, t;
      cin >> b >> r >> t;

      vb.push_back(Bank(b, r, t));
    }

    // ganpon * (1 + y * r/100)
    // ganpon * (1 + r/100)^y

    double maxi = 0;
    int ans = -1;

    for (int i = 0; i < vb.size(); ++i) {
      double g = 100000;
      double r1 = g * (1. + y * vb[i].r / 100.);
      double r2 = g * pow(1 + vb[i].r / 100., y);
      if (vb[i].t == 1) {
        if (maxi < r1) {
          maxi = r1;
          ans = vb[i].id;
        }
      } else {
        if (maxi < r2) {
          maxi = r2;
          ans = vb[i].id;
        }
      }
    }

    cout << ans << endl;
  }
}

// 1
Problem p00241 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main(void) {
  int n, i, x1, y1, z1, w1, x2, y2, z2, w2, x[1000], y[1000], z[1000], w[1000];
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%d %d %d %d %d %d %d %d", &x1, &y1, &z1, &w1, &x2, &y2, &z2, &w2);
      x[i] = (x1 * x2) + (y1 * y2 * -1) + (z1 * z2 * -1) + (w1 * w2 * -1);
      y[i] = (x1 * y2) + (y1 * x2) + (z1 * w2) + (w1 * z2 * -1);
      z[i] = (x1 * z2) + (y1 * w2 * -1) + (z1 * x2) + (w1 * y2);
      w[i] = (x1 * w2) + (y1 * z2) + (z1 * y2 * -1) + (w1 * x2);
    }
    for (i = 0; i < n; i++) {
      printf("%d %d %d %d\\n", x[i], y[i], z[i], w[i]);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main(void) {
  int x1, y1, z1, w1, x2, y2, z2, w2, x, y, z, w;
  while (1) {
    scanf("%d", &x1);
    if (x1 == 0)
      break;
    scanf("%d %d %d %d %d %d %d", &y1, &z1, &w1, &x2, &y2, &z2, &w2);
    x = (x1 * x2) + (y1 * y2 * -1) + (z1 * z2 * -1) + (w1 * w2 * -1);
    y = (x1 * y2) + (y1 * x2) + (z1 * w2) + (w1 * z1 - 1);
    z = (x1 * z2) + (y1 * w2 * -1) + (z1 * x2) + (w1 * y2 * -1);
    w = (x1 * w2) + (y1 * z2) + (z1 * y2 * -1) + (w1 * x2);
    printf("%d %d %d %d\\n", x, y, z, w);
  }
  return 0;
}
Problem p00241 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;
int main() {

  int n, fi, se, th, fo, x1, x2, y1, y2, z1, z2, w1, w2;
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &z1, &w1, &x2, &y2, &z2, &w2);
      fi = x1 * x2 + (-1) * (y1 * y2) + (-1) * (z1 * z2) + (-1) * (w1 * w2);
      se = x1 * y2 + y1 * x2 + z1 * w2 + (-1) * w1 * z2;

      th = x1 * z2 + (-1) * (y1 * w2) + z1 * x2 + w1 * y2;
      fo = x1 * w2 + y1 * z2 + (-1) * (z1 * y2) + w1 * x2;

      printf("%d %d %d %d\\n", fi, se, th, fo);
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
using namespace std;
int main() {

  int n, fi, se, th, fo, x1, x2, y1, y2, z1, z2, w1, w2;
  while (true) {
    scanf("%d", n);
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d%d%d%d%d%d%d%d", &x1, &y1, &z1, &w1, &x2, &y2, &z2, &w2);
      fi = x1 * x2 + (-1) * (y1 * y2) + (-1) * (z1 * z2) + (-1) * (w1 * w2);
      se = x1 * y2 + y1 * x2 + z1 * w2 + (-1) * w1 * z2;

      th = x1 * z2 + (-1) * (y1 * w2) + z1 * x2 + w1 * y2;
      fo = x1 * w2 + y1 * z2 + (-1) * (z1 * y2) + w1 * x2;

      printf("%d %d %d %d", fi, se, th, fo);
    }
  }
  return 0;
}
Problem p00241 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
using namespace std;

int main() {
  int a[4], b[4];
  int n;
  while (1) {
    cin >> n;
    if (!n)
      break;
    while (n) {
      for (int i = 0; i < 4; i++)
        cin >> a[i];
      for (int i = 0; i < 4; i++)
        cin >> b[i];

      cout << a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3] << " "
           << a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2] << " "
           << a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1] << " "
           << a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0] << endl;
      n--;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a[4], b[4];
  int n;
  cin >> n;
  while (true) {
    cin >> a[0];
    if (!a[0])
      break;
    for (int i = 1; i < 4; i++)
      cin >> a[i];
    for (int i = 0; i < 4; i++)
      cin >> b[i];

    cout << a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3] << " "
         << a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2] << " "
         << a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1] << " "
         << a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0] << endl;
  }
}
Problem p00241 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n;
  int r, rr, i, ii, j, jj, k, kk;
  int rout, iout, jout, kout;

  while (cin >> n, n) {
    for (int num = 0; num < n; num++) {
      cin >> r >> i >> j >> k >> rr >> ii >> jj >> kk;

      rout = r * rr - i * ii - j * jj - k * kk;
      iout = r * ii + i * rr + j * kk - k * jj;
      jout = r * jj - i * kk + j * rr + k * ii;
      kout = r * kk + i * jj - j * ii + k * rr;

      cout << rout << " " << iout << " " << jout << " " << kout << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n1, i1, j1, k1;
  int n2, i2, j2, k2;

  while (cin >> n1, n1) {
    cin >> i1 >> j1 >> k1;
    cin >> n2 >> i2 >> j2 >> k2;

    cout << n1 * n2 - i1 * i2 - j1 * j2 - k1 * k2 << " ";
    cout << n1 * i2 + n2 * i1 + j1 * k2 - j2 * k1 << " ";
    cout << n1 * j2 + n2 * j1 - i1 * k2 + i2 * k1 << " ";
    cout << n1 * k2 + n2 * k1 + i1 * j2 - i2 * j1 << endl;
  }
}
Problem p00242 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// #define int long long
#define pb push_back
#define print(Target) cout << Target << '\\n'
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, a, n) for (int i = (a); i < (n); i++)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vector<int>> vii;

bool is_uruu(int y) { return (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)); }

struct DIC {
  string s;
  int cnt;
};

bool cmp(DIC lhs, DIC rhs) {
  if (lhs.cnt != rhs.cnt) {
    return (lhs.cnt > rhs.cnt);
  } else {
    return (lhs.s < rhs.s);
  }
}

signed main() {
  // ios::sync_with_stdio(false);
  int n;
  while (cin >> n, n) {
    vector<DIC> dic;
    REP(i, n) {
      string input;
      while (cin >> input) {
        bool same = false;
        REP(j, dic.size()) {
          if (input == dic[j].s) {
            same = true;
            dic[j].cnt++;
            break;
          }
        }
        if (!same) {
          dic.pb((DIC){input, 1});
        }
        char c;
        if (scanf("%c", &c), c == '\\n')
          break;
      }
    }
    string t;
    cin >> t;
    sort(dic.begin(), dic.end(), cmp);

    int w = 0;
    bool first = false;
    for (int i = 0; w < 5 && i < dic.size(); i++) {
      if (t[0] == dic[i].s[0]) {
        if (first) {
          cout << " ";
        }
        cout << dic[i].s;
        w++;
        first = true;
      }
    }
    if (!w) {
      cout << "NA";
    }
    print("");
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  string input;
  vector<string> dic[256];
  int n;

  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      while (cin >> input) {
        dic[input[0]].push_back(input);
      }
    }
    char c;
    scanf("%c", &c);
    sort(dic[c].begin(), dic[c].end());
    if (dic[c].empty()) {
      puts("NA");
    } else {
      for (int i = 0; i < dic[c].size() - 1; i++) {
        cout << dic[c][i] << " ";
      }
      cout << dic[c][dic[c].size() - 1] << endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
string S;
int main() {
  int n;
  while (true) {
    cin >> n;
    vector<string> vec;
    vector<pair<int, string>> w;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      S = "";
      while (S == "") {
        getline(cin, S);
      }
      S += " ";
      string T;
      for (int i = 0; i < S.size(); i++) {
        if (S[i] == ' ') {
          vec.push_back(T);
          T = "";
        } else {
          T += S[i];
        }
      }
    }
    char c;
    cin >> c;
    sort(vec.begin(), vec.end());
    if (vec[0][0] == c)
      w.push_back(make_pair(-1, vec[0]));
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i][0] == c) {
        if (vec[i] == vec[i - 1]) {
          w[w.size() - 1].first--;
        } else {
          w.push_back(make_pair(-1, vec[i]));
        }
      }
    }
    sort(w.begin(), w.end());
    if (w.size() == 0) {
      cout << "NA";
    } else {
      for (int i = 0; i < w.size(); i++) {
        if (i >= 5) {
          break;
        }
        if (i) {
          cout << ' ';
        }
        cout << w[i].second;
      }
    }
    cout << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string S;
int main() {
  int n;
  while (true) {
    cin >> n;
    vector<string> vec, w;
    for (int i = 0; i < n; i++) {
      S = "";
      while (S == "") {
        getline(cin, S);
      }
      S += " ";
      string T;
      for (int i = 0; i < S.size(); i++) {
        if (S[i] == ' ') {
          vec.push_back(T);
          T = "";
        } else {
          T += S[i];
        }
      }
    }
    char c;
    cin >> c;
    sort(vec.begin(), vec.end());
    if (vec[0][0] == c) {
      w.push_back(vec[0]);
    }
    for (int i = 1; i < vec.size(); i++) {
      if (vec[i][0] == c && vec[i - 1] != vec[i]) {
        w.push_back(vec[i]);
      }
    }
    for (int i = 0; i < w.size(); i++) {
      if (i)
        cout << ' ';
      cout << w[i];
    }
    if (w.size() == 0) {
      cout << "NA";
    }
    cout << endl;
  }
}
Problem p00242 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

typedef pair<int, string> P;
vector<string> IN;
vector<P> V;
char c;

void make(string s) {
  s += " ";
  string in = "";
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == ' ') {
      IN.push_back(in);
      // cout << in << endl;
      in = "";
      continue;
    }
    in += s[i];
  }
}

string gets(char key) {

  for (int i = 0; i < IN.size(); i++)
    if (IN[i][0] == key)
      return IN[i];

  return "Cannot find";
}
void solve() {

  IN.push_back("{{{}}}");
  sort(IN.begin(), IN.end());

  // string a = "";
  // a += c;
  // string s = *lower_bound(IN.begin(),IN.end(),a);
  string s = gets(c);
  if (s == "Cannot find") {
    cout << "NA" << endl;
    return;
  }
  while (1) {
    P p;
    p.first = -1 * (upper_bound(IN.begin(), IN.end(), s) -
                    lower_bound(IN.begin(), IN.end(), s));
    p.second = s;
    V.push_back(p);
    s = *upper_bound(IN.begin(), IN.end(), s);
    if (s[0] != c)
      break;
  }

  sort(V.begin(), V.end());

  for (int i = 0; i < min((int)V.size(), 5); i++) {
    if (i)
      cout << " ";
    cout << V[i].second;
  }
  cout << endl;
}

void init() {

  IN.clear();
  V.clear();
}

int main() {
  int n;
  while (cin >> n && n) {
    init();
    string in;
    getline(cin, in);
    for (int i = 0; i < n; i++) {
      getline(cin, in);
      make(in);
    }
    cin >> c;
    solve();
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

typedef pair<int, string> P;
vector<string> IN;
vector<P> V;
char c;

void make(string s) {
  s += " ";
  string in = "";
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == ' ') {
      IN.push_back(in);
      // cout << in << endl;
      in = "";
      continue;
    }
    in += s[i];
  }
}

string gets(char key) {

  for (int i = 0; i < IN.size(); i++)
    if (IN[i][0] == key)
      return IN[i];

  return "Cannot find";
}
void solve() {
  sort(IN.begin(), IN.end());

  // string a = "";
  // a += c;
  // string s = *lower_bound(IN.begin(),IN.end(),a);
  string s = gets(c);
  if (s == "Cannot find") {
    cout << "NA" << endl;
    return;
  }

  while (1) {
    P p;
    p.first = -1 * (upper_bound(IN.begin(), IN.end(), s) -
                    lower_bound(IN.begin(), IN.end(), s));
    p.second = s;
    V.push_back(p);
    s = *upper_bound(IN.begin(), IN.end(), s);
    if (s[0] != c)
      break;
  }

  sort(V.begin(), V.end());

  for (int i = 0; i < min((int)V.size(), 5); i++) {
    if (i)
      cout << " ";
    cout << V[i].second;
  }
  cout << endl;
}

void init() {

  IN.clear();
  V.clear();
}

int main() {
  int n;
  while (cin >> n && n) {
    init();
    string in;
    getline(cin, in);
    for (int i = 0; i < n; i++) {
      getline(cin, in);
      make(in);
    }
    cin >> c;
    solve();
  }
  return 0;
}