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

#include <iostream>
using namespace std;
int main() {
  int n;
  int m;
  int b;
  int c;
  int d;
  int e;
  while (cin >> n) {
    cin >> m;
    if (n == 0) {
      break;
    }
    int a[m];
    b = 0;
    while (b < m) {
      a[b] = 0;
      b = b + 1;
    }
    b = 0;
    while (b < n) {
      c = 0;
      while (c < m) {
        cin >> d;
        if (d == 1) {
          a[c] = a[c] + 1;
        }
        c = c + 1;
      }
      b = b + 1;
    }
    e = 0;
    b = n;
    while (b >= 0) {
      c = 0;
      while (c < m) {
        if (a[c] == b) {
          cout << c + 1;
          e = e + 1;
          if (e == m) {
            cout << endl;
          } else {
            cout << " ";
          }
        }
        c = c + 1;
      }
      b = b - 1;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  int m;
  int b;
  int c;
  int d;
  while (cin >> n) {
    cin >> m;
    if (n == 0) {
      break;
    }
    vector<int> a(m);
    b = 0;
    while (b < m) {
      a[b] = 0;
      b = b + 1;
    }
    b = 0;
    while (b < n) {
      c = 0;
      while (c < m) {
        cin >> d;
        if (d == 1) {
          a[c] = a[c] + 1;
        }
        c = c + 1;
      }
      b = b + 1;
    }
    stable_sort(a.begin(), a.end(), greater<int>());
    b = 0;
    while (b < m) {
      cout << a[b];
      if (b == m - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
}
Problem p00428 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    int j;
    int w[1100] = {};
    for (int i = 0; i < n; i++)
      for (int k = 0; k < m; k++) {
        cin >> j;
        w[k] += j;
      }
    j = 0;
    for (int i = 0; i < m; i++) {
      for (int k = 0; k < m; k++) {
        if (w[j] < w[k])
          j = k;
      }
      cout << j + 1;
      if (i != m - 1)
        cout << " ";
      w[j] = -1;
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n, m;

    cin >> n >> m;
    int su[1200] = {}, hage[1200] = {};
    for (int i = 0; i < n; i++) {
      for (int s = 0; s < m; s++) {
        int q;
        cin >> q;
        su[s] += q;
        hage[s] += q;
      }
    }
    sort(su, su + m);
    for (int i = 0; i < m; i++) {
      for (int c = m - 1; c != -1; c--) {
        if (su[c] == hage[i]) {
          su[c] = 1500;
          cout << 7 - (c + 1) << " ";
          break;
        }
      }
    }

    cout << endl;
  }
  return 0;
}
Problem p00428 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, t;
  while (cin >> n >> m, n) {
    pair<int, int> v[m];
    for (int i = 0; i < m; i++)
      v[i] = make_pair(n, i + 1);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> t, v[j].first -= t;
    sort(v, v + m);
    for (int i = 0; i < m - 1; i++)
      cout << v[i].second << ' ';
    cout << v[m - 1].second << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
  int n, m, ctn[1000], t;
  while (cin >> n >> m, n) {
    fill(ctn, ctn + m, 0);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> t, ctn[j] += t;
    t = 0;
    int i = n;
    while (t != m - 1) {
      for (int j = 0; j < m; j++) {
        if (ctn[j] == i) {
          cout << j + 1 << " ";
          t++;
          ctn[j] = -1;
        }
      }
      i--;
    }
    for (int i = 0; i < m; i++)
      if (ctn[i] != -1)
        cout << i + 1 << endl;
  }
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int tab[100], table[100], a, b;
int main() {
  memset(table, 0, sizeof(table));
  scanf("%d%d", &a, &b);
  if (a == 0 && b == 0)
    return 0;
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      int n;
      scanf("%d", &n);
      table[j] += n;
    }
  }
  for (int i = 0; i < b; i++)
    tab[i] = table[i];
  sort(tab, tab + b);
  for (int j = b - 1; j >= 0; j--) {
    for (int i = 0; i < b; i++) {
      if (table[i] == tab[j]) {
        if (j != b - 1)
          printf(" ");
        printf("%d", i + 1);
        table[i] = -1;
        break;
      }
    }
  }
  printf("\\n");
  main();
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
int main() {
  int a, b, c;
  while (1) {
    scanf("%d%d", &a, &b);
    if (a == 0)
      return 0;
    int basho[b], res[b];
    memset(basho, 0, sizeof(int) * b);
    for (int i = 0; i < b; i++)
      res[i] = i;
    for (int i = 0; i < a; i++)
      for (int j = 0; j < b; j++) {
        scanf("%d", &c);
        basho[j] += c;
      }
    for (int i = 0; i < b - 1; i++)
      for (int j = i + 1; i < b; j++)
        if (basho[i] < basho[j]) {
          c = basho[i];
          basho[i] = basho[j];
          basho[j] = c;
          c = res[i];
          res[i] = res[j];
          res[j] = c;
        }

    for (int i = 0; i < b; i++)
      printf("%d ", res[i]);
    printf("\\n");
  }
}
Problem p00428 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int main() {
  int N, M, v;
  pair<int, int> vote_area[110];
  while (cin >> N >> M && M > 0) {
    // ペアの初期化
    for (int j = 0; j < M; ++j) {
      vote_area[j].first = 0;
      vote_area[j].second = j + 1; /* code */
    }

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++j) {
        cin >> v; /* i人目の人が場所jに行きたいかどうか */
        vote_area[j].first -= v;
        // cout << vote_area[j].first;
      }
      // cout << endl;
    }

    // あるタイミングで整列する
    sort(vote_area, vote_area + M);
    // reverse(vote_area, vote_area+M);
    for (int i = 0; i < M; ++i) {
      cout << vote_area[i].second;
      if (i < M - 1) {
        cout << " ";
      }
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
int main() {
  int N, M, v;
  pair<int, int> vote_area[110];
  while (cin >> N >> M && N > 0) {
    // ペアの初期化
    for (int j = 0; j < M; ++j) {
      vote_area[j].first = 0;
      vote_area[j].second = j + 1; /* code */
    }

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < M; ++i) {
        cin >> v; /* i人目の人が場所jに行きたいかどうか */
        if (v == 1) {
          vote_area[j].first += 1;
        }
      }
    }

    // あるタイミングで整列する
    reverse(vote_area, vote_area + M);
    for (int i = 0; i < M; ++i) {
      cout << vote_area[i].second;
    }
    cout << endl;
  }
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct Place {
  int id;
  int num;
};

int main() {
  int n, m, i, j, which;
  ifstream in("input.txt");
  ofstream out("output.txt");
  while (true) {
    Place places[100];
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (i = 0; i < m; ++i) {
      places[i].id = i + 1;
      places[i].num = 0;
    }
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j) {
        cin >> which;
        if (which) {
          ++places[j].num;
        }
      }
    }
    for (i = m; i > 0; --i) {
      for (j = 0; j < m - 1; ++j) {
        if (places[j].num < places[j + 1].num) {
          swap(places[j], places[j + 1]);
        }
      }
    }
    for (i = 0; i < m; ++i) {
      if (i) {
        cout << " ";
      }
      cout << places[i].id;
    }
    cout << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

struct Place {
  int id;
  int num;
};

int main() {
  int n, m, i, j, which;
  ifstream in("input.txt");
  ofstream out("output.txt");
  while (true) {
    Place places[100];
    in >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (i = 0; i < m; ++i) {
      places[i].id = i + 1;
      places[i].num = 0;
    }
    for (i = 0; i < n; ++i) {
      for (j = 0; j < m; ++j) {
        in >> which;
        if (which) {
          ++places[j].num;
        }
      }
    }
    for (i = m; i > 0; --i) {
      for (j = 0; j < m - 1; ++j) {
        if (places[j].num < places[j + 1].num) {
          swap(places[j], places[j + 1]);
        }
      }
    }
    for (i = 0; i < m; ++i) {
      if (i) {
        out << " ";
      }
      out << places[i].id;
    }
    out << endl;
  }
  return 0;
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

struct A {
  int n;
  int a;
};

struct A V_A[105];

class sort_ {
public:
  bool operator()(const A &a, const A &b) {
    if (b.n == a.n) {
      return a.a < b.a;
    }
    return b.n < a.n;
  }
};

int main() {
  int n, m;
  while (true) {
    for (int i = 0; i < 100; i++) {
      V_A[i].n = 0;
      V_A[i].a = i;
    }
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int k = 0;
        scanf("%d", &k);
        V_A[j].n += k;
      }
    }
    sort(V_A, V_A + m, sort_());
    for (int i = 0; i < m; i++) {
      if (i != 0)
        printf(" ");
      printf("%d", V_A[i].a + 1);
    }
    printf("\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

struct A {
  int n;
  int a;
};

struct A V_A[100];

class sort_ {
public:
  bool operator()(const A &a, const A &b) {
    if (b.n == a.n) {
      return a.a < b.a;
    }
    return b.n < a.n;
  }
};

int main() {
  int n, m;
  while (true) {
    for (int i = 0; i < 100; i++) {
      V_A[i].n = 0;
      V_A[i].a = i;
    }
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int k = 0;
        scanf("%d", &k);
        V_A[j].n += k;
      }
    }
    sort(V_A, V_A + sizeof(A) * m, sort_());
    for (int i = 0; i < m; i++) {
      if (i != 0)
        printf(" ");
      printf("%d", V_A[i].a + 1);
    }
    printf("\\n");
  }
  return 0;
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>

int main() {
  int ba[101][2], n, m, i, j, c;

  while (0 <= scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;

    // ÚInÔÆlú»
    memset(ba, 0, sizeof(ba));

    // ÚInÔãüÆlvZ
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        scanf("%d", &c);
        if (c == 1)
          ++ba[j][1];
        ba[j][0] = j;
      }
    }

    // lÅ\\[g
    for (i = 1; i <= m; i++) {
      for (j = 1; j < m; j++) {
        if (ba[j][1] < ba[j + 1][1]) {
          c = ba[j][1];
          ba[j][1] = ba[j + 1][1];
          ba[j + 1][1] = c;

          c = ba[j][0];
          ba[j][0] = ba[j + 1][0];
          ba[j + 1][0] = c;
        }
      }
    }

    // oÍ
    for (i = 1; i <= m; i++) {
      printf("%d", ba[i][0]);
      if (i != m)
        printf(" ");
    }
    printf("\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>

int main() {
  int n, m, ba[100][2], i, j, c;

  while (0 <= scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;

    memset(ba, 0, sizeof(ba));

    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        scanf("%d", &c);
        if (c == 1)
          ++ba[j][1];
        ba[j][0] = j;
      }
    }

    for (i = 1; i <= m; i++) {
      for (j = i; j <= m; j++) {
        if (ba[j][1] > ba[i][1]) {
          c = ba[j][1];
          ba[j][1] = ba[i][1];
          ba[i][1] = c;

          c = ba[j][0];
          ba[j][0] = ba[i][0];
          ba[i][0] = c;
        }
      }
    }
    for (i = 1; i <= m; i++) {
      printf("%d", ba[i][0]);
      if (i != m)
        printf(" ");
    }
    printf("\\n");
  }
  return 0;
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, tb[101];
  while (1) {
    cin >> a >> b;
    for (int i = 0; i < b; i++) {
      tb[i] = b - i;
    }
    if (a == b && b == 0)
      break;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        cin >> c;
        if (c == 1)
          tb[j] += 1000;
      }
    }

    sort(tb, tb + b);

    for (int i = 0; i < b; i++) {
      // cout<<" "<<tb[i]<<endl;
    }

    cout << 1 + b - (tb[b - 1] % 1000);
    for (int i = b - 2; i >= 0; i--) {
      cout << " " << 1 + b - (tb[i] % 1000);
    }
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a, b, c, d, tb[101], no[101];
  while (1) {
    for (int i = 0; i < 101; i++) {
      tb[i] = 0;
      no[i] = i + 1;
    }
    cin >> a >> b;
    if (a == b && b == 0)
      break;
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < b; j++) {
        cin >> c;
        if (c == 1)
          tb[j]++;
      }
    }
    for (int i = a; i > 0; i--) {
      for (int j = 0; j < i; j++) {
        if (tb[j] < tb[j + 1]) {
          d = tb[j];
          tb[j] = tb[j + 1];
          tb[j + 1] = d;
          d = no[j];
          no[j] = no[j + 1];
          no[j + 1] = d;
        }
      }
    }
    cout << no[0];
    for (int i = 1; i < b; i++) {
      cout << " " << no[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00428 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int n, m, c;

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == 0 && m == 0) {
      break;
    }

    vector<pair<int, int>> r(m);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", &c);

        r[j].first -= c;
      }
    }

    for (int i = 0; i < m; i++) {
      r[i].second = i + 1;
    }

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

    for (int i = 0; i < m; i++) {
      if (i) {
        printf(" ");
      }

      printf("%d", r[i].second);
    }

    printf("\\n");
  }

  return 0;
}

Error: -11

Fixed submission:

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

using namespace std;

int n, m, c;

int main() {
  while (true) {
    scanf("%d", &n);
    scanf("%d", &m);

    if (n == 0 && m == 0) {
      break;
    }

    vector<pair<int, int>> r(m);

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", c);

        r[j].first -= c;
      }
    }

    for (int i = 0; i < m; i++) {
      r[i].second = i + 1;
    }

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

    for (int i = 0; i < m; i++) {
      if (i) {
        printf(" ");
      }

      printf("%d", r[i].second);
    }
  }

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

#include <iostream>
#include <string>
using namespace std;
string T, F;
void fuego() {
  string ans, g;
  int i, j, k = 1;
  T += '-';
  F = "";
  for (i = 1, j = T.size(); i < j; i++)
    if (T[i] != T[i - 1]) {
      while (k)
        g = k % 10 + '0', ans = g + ans, k /= 10;
      F += ans + T[i - 1];
      k = 1, ans = "";
    } else
      k++;
  T = F;
}
int main() {
  int N;
  while (1) {
    cin >> N;
    if (!N)
      return 0;
    cin >> T;
    while (N--)
      fuego();
    cout << T << endl;
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <string.h>
char T[505], F[505], ans[505];
void fuego() {
  int i, j, k = 1, l = 0, f = 0;
  for (i = 1, j = strlen(T); i < j; i++)
    if (T[i] != T[i - 1]) {
      while (k)
        ans[f++] = k % 10 + 48, k /= 10;
      F[f + l] = T[i - 1];
      for (--f; f >= 0; f--, l++)
        F[l] = ans[f];
      f = 0, l++, k = 1;
    } else
      k++;
  while (k)
    ans[f++] = k % 10 + 48, k /= 10;
  F[f + l] = T[i - 1];
  for (--f; f >= 0; f--, l++)
    F[l] = ans[f];
  for (i = 0; i <= l; i++)
    T[i] = F[i];
  T[i] = 0;
}
int main() {
  int N;
  while (1) {
    scanf("%d", &N);
    if (!N)
      return 0;
    scanf("%s", &T);
    while (N--)
      fuego();
    printf("%s\\n", T);
  }
}
Problem p00429 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  int n, num, cont, v;
  string a, nex;
  stringstream ss;
  while (1) {

    scanf("%d", &n);

    if (n == 0) {
      break;
    }
    cin >> a;

    for (int i = 0; i < n; i++) {
      cont = 0;
      while (1) {
        if (cont >= a.size()) {
          break;
        }

        num = a[cont];

        v = 0;
        while (1) {
          if (a[cont] != num) {
            break;
          }
          v++;
          cont++;
        }
        ss.str("");
        ss << v;

        nex += ss.str();
        nex += num;
      }
      a = nex;
      nex = "";
    }
    cout << a << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  int n, num, cont, v;
  string a, nex;
  stringstream ss;
  while (1) {

    scanf("%d", &n);

    if (n == 0) {
      break;
    }
    cin >> a;

    for (int i = 0; i < n; i++) {
      cont = 0;
      while (1) {
        if (cont >= a.size()) {
          break;
        }

        num = a[cont];

        v = 0;
        while (1) {
          if (a[cont] != num) {
            break;
          }
          v++;
          cont++;
        }

        ss.clear();
        ss << v;

        nex += ss.str();
        nex += num;
      }
      a = nex;
      nex = "";
    }
    cout << a << endl;
  }
}
Problem p00429 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
char b[100000], d[100000];
int main() {
  string a;
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> a;
    for (int i = 0; i < a.size(); i++)
      d[i] = a[i];
    int dtop = a.size();
    for (int u = 0; u < n; u++) {
      int count = 1, top = 0;
      for (int i = 1; i < dtop; i++) {
        if (d[i - 1] == d[i])
          count++;
        else {
          int c = 100000;
          bool first = true;
          for (int j = 0; j < 6; j++) {
            if (count / c != 0 || first == false) {
              b[top] = count / c + '0';
              count %= c;
              first = false;
              top++;
            }
            c /= 10;
          }
          b[top] = d[i - 1];
          top++;
          count = 1;
        }
      }
      int c = 100000;
      bool first = true;
      for (int j = 0; j < 6; j++) {
        if (count / c != 0 || first == false) {
          b[top] = count / c + '0';
          count %= c;
          first = false;
          top++;
        }
        c /= 10;
      }
      b[top] = d[dtop - 1];
      top++;
      for (int i = 0; i < top; i++)
        d[i] = b[i];
      dtop = top;
    }
    for (int i = 0; i < dtop; i++)
      cout << d[i];
    cout << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
char b[10000], d[10000];
int main() {
  string a;
  int n;
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    cin >> a;
    for (int i = 0; i < a.size(); i++)
      d[i] = a[i];
    int dtop = a.size();
    for (int u = 0; u < n; u++) {
      int count = 1, top = 0;
      for (int i = 1; i < dtop; i++) {
        if (d[i - 1] == d[i])
          count++;
        else {
          int c = 10000;
          for (int j = 0; j < 5; j++) {
            if (count / c != 0) {
              b[top] = count / c + '0';
              count %= c;
              top++;
            }
            c /= 10;
          }
          b[top] = d[i - 1];
          top++;
          count = 1;
        }
      }
      int c = 10000;
      for (int j = 0; j < 5; j++) {
        if (count / c != 0) {
          b[top] = count / c + '0';
          count %= c;
          top++;
        }
        c /= 10;
      }
      b[top] = a[a.size() - 1];
      top++;
      for (int i = 0; i < top; i++)
        d[i] = b[i];
      dtop = top;
    }
    for (int i = 0; i < dtop; i++)
      cout << d[i];
    cout << endl;
  }
  return 0;
}
Problem p00429 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void reset_stringstream(stringstream &ss) {
  ss.str("");
  ss.clear(stringstream::goodbit);
}

int main() {
  int n, count = 1, p = 0;
  string str;
  int r, a, x[100000];
  int size;
  stringstream ss;
  string newstr;

  while (cin >> n) {
    if (n == 0)
      break;
    cin >> str;
    for (int i = 0; i < n; i++) {
      newstr = "";
      size = str.size();
      count = 1;
      x[0] = atoi(str.substr(0, 1).c_str());
      for (int k = 1; k < size; k++) {
        x[k] = atoi(str.substr(k, 1).c_str());
        if (x[k] == x[k - 1]) {
          count++;
          if (k == size - 1) {
            r = count;
            a = x[k - 1];
            // cout << r << " " << a;ok
            ss << r << a;
            newstr += ss.str();
            reset_stringstream(ss);
            str = newstr;
            count = 1;
          }
        } else {
          //	  cout << count << " " << x[k-1] << endl;
          r = count;
          a = x[k - 1];
          ss << r << a;
          newstr += ss.str();
          reset_stringstream(ss);
          count = 1;
          if (k == size - 1) {
            r = 1;
            a = x[k];
            ss << r << a;
            newstr += ss.str();
            reset_stringstream(ss);
            str = newstr;
          }
        }
      }
      count = 1;
    }
    cout << str << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void reset_stringstream(stringstream &ss) {
  ss.str("");
  ss.clear(stringstream::goodbit);
}

int main() {
  int n, count = 1, p = 0;
  string str;
  int r, a, x[10000];
  int size;
  stringstream ss;
  string newstr;

  while (cin >> n) {
    if (n == 0)
      break;
    cin >> str;
    for (int i = 0; i < n; i++) {
      newstr = "";
      size = str.size();
      count = 1;
      x[0] = atoi(str.substr(0, 1).c_str());
      for (int k = 1; k < size; k++) {
        x[k] = atoi(str.substr(k, 1).c_str());
        if (x[k] == x[k - 1]) {
          count++;
          if (k == size - 1) {
            r = count;
            a = x[k - 1];
            // cout << r << " " << a;ok
            ss << r << a;
            newstr += ss.str();
            reset_stringstream(ss);
            str = newstr;
            count = 1;
          }
        } else {
          //	  cout << count << " " << x[k-1] << endl;
          r = count;
          a = x[k - 1];
          ss << r << a;
          newstr += ss.str();
          reset_stringstream(ss);
          count = 1;
          if (k == size - 1) {
            r = 1;
            a = x[k];
            ss << r << a;
            newstr += ss.str();
            reset_stringstream(ss);
            str = newstr;
          }
        }
      }
      count = 1;
    }
    cout << str << endl;
  }
  return 0;
}