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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int n, m, co[100] = {0}, i, j;
  char a[100] = {0}, b[100] = {0}, c[100] = {0};
  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      co[i] = 1;
    }
    cin >> m;
    for (j = 0; j < m; j++) {
      cin >> c[j];
      for (i = 0; i < n; i++) {
        if (c[j] == a[i]) {
          c[j] = b[i];
          co[i] = 0;
          break;
        }
      }
    }
    for (j = 0; j < m; j++) {
      cout << c[j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main(void) {
  int n, m, co[1000] = {0}, i, j;
  char a[1000] = {0}, b[1000] = {0}, c[100011] = {0};
  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
      co[i] = 1;
    }
    cin >> m;
    for (j = 0; j < m; j++) {
      cin >> c[j];
      for (i = 0; i < n; i++) {
        if (c[j] == a[i]) {
          c[j] = b[i];
          co[i] = 0;
          break;
        }
      }
    }
    for (j = 0; j < m; j++) {
      cout << c[j];
    }
    cout << endl;
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
////////////////*My library*////////////////////////
#define rep_to(a, i, n) for (int i = a; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x) cout << x << endl
#define s_1(x) cin >> x
#define s_2(x, y) cin >> x >> y
////////////////////////////////////////////////////

////////////////////////
/*prime()*/
/*primecount()*/
/*sort()*/
/*imos???*/
/*????????¢?´¢*/
/*??±???????????¢?´¢*/
/*???????¨???????*/
///////////////////////

int main() {
  int N, len;
  char str[10000][10] = {};
  char s[10000] = {};

  while (1) {
    s_1(N);
    if (N == 0)
      break;
    rep(i, N) cin >> str[i][0] >> str[i][1];
    s_1(len);

    rep(i, len) {
      s_1(s[i]);
      rep(j, N) {
        if (str[j][0] == s[i]) {
          s[i] = str[j][1];
          break;
        }
      }
    }
    rep(i, len) cout << s[i];
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
////////////////*My library*////////////////////////
#define rep_to(a, i, n) for (int i = a; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep_r(i, n) for (int i = n - 1; i >= 0; i--)
#define debug(x) cout << x << endl
#define s_1(x) cin >> x
#define s_2(x, y) cin >> x >> y
////////////////////////////////////////////////////

////////////////////////
/*prime()*/
/*primecount()*/
/*sort()*/
/*imos???*/
/*????????¢?´¢*/
/*??±???????????¢?´¢*/
/*???????¨???????*/
///////////////////////

int main() {
  int N, len;
  char str[10000][10] = {};
  char s[100000] = {};

  while (1) {
    s_1(N);
    if (N == 0)
      break;
    rep(i, N) cin >> str[i][0] >> str[i][1];
    s_1(len);

    rep(i, len) {
      s_1(s[i]);
      rep(j, N) {
        if (str[j][0] == s[i]) {
          s[i] = str[j][1];
          break;
        }
      }
    }
    rep(i, len) cout << s[i];
    cout << endl;
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int i, j, n, x, l;
  char a[10000], b[10000], c[10000], m;
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> x;
    for (j = 0; j < x; j++) {
      cin >> c[j];
    }
    for (l = 0; l < x; l++) {
      for (int k = 0; k < n; ++k) {
        if (c[l] == a[k]) {
          c[l] = b[k];
          break;
        }
      }
    }
    for (int k = 0; k < x; ++k) {
      cout << c[k];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int i, j, n, x, l;
  char a[100001], b[100001], c[100001], m;
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (i = 0; i < n; i++) {
      cin >> a[i] >> b[i];
    }
    cin >> x;
    for (j = 0; j < x; j++) {
      cin >> c[j];
    }
    for (l = 0; l < x; l++) {
      for (int k = 0; k < n; ++k) {
        if (c[l] == a[k]) {
          c[l] = b[k];
          break;
        }
      }
    }
    for (int k = 0; k < x; ++k) {
      cout << c[k];
    }
    cout << endl;
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  for (;;) {
    char henkan[1000][2];
    char str[1000];
    int n, m;
    char temp;
    bool flg = 0;

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

    for (int i = 0; i < n; i++) {
      cin >> henkan[i][0] >> henkan[i][1];
    }
    /*
    for(int i = 0; i < n; i++){
            cout << henkan[i][1] << endl;
    }
    */

    cin >> m;

    for (int i = 0; i < m; i++) {
      cin >> str[i];
    }

    for (int i = 0; i < m; i++) {
      flg = 0;
      for (int j = 0; j < n; j++) {
        if (str[i] == henkan[j][0]) {
          temp = henkan[j][1];
          flg = 1;
        }
      }
      if (flg == 1) {
        printf("%c", temp);
      } else {
        printf("%c", str[i]);
      }
    }
    cout << endl;
  }
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  for (;;) {
    char henkan[100000][2];
    char str[100000];
    int n, m;
    char temp;
    bool flg = 0;

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

    for (int i = 0; i < n; i++) {
      cin >> henkan[i][0] >> henkan[i][1];
    }
    /*
    for(int i = 0; i < n; i++){
            cout << henkan[i][1] << endl;
    }
    */

    cin >> m;

    for (int i = 0; i < m; i++) {
      cin >> str[i];
    }

    for (int i = 0; i < m; i++) {
      flg = 0;
      for (int j = 0; j < n; j++) {
        if (str[i] == henkan[j][0]) {
          temp = henkan[j][1];
          flg = 1;
        }
      }
      if (flg == 1) {
        printf("%c", temp);
      } else {
        printf("%c", str[i]);
      }
    }
    cout << endl;
  }
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  int n;
  while (cin >> n) {
    vector<vector<char>> c(n, vector<char>(2));
    const int first = 0;
    const int second = 1;
    while (n--) {
      cin >> c[n][first] >> c[n][second];
    }
    cin >> n;
    char in;
    string ans;
    while (n--) {
      cin >> in;
      for (int i = 0; i < c.size(); i++) {
        if (in == c[i][first]) {
          in = c[i][second];
          break;
        }
      }
      ans += in;
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  int n;
  while (cin >> n, n) {
    vector<vector<char>> c(n, vector<char>(2));
    const int first = 0;
    const int second = 1;
    while (n--) {
      cin >> c[n][first] >> c[n][second];
    }
    cin >> n;
    char in;
    string ans;
    while (n--) {
      cin >> in;
      for (int i = 0; i < c.size(); i++) {
        if (in == c[i][first]) {
          in = c[i][second];
          break;
        }
      }
      ans += in;
    }
    cout << ans << endl;
  }
}
Problem p00424 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
using namespace std;

int main() {
  int n;
  char a, b;
  map<char, char> list;
  map<char, char>::iterator it;
  while (1) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      list.insert(pair<char, char>(a, b));
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a;
      it = list.find(a);
      if (it == list.end()) {
        cout << a;
      } else {
        cout << (*it).second;
      }
    }
    cout << endl;
    list.clear();
  }
  return 0;
}

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

int main() {
  int n;
  char a, b;
  map<char, char> list;
  map<char, char>::iterator it;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      list.insert(pair<char, char>(a, b));
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a;
      it = list.find(a);
      if (it == list.end()) {
        cout << a;
      } else {
        cout << (*it).second;
      }
    }
    cout << endl;
    list.clear();
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
char hantei(char);

int n;
char chana[1000000000], chanb[1000000000];

int main() {
  int i, j, m;
  char w;

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

    for (i = 0; i < n; i++) {
      scanf(" %c %c", &chana[i], &chanb[i]);
    }

    scanf("%d", &m);
    for (j = 0; j < m; j++) {
      scanf(" %c", &w);
      printf("%c", hantei(w));
    }
    printf("\n");
  }
  return 0;
}

char hantei(char w) {
  int i;
  for (i = 0; i < n; i++) {
    if (w == chana[i])
      return chanb[i];
  }
  return w;
}

Fixed submission:
#include <stdio.h>
char hantei(char);

int n;
char chana[1000], chanb[1000];

int main() {
  int i, j, m;
  char w;

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

    for (i = 0; i < n; i++) {
      scanf(" %c %c", &chana[i], &chanb[i]);
    }

    scanf("%d", &m);
    for (j = 0; j < m; j++) {
      scanf(" %c", &w);
      printf("%c", hantei(w));
    }
    printf("\n");
  }
  return 0;
}

char hantei(char w) {
  int i;
  for (i = 0; i < n; i++) {
    if (w == chana[i])
      return chanb[i];
  }
  return w;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int main() {
  int n, i, j, m, f;
  char a[256], b[256], x;
  while (1) {
    scanf("%d ", &n);
    if (n == 0) {
      break;
    }

    for (i = 0; i < n; i++) {
      scanf("%c %c ", &a[i], &b[i]);
    }

    scanf("%d ", &m);
    for (i = 0; i < m; i++) {
      scanf("%s", &x);
      f = 0;
      for (j = 0; j < n; j++) {
        if (x == a[j]) {
          printf("%c", b[j]);
          f = 1;
          break;
        }
      }
      if (f == 0) {
        printf("%s", x);
      }
    }
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
int main() {
  int n, i, j, m, f;
  char a[256], b[256], x;
  while (1) {
    scanf("%d ", &n);
    if (n == 0) {
      break;
    }

    for (i = 0; i < n; i++) {
      scanf("%c %c ", &a[i], &b[i]);
    }

    scanf("%d ", &m);
    for (i = 0; i < m; i++) {
      scanf("%s", &x);
      f = 0;
      for (j = 0; j < n; j++) {
        if (x == a[j]) {
          printf("%c", b[j]);
          f = 1;
          break;
        }
      }
      if (f == 0) {
        printf("%c", x);
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int m, n;
    char cha[10000], con[10000], a[10000];
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> cha[i] >> con[i];
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      for (int j = 0; j < m; j++) {
        if (a[i] == cha[j]) {
          a[i] = con[j];
          break;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cout << a[i];
    }
    cout << "\n";
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {
  while (1) {
    int m, n;
    char cha[100000], con[100000], a[100000];
    cin >> m;
    if (m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> cha[i] >> con[i];
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      for (int j = 0; j < m; j++) {
        if (a[i] == cha[j]) {
          a[i] = con[j];
          break;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cout << a[i];
    }
    cout << "\n";
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
struct T {
  unsigned char a, b;
};
T array[100000000];
int main() {
  int n, m, i, j;
  unsigned char a, b;
  string ca, cb;
  bool flag;
  while (cin >> n && n) {
    flag = true;
    string out;
    memset(array, 0, sizeof(array));
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      array[i].a = a;
      array[i].b = b;
    }
    cin >> m;
    for (i = 0; i < m; i++) {
      cin >> a;
      ca = a;
      for (j = 0; j < n; j++) {
        cb = array[j].a;
        if (ca == cb) {
          flag = false;
          break;
        }
      }
      if (flag) {
        out += a;
      } else {
        out += array[j].b;
      }
      flag = true;
    }
    cout << out << endl;
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
struct T {
  unsigned char a, b;
};
T array[1000000];
int main() {
  int n, m, i, j;
  unsigned char a, b;
  string ca, cb;
  bool flag;
  while (cin >> n && n) {
    flag = true;
    string out;
    memset(array, 0, sizeof(array));
    for (i = 0; i < n; i++) {
      cin >> a >> b;
      array[i].a = a;
      array[i].b = b;
    }
    cin >> m;
    for (i = 0; i < m; i++) {
      cin >> a;
      ca = a;
      for (j = 0; j < n; j++) {
        cb = array[j].a;
        if (ca == cb) {
          flag = false;
          break;
        }
      }
      if (flag) {
        out += a;
      } else {
        out += array[j].b;
      }
      flag = true;
    }
    cout << out << endl;
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int n, m;
  int i, j;
  char c[10000000][2];
  char t[10000000];
  int f;
  while (scanf("%d", &n), n) {
    for (i = 0; i < n; i++) {
      scanf("%s", &c[i][0]);
      scanf("%s", &c[i][1]);
    }

    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf(" %s", &t[i]);
    }

    for (i = 0; i < m; i++) {
      f = -1;
      for (j = 0; j < n; j++) {
        if (t[i] == c[j][0]) {
          f = j;
        }
      }

      if (f != -1) {
        printf("%c", c[f][1]);
      } else if (f == -1) {
        printf("%c", t[i]);
      }
    }
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

int main() {
  int n, m;
  int i, j;
  char c[1000000][2];
  char t[1000000];
  int f;
  while (scanf("%d", &n), n) {
    for (i = 0; i < n; i++) {
      scanf("%s", &c[i][0]);
      scanf("%s", &c[i][1]);
    }

    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf(" %s", &t[i]);
    }

    for (i = 0; i < m; i++) {
      f = -1;
      for (j = 0; j < n; j++) {
        if (t[i] == c[j][0]) {
          f = j;
        }
      }

      if (f != -1) {
        printf("%c", c[f][1]);
      } else if (f == -1) {
        printf("%c", t[i]);
      }
    }
    printf("\n");
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
char v[256];
int n, m;
int main() {
  while (true) {
    cin >> n;
    for (int i = 32; i < 127; i++) {
      v[i] = i;
    }
    for (int i = 0; i < n; i++) {
      char p, q;
      cin >> p >> q;
      v[p] = q;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      char f;
      cin >> f;
      cout << v[f];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
char v[256];
int n, m;
int main() {
  while (true) {
    cin >> n;
    for (int i = 32; i < 127; i++) {
      v[i] = i;
    }
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      char p, q;
      cin >> p >> q;
      v[p] = q;
    }
    cin >> m;
    for (int i = 0; i < m; i++) {
      char f;
      cin >> f;
      cout << v[f];
    }
    cout << endl;
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  map<char, char> mp;
  char a, b, c;
  string x;
  int n;
  cin >> n;
  do {
    mp.clear();
    n = 0;
    x.clear();
    do {
      cin >> a >> b;
      mp[a] = b;
    } while (--n);

    cin >> n;
    do {
      cin >> c;
      if (mp[c] != NULL) {
        x += mp[c];
      } else {
        x += c;
      }
    } while (--n);
    cout << x << endl;
  } while (cin >> n, n != 0);

  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
  map<char, char> mp;
  char a, b, c;
  string x;
  int n;
  cin >> n;
  do {
    mp.clear();
    x.clear();
    do {
      cin >> a >> b;
      mp[a] = b;
    } while (--n);

    cin >> n;
    do {
      cin >> c;
      if (mp[c] != NULL) {
        x += mp[c];
      } else {
        x += c;
      }
    } while (--n);
    cout << x << endl;
  } while (cin >> n, n != 0);

  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
using namespace std;

int main() {
  int n, m;
  string dic[100];
  string words[100];
  map<string, string> mp;
  while (true) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    string result = "";
    for (int i = 0; i < n; i++) {
      cin >> dic[i];
      cin >> mp[dic[i]];
    }
    cin >> m;
    for (int k = 0; k < m; k++) {
      cin >> words[k];
      for (int j = 0; j < n; j++) {
        if (words[k] == dic[j]) {
          words[k] = mp[dic[j]];
          break;
        }
      }
      result = result + words[k];
    }
    cout << result << "\n";
  }
  return 0;
}

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

int main() {
  int n, m;
  string dic[100000];
  string words[100000];
  map<string, string> mp;
  while (true) {
    cin >> n;
    if (n == 0) {
      return 0;
    }
    string result = "";
    for (int i = 0; i < n; i++) {
      cin >> dic[i];
      cin >> mp[dic[i]];
    }
    cin >> m;
    for (int k = 0; k < m; k++) {
      cin >> words[k];
      for (int j = 0; j < n; j++) {
        if (words[k] == dic[j]) {
          words[k] = mp[dic[j]];
          break;
        }
      }
      result = result + words[k];
    }
    cout << result << "\n";
  }
  return 0;
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  while (1) {
    char d;
    char ans[101];
    char f[101], g[101];
    int n;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> d;
      f[i] = d;
      cin >> d;
      g[i] = d;
    }
    int s;
    cin >> s;
    for (int i = 0; i < s; i++) {
      cin >> d;
      ans[i] = d;
      for (int j = 0; j < n; j++) {
        if (ans[i] == f[j]) {
          ans[i] = g[j];
          break;
        }
      }
    }
    for (int i = 0; i < s; i++) {
      cout << ans[i];
    }
    cout << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int main() {
  while (1) {
    char d;
    char ans[101000];
    char f[1010000], g[101000];
    int n;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> d;
      f[i] = d;
      cin >> d;
      g[i] = d;
    }
    int s;
    cin >> s;
    for (int i = 0; i < s; i++) {
      cin >> d;
      ans[i] = d;
      for (int j = 0; j < n; j++) {
        if (ans[i] == f[j]) {
          ans[i] = g[j];
          break;
        }
      }
    }
    for (int i = 0; i < s; i++) {
      cout << ans[i];
    }
    cout << endl;
  }
}
Problem p00424 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>
std::string r1[100], r2[100], s[10000000];
int main() {
  int n;
  int m;
  while (true) {
    std::cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      std::cin >> r1[i] >> r2[i];
    std::cin >> m;
    for (int i = 0; i < m; i++)
      std::cin >> s[i];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (r1[j] == s[i]) {
          s[i] = r2[j];
          goto p;
        }
      }
    p:;
    }
    for (int i = 0; i < m; i++) {
      if (i == m - 1)
        std::cout << s[i] << std::endl;
      else {
        std::cout << s[i];
      }
    }
  }
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
std::string r1[100], r2[100], s[1000000];
int main() {
  int n;
  int m;
  while (true) {
    std::cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      std::cin >> r1[i] >> r2[i];
    std::cin >> m;
    for (int i = 0; i < m; i++)
      std::cin >> s[i];
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        if (r1[j] == s[i]) {
          s[i] = r2[j];
          goto p;
        }
      }
    p:;
    }
    for (int i = 0; i < m; i++) {
      if (i == m - 1)
        std::cout << s[i] << std::endl;
      else {
        std::cout << s[i];
      }
    }
  }
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

int main() {
  int n, m, i, j;
  char nn[99999999], nm[99999999], mm[99999999];
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      scanf(" %c %c", &nn[i], &nm[i]);
    }
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf(" %c", &mm[i]);
      for (j = 0; j < n; j++)
        if (mm[i] == nn[j]) {
          mm[i] = nm[j];
          break;
        }
    }
    puts(mm);
  }
}

Fixed submission:
#include <stdio.h>

int main() {
  int n, m, i, j;
  char nn[1000000], nm[1000000], mm[1000000];
  while (1) {
    scanf("%d", &n);
    if (!n)
      break;
    for (i = 0; i < n; i++) {
      scanf(" %c %c", &nn[i], &nm[i]);
    }
    scanf("%d", &m);
    for (i = 0; i < m; i++) {
      scanf(" %c", &mm[i]);
      for (j = 0; j < n; j++)
        if (mm[i] == nn[j]) {
          mm[i] = nm[j];
          break;
        }
    }
    puts(mm);
  }
}
Problem p00424 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m;
  string op;
  while (cin >> n && n) {
    char c1[40], c2[40];
    op = "";
    for (int i = 0; i < n; ++i)
      cin >> c1[i] >> c2[i];
    cin >> m;
    for (int i = 0; i < m; ++i) {
      char c;
      cin >> c;
      for (int j = 0; j < n; ++j) {
        if (c == c1[j]) {
          c = c2[j];
          break;
        }
      }
      op += c;
    }
    cout << op << endl;
  }

  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, m;
  string op;
  while (cin >> n && n) {
    char c1[100], c2[100];
    op = "";
    for (int i = 0; i < n; ++i)
      cin >> c1[i] >> c2[i];
    cin >> m;
    for (int i = 0; i < m; ++i) {
      char c;
      cin >> c;
      for (int j = 0; j < n; ++j) {
        if (c == c1[j]) {
          c = c2[j];
          break;
        }
      }
      op += c;
    }
    cout << op << endl;
  }

  return 0;
}
Problem p00425 (C++)

Buggy submission (status: Time Limit Exceeded):
#import <cstdio>
int main() {
  for (int n, a, t, c, r; c = r = scanf("%d", &n), n; printf("%d\n", r))
    for (char d[] = "123456"; n--; r += *d - 48)
      scanf("%s", &t), t %= 256,
          t % 6 - 4 ? a = t % 5 + t % 2 * 2 + c & 3, t = *d,
                      *d = d[a += a / 2 ? 2 - a % 2 * 2 : 1], d[a] = d[5],
                      d[5] = d[5 - a], d[5 - a] = t : c += t + 3;
}

Fixed submission:
#import <cstdio>
int main() {
  for (int n, a, c, r, t; c = r = scanf("%d", &n), n; printf("%d\n", r))
    for (char d[] = "123546", s[9]; n--; r += *d - 48)
      scanf("%s", s), *s % 6 - 4 ? a = *s % 5 + *s % 2 * 2 + c & 3, t = *d,
                                   *d = d[++a], d[++a - 1] = d[5],
                                   d[5] = d[a % 4 + 1],
                                   d[a % 4 + 1] = t : c += *s + 3;
}
Problem p00425 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

typedef struct {
  int U, W, E, S, N, D;
} dice_t;

int t;

void shoki(dice_t *dice) { // ダイスの初期位置
  dice->U = 1;
  dice->W = 4;
  dice->E = 3;
  dice->S = 2;
  dice->N = 5;
  dice->D = 6;
}

void N(dice_t *dice) {
  t = dice->U;
  dice->U = dice->S;
  dice->S = dice->D;
  dice->D = dice->N;
  dice->N = t;
}

void E(dice_t *dice) {
  t = dice->U;
  dice->U = dice->W;
  dice->W = dice->D;
  dice->D = dice->E;
  dice->E = t;
}

void W(dice_t *dice) {
  t = dice->U;
  dice->U = dice->E;
  dice->E = dice->D;
  dice->D = dice->W;
  dice->W = t;
}

void S(dice_t *dice) {
  t = dice->U;
  dice->U = dice->N;
  dice->N = dice->D;
  dice->D = dice->S;
  dice->S = t;
}

void R(dice_t *dice) {
  t = dice->S;
  dice->S = dice->E;
  dice->E = dice->N;
  dice->N = dice->W;
  dice->W = t;
}

void L(dice_t *dice) {
  t = dice->S;
  dice->S = dice->W;
  dice->W = dice->N;
  dice->N = dice->E;
  dice->E = t;
}

int main(void) {
  char sousa[10];
  int i, n, s;
  dice_t dice;
  while (0 == 0) {
    scanf("%d", &n);
    if (n == 0)
      break;
    shoki(&dice);
    s = 1;
    for (i = 0; i < n; i++) {
      scanf("%d", sousa);
      switch (sousa[0]) {
      case 'N':
        N(&dice);
        break;
      case 'E':
        E(&dice);
        break;
      case 'W':
        W(&dice);
        break;
      case 'S':
        S(&dice);
        break;
      case 'R':
        R(&dice);
        break;
      case 'L':
        L(&dice);
        break;
      }
      s += dice.U;
    }
    printf("%d\n", s);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>

typedef struct {
  int U, W, E, S, N, D;
} dice_t;

int t;

void shoki(dice_t *dice) { // ダイスの初期位置
  dice->U = 1;
  dice->W = 4;
  dice->E = 3;
  dice->S = 2;
  dice->N = 5;
  dice->D = 6;
}

void N(dice_t *dice) {
  t = dice->U;
  dice->U = dice->S;
  dice->S = dice->D;
  dice->D = dice->N;
  dice->N = t;
}

void E(dice_t *dice) {
  t = dice->U;
  dice->U = dice->W;
  dice->W = dice->D;
  dice->D = dice->E;
  dice->E = t;
}

void W(dice_t *dice) {
  t = dice->U;
  dice->U = dice->E;
  dice->E = dice->D;
  dice->D = dice->W;
  dice->W = t;
}

void S(dice_t *dice) {
  t = dice->U;
  dice->U = dice->N;
  dice->N = dice->D;
  dice->D = dice->S;
  dice->S = t;
}

void R(dice_t *dice) {
  t = dice->S;
  dice->S = dice->E;
  dice->E = dice->N;
  dice->N = dice->W;
  dice->W = t;
}

void L(dice_t *dice) {
  t = dice->S;
  dice->S = dice->W;
  dice->W = dice->N;
  dice->N = dice->E;
  dice->E = t;
}

int main(void) {
  char sousa[10];
  int i, n, s;
  dice_t dice;
  while (0 == 0) {
    scanf("%d", &n);
    if (n == 0)
      break;
    shoki(&dice);
    s = 1;
    for (i = 0; i < n; i++) {
      scanf("%s", sousa);
      switch (sousa[0]) {
      case 'N':
        N(&dice);
        break;
      case 'E':
        E(&dice);
        break;
      case 'W':
        W(&dice);
        break;
      case 'S':
        S(&dice);
        break;
      case 'R':
        R(&dice);
        break;
      case 'L':
        L(&dice);
        break;
      }
      s += dice.U;
    }
    printf("%d\n", s);
  }
  return 0;
}
Problem p00425 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <utility>
using namespace std;

#define DUMP(x) cerr << #x << " = " << (x) << endl;

void rotate(int *a, int *b, int *c, int *d) {
  int temp;

  temp = *d;
  *d = *c;
  *c = *b;
  *b = *a;
  *a = temp;
}

int main(int argc, char const *argv[]) {
  int i, n, total;
  string input;

  int up = 1, bottom = 6, left = 4, right = 3, front = 2, back = 5;

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

    up = 1;
    bottom = 6;
    left = 4;
    right = 3;
    front = 2;
    back = 5;
    total = 1;

    for (i = 0; i < n; i++) {
      cin >> input;

      if (input == "North") {
        rotate(&up, &back, &bottom, &front);
      } else if (input == "East") {
        rotate(&up, &right, &bottom, &left);
      } else if (input == "West") {
        rotate(&up, &left, &bottom, &right);
      } else if (input == "South") {
        rotate(&up, &front, &bottom, &back);
      } else if (input == "Right") {
        rotate(&front, &left, &back, &right);
      } else if (input == "Left") {
        rotate(&front, &right, &back, &left);
      }

      total += up;
      DUMP(up);
      DUMP(total);
    }

    cout << total << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <utility>
using namespace std;

#define DUMP(x) cerr << #x << " = " << (x) << endl;

void rotate(int *a, int *b, int *c, int *d) {
  int temp;

  temp = *d;
  *d = *c;
  *c = *b;
  *b = *a;
  *a = temp;
}

int main(int argc, char const *argv[]) {
  int i, n, total;
  string input;

  int up = 1, bottom = 6, left = 4, right = 3, front = 2, back = 5;

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

    up = 1;
    bottom = 6;
    left = 4;
    right = 3;
    front = 2;
    back = 5;
    total = 1;

    for (i = 0; i < n; i++) {
      cin >> input;

      if (input == "North") {
        rotate(&up, &back, &bottom, &front);
      } else if (input == "East") {
        rotate(&up, &right, &bottom, &left);
      } else if (input == "West") {
        rotate(&up, &left, &bottom, &right);
      } else if (input == "South") {
        rotate(&up, &front, &bottom, &back);
      } else if (input == "Right") {
        rotate(&front, &left, &back, &right);
      } else if (input == "Left") {
        rotate(&front, &right, &back, &left);
      }

      total += up;
    }

    cout << total << endl;
  }
  return 0;
}
Problem p00426 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int mp[5000000];
int Hash(string s) {
  int cnt = 0, t = 1;
  rep(i, s.size()) {
    cnt += (int)(s[i] - '0') * t;
    t *= 3;
  }
  return cnt;
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(mp, 0, sizeof(mp));
    string in = "000000000000000";
    rep(i, 3) {
      int len;
      scanf("%d", &len);
      rep(j, len) {
        int a;
        scanf("%d", &a);
        in[--a] = i + '0';
      }
    }
    queue<string> que;
    mp[Hash(in)] = 1;
    que.push(in);
    while (!que.empty()) {
      string u = que.front();
      que.pop();
      if (count(u.begin(), u.begin() + n, '0') == n ||
          count(u.begin(), u.begin() + n, '2') == n) {
        printf("%d\n", mp[Hash(u)] - 1);
        goto g;
      }
      if (mp[Hash(u)] > m)
        continue;
      rep(k, 2) rep(t, 2) {
        int i, j;
        if (t) {
          i = k, j = k + 1;
        } else {
          i = k + 1;
          j = k;
        }
        int fi = (int)u.rfind(i + '0', n - 1),
            fj = (int)u.rfind(j + '0', n - 1);
        if (fi == -1 || fi < fj)
          continue;
        string v = u;
        v[fi] = j + '0';
        if (!mp[Hash(v)]) {
          mp[Hash(v)] = mp[Hash(u)] + 1;
          que.push(v);
        }
      }
    }
    puts("-1");
  g:;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int mp[15000000];
int Hash(string s) {
  int cnt = 0, t = 1;
  rep(i, s.size()) {
    cnt += (int)(s[i] - '0') * t;
    t *= 3;
  }
  return cnt;
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(mp, 0, sizeof(mp));
    string in = "000000000000000";
    rep(i, 3) {
      int len;
      scanf("%d", &len);
      rep(j, len) {
        int a;
        scanf("%d", &a);
        in[--a] = i + '0';
      }
    }
    queue<string> que;
    mp[Hash(in)] = 1;
    que.push(in);
    while (!que.empty()) {
      string u = que.front();
      que.pop();
      if (count(u.begin(), u.begin() + n, '0') == n ||
          count(u.begin(), u.begin() + n, '2') == n) {
        printf("%d\n", mp[Hash(u)] - 1);
        goto g;
      }
      if (mp[Hash(u)] > m)
        continue;
      rep(k, 2) rep(t, 2) {
        int i, j;
        if (t) {
          i = k, j = k + 1;
        } else {
          i = k + 1;
          j = k;
        }
        int fi = (int)u.rfind(i + '0', n - 1),
            fj = (int)u.rfind(j + '0', n - 1);
        if (fi == -1 || fi < fj)
          continue;
        string v = u;
        v[fi] = j + '0';
        if (!mp[Hash(v)]) {
          mp[Hash(v)] = mp[Hash(u)] + 1;
          que.push(v);
        }
      }
    }
    puts("-1");
  g:;
  }
}
Problem p00427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define Z 100001
using namespace std;
int d[Z + 1];
int main() {
  int n, m, k, r, l;
  while (true) {
    cin >> n >> k >> m >> r;
    if (!(n + m + k + r))
      break;
    rep(i, 0, Z) d[i] = 0;
    k = 1, l = n;
    rep(i, 0, Z) d[i] += k / l, k %= l, k *= 10;
    if (m)
      rep(i, 1, n) {
        k = 1, l = i * n;
        rep(j, 0, Z) d[j] += k / l, k %= l, k *= 10;
      }
    for (int i = Z; i > 0; i--)
      d[i - 1] += d[i] / 10, d[i] %= 10;
    cout << d[0] << ".";
    rep(i, 1, r + 1) cout << d[i];
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define Z 10005
using namespace std;
int d[Z + 1];
int main() {
  int n, m, k, r, l;
  while (true) {
    cin >> n >> k >> m >> r;
    if (!(n + m + k + r))
      break;
    rep(i, 0, Z) d[i] = 0;
    k = 1, l = n;
    rep(i, 0, Z) d[i] += k / l, k %= l, k *= 10;
    if (m)
      rep(i, 1, n) {
        k = 1, l = i * n;
        rep(j, 0, Z) d[j] += k / l, k %= l, k *= 10;
      }
    for (int i = Z; i > 0; i--)
      d[i - 1] += d[i] / 10, d[i] %= 10;
    cout << d[0] << ".";
    rep(i, 1, r + 1) cout << d[i];
    cout << endl;
  }
  return 0;
}
Problem p00427 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1100

int N, K, M, R;
int ans[NUM];

void calc(int bunshi, int bunbo) {

  for (int i = 0; i < NUM; i++) {
    ans[i] += bunshi / bunbo;
    bunshi %= bunbo;
    bunshi *= 10;
    if (bunshi == 0)
      return;
  }
}

void func() {

  for (int i = 0; i < NUM; i++)
    ans[i] = 0;

  if (M == 0) {
    calc(1, N);
  } else { // M == 1
    calc(1, N);
    for (int a = 1; a <= N - 1; a++) {
      calc(1, N * a);
    }
  }

  for (int a = NUM - 1; a >= 1; a--) {
    if (ans[a] >= 10) {
      ans[a - 1] += ans[a] / 10;
      ans[a] %= 10;
    }
  }

  printf("%d.", ans[0]);
  for (int a = 1; a <= R; a++) {
    printf("%d", ans[a]);
  }
  printf("\n");
}

int main() {

  while (true) {
    scanf("%d %d %d %d", &N, &K, &M, &R);
    if (N == 0 && K == 0 && M == 0 && R == 0)
      break;

    func();
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 11000

int N, K, M, R;
int ans[NUM];

void calc(int bunshi, int bunbo) {

  for (int i = 0; i < NUM; i++) {
    ans[i] += bunshi / bunbo;
    bunshi %= bunbo;
    bunshi *= 10;
    if (bunshi == 0)
      return;
  }
}

void func() {

  for (int i = 0; i < NUM; i++)
    ans[i] = 0;

  if (M == 0) {
    calc(1, N);
  } else { // M == 1
    calc(1, N);
    for (int a = 1; a <= N - 1; a++) {
      calc(1, N * a);
    }
  }

  for (int a = NUM - 1; a >= 1; a--) {
    if (ans[a] >= 10) {
      ans[a - 1] += ans[a] / 10;
      ans[a] %= 10;
    }
  }

  printf("%d.", ans[0]);
  for (int a = 1; a <= R; a++) {
    printf("%d", ans[a]);
  }
  printf("\n");
}

int main() {

  while (true) {
    scanf("%d %d %d %d", &N, &K, &M, &R);
    if (N == 0 && K == 0 && M == 0 && R == 0)
      break;

    func();
  }

  return 0;
}
Problem p00427 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int answer[10500];

void add_gyakusu(int x) {
  int t = 1;
  for (int i = 0; i < 10200; i++) {
    answer[i] += t / x;
    t %= x;
    t *= 10;
  }
}

void kuriagari() {
  for (int i = 10199; i >= 0; i--) {
    answer[i - 1] += answer[i] / 10;
    answer[i] %= 10;
  }
}

int main() {
  int n, k, m, r;
  while (true) {
    cin >> n >> k >> m >> r;
    fill(answer, answer + 10500, 0);
    if (n + k + m + r == 0)
      return 0;
    add_gyakusu(n);
    if (m == 1) {
      for (int i = 1; i < n;) {
        add_gyakusu(i * n);
      }
    }
    kuriagari();
    cout << answer[0] << '.';
    for (int i = 1; i <= r; i++) {
      cout << answer[i];
    }
    cout << endl;
  }
}

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

int answer[10500];

void add_gyakusu(int x) {
  int t = 1;
  for (int i = 0; i < 10200; i++) {
    answer[i] += t / x;
    t %= x;
    t *= 10;
  }
}

void kuriagari() {
  for (int i = 10199; i >= 0; i--) {
    answer[i - 1] += answer[i] / 10;
    answer[i] %= 10;
  }
}

int main() {
  int n, k, m, r;
  while (true) {
    cin >> n >> k >> m >> r;
    fill(answer, answer + 10500, 0);
    if (n + k + m + r == 0)
      return 0;
    add_gyakusu(n);
    if (m == 1) {
      for (int i = 1; i < n; i++) {
        add_gyakusu(i * n);
      }
    }
    kuriagari();
    cout << answer[0] << '.';
    for (int i = 1; i <= r; i++) {
      cout << answer[i];
    }
    cout << endl;
  }
}
Problem p00429 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

// sツづ個静ヲツ督ェツつゥツづァツづ個連ツ堕アツ青板づーツ陛板つキ
// sst ツづ按づァツ　return 2
int f(string s) {
  unsigned int count;
  for (count = 1; count < s.size(); count++) {
    if (s.at(count - 1) != s.at(count))
      break;
  }
  return count;
}

int main() {
  int n;
  string str;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> str;
    for (int i = 0; i < n; i++) {
      string ans;
      for (unsigned int j = 0; j < str.size();) {
        char buf[1024];
        int len = f(str.substr(j));
        // sprintf_s(buf, 1024, "%d%c", len, str.at(j));
        ans += buf;
        j += len;
      }
      str = ans;
    }
    cout << str << endl;
  }
}

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

// sツづ個静ヲツ督ェツつゥツづァツづ個連ツ堕アツ青板づーツ陛板つキ
// sst ツづ按づァツ　return 2
int f(string s) {
  unsigned int count;
  for (count = 1; count < s.size(); count++) {
    if (s.at(count - 1) != s.at(count))
      break;
  }
  return count;
}

int main() {
  int n;
  string str;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> str;
    for (int i = 0; i < n; i++) {
      string ans;
      for (unsigned int j = 0; j < str.size();) {
        char buf[1024];
        int len = f(str.substr(j));
        sprintf(buf, "%d%c", len, str.at(j));
        ans += buf;
        j += len;
      }
      str = ans;
    }
    cout << str << endl;
  }
}
Problem p00429 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;

int str[21][250];

int main() {
  int n;
  while (cin >> n, n) {
    int a, r, cntlen;
    char input[101];
    cin >> input;
    for (int i = 0;; i++) {
      if (input[i] == '\0') {
        str[0][i] = -1;
        break;
      }
      str[0][i] = input[i] - '0';
    }
    for (int i = 0; i < n; i++) {
      /*for(int j=0;str[i][j]!=-1;j++){
          printf("%d",str[i][j]);
      }puts("");*/
      a = -1;
      cntlen = 0;
      for (int j = 0;; j++) {
        if (str[i][j] == -1 || a != str[i][j]) {
          if (~a) {
            str[i + 1][cntlen] = r;
            str[i + 1][cntlen + 1] = a;
            cntlen += 2;
          }
          if (str[i][j] == -1) {
            str[i + 1][cntlen] = -1;
            break;
          }
          a = str[i][j];
          r = 1;
        } else {
          r++;
          if (r == 10) {
            str[i + 1][cntlen] = 1;
            cntlen++;
            r = 0;
          }
        }
      }
    }
    for (int i = 0; str[n][i] != -1; i++) {
      printf("%d", str[n][i]);
    }
    puts("");
  }
  return 0;
}

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

int str[21][100000];

int main() {
  int n;
  while (cin >> n, n) {
    int a, r, cntlen;
    char input[101];
    cin >> input;
    for (int i = 0;; i++) {
      if (input[i] == '\0') {
        str[0][i] = -1;
        break;
      }
      str[0][i] = input[i] - '0';
    }
    for (int i = 0; i < n; i++) {
      /*for(int j=0;str[i][j]!=-1;j++){
          printf("%d",str[i][j]);
      }puts("");*/
      a = -1;
      cntlen = 0;
      for (int j = 0;; j++) {
        if (str[i][j] == -1 || a != str[i][j]) {
          if (~a) {
            str[i + 1][cntlen] = r;
            str[i + 1][cntlen + 1] = a;
            cntlen += 2;
          }
          if (str[i][j] == -1) {
            str[i + 1][cntlen] = -1;
            break;
          }
          a = str[i][j];
          r = 1;
        } else {
          r++;
          if (r == 10) {
            str[i + 1][cntlen] = 1;
            cntlen++;
            r = 0;
          }
        }
      }
    }
    for (int i = 0; str[n][i] != -1; i++) {
      printf("%d", str[n][i]);
    }
    puts("");
  }
  return 0;
}
Problem p00429 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
char str[21][10000];

int main(void) {
  while (cin >> n, n != 0) {
    cin >> str[0];

    REP(i, 0, n) {
      int j = 0;
      int k = 0;
      int len = strlen(str[i]);
      while (k < len) {
        int s = k;
        while (k < len && str[i][k] == str[i][k + 1])
          k++;
        int cnt = k - s + 1;
        int l = 0;
        int d[100];
        while (cnt != 0) {
          d[l] = cnt % 10;
          cnt /= 10;
          l++;
        }
        while (l > 0) {
          str[i + 1][j++] = '0' + d[--l];
        }
        str[i + 1][j++] = str[i][s];
        k++;
      }
      str[i + 1][j++] = '\0';
    }

    cout << str[n] << endl;
  }

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#define REP(i, a, n) for (int i = a; i < n; i++)
using namespace std;

int n;
char str[21][100000];

int main(void) {
  while (cin >> n, n != 0) {
    cin >> str[0];

    REP(i, 0, n) {
      int j = 0;
      int k = 0;
      int len = strlen(str[i]);
      while (k < len) {
        int s = k;
        while (k < len && str[i][k] == str[i][k + 1])
          k++;
        int cnt = k - s + 1;
        int l = 0;
        int d[100];
        while (cnt != 0) {
          d[l] = cnt % 10;
          cnt /= 10;
          l++;
        }
        while (l > 0) {
          str[i + 1][j++] = '0' + d[--l];
        }
        str[i + 1][j++] = str[i][s];
        k++;
      }
      str[i + 1][j++] = '\0';
    }

    cout << str[n] << endl;
  }

  return 0;
}
Problem p00429 (C++)

Buggy submission (status: 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[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;
}

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[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;
}
Problem p00429 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int main() {
  int n;
  char str[10000];
  char tmp[10000];

  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    scanf("%s", str);

    while (n > 0) {
      int i, j, len;

      len = strlen(str);
      memset(tmp, '\0', sizeof(tmp));
      char c = '\0';
      char countbuf[10], valuebuf[10];
      int count;

      for (i = 0; i < len; i++) {
        if (c != str[i]) {
          count = 0;
          for (j = i; j < len; j++) {
            if (str[i] == str[j]) {
              count++;
            } else {
              break;
            }
          }
          memset(valuebuf, '\0', sizeof(valuebuf));
          valuebuf[0] = str[i];
          sprintf(countbuf, "%d", count);
          strcat(tmp, countbuf);
          strcat(tmp, valuebuf);
          memset(countbuf, '\0', sizeof(countbuf));
          c = str[i];
        }
      }
      memcpy(str, tmp, sizeof(tmp));
      n--;
    }

    printf("%s\n", tmp);
  }
  return 0;
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int main() {
  int n;
  char str[100000];
  char tmp[100000];

  while (1) {
    scanf("%d", &n);
    if (n == 0) {
      break;
    }
    scanf("%s", str);

    while (n > 0) {
      int i, j, len;

      len = strlen(str);
      memset(tmp, '\0', sizeof(tmp));
      char c = '\0';
      char countbuf[10], valuebuf[10];
      int count;

      for (i = 0; i < len; i++) {
        if (c != str[i]) {
          count = 0;
          for (j = i; j < len; j++) {
            if (str[i] == str[j]) {
              count++;
            } else {
              break;
            }
          }
          memset(valuebuf, '\0', sizeof(valuebuf));
          valuebuf[0] = str[i];
          sprintf(countbuf, "%d", count);
          strcat(tmp, countbuf);
          strcat(tmp, valuebuf);
          memset(countbuf, '\0', sizeof(countbuf));
          c = str[i];
        }
      }
      memcpy(str, tmp, sizeof(tmp));
      n--;
    }

    printf("%s\n", tmp);
  }
  return 0;
}