Problem p00429 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
using namespace std;

int n;
char s[1000001], s2[1000001];

int main() {
  while (cin >> n) {
    if (n == 0)
      return 0;
    for (int i = 0; i < 10001; i++) {
      s[i] = 0;
      s2[i] = 0;
    }
    cin >> s;
    int l = 0;
    while (isdigit(s[l]))
      l++;
    for (int i = 0; i < n; i++) {
      int m, t = 0, cur = 0;
      for (int j = 0; j < l; j++) {
        if (t == 0) {
          m = s[j] - '0';
          t = 1;
        } else {
          if (s[j] - '0' == m)
            t++;
          else {
            int f = 0;
            if (t > 99) {
              s2[cur++] = t / 100 + '0';
              t %= 100;
              f = 1;
            }
            if (t > 9 || f) {
              s2[cur++] = t / 10 + '0';
              t %= 10;
            }
            s2[cur++] = t + '0';
            s2[cur++] = m + '0';
            m = s[j] - '0';
            t = 1;
          }
        }
      }
      int f = 0;
      if (t > 99) {
        s2[cur++] = t / 100 + '0';
        t %= 100;
        f = 1;
      }
      if (t > 9 || f) {
        s2[cur++] = t / 10 + '0';
        t %= 10;
      }
      s2[cur++] = t + '0';
      s2[cur++] = m + '0';
      for (int i = 0; i < cur; i++)
        s[i] = s2[i];
      l = cur;
    }
    for (int i = 0; i < l; i++)
      cout << s[i];
    cout << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
using namespace std;

int n;
char s[101], s2[10001];

void f(int t, int m, int cur) {
  cout << "t" << t << " m" << m << " cur" << cur << " ";
  if (t > 9999) {
    s2[cur++] = t / 10000 + '0';
    t %= 10000;
  }
  if (t > 999) {
    s2[cur++] = t / 1000 + '0';
    t %= 1000;
  }
  if (t > 99) {
    s2[cur++] = t / 100 + '0';
    t %= 100;
  }
  if (t > 9) {
    s2[cur++] = t / 10 + '0';
    t %= 10;
  }
  s2[cur++] = t + '0';
  s2[cur++] = m + '0';
  cout << s2[0] << s2[1] << s2 << " cur" << cur << endl;
}

int main() {
  while (cin >> n) {
    if (n == 0)
      return 0;
    for (int i = 0; i < 10001; i++) {
      s[i] = 0;
      s2[i] = 0;
    }
    cin >> s;
    int l = 0;
    while (isdigit(s[l]))
      l++;
    for (int i = 0; i < n; i++) {
      int m, t = 0, cur = 0;
      for (int j = 0; j < l; j++) {
        if (t == 0) {
          m = s[j] - '0';
          t = 1;
        } else {
          if (s[j] - '0' == m)
            t++;
          else {
            int f = 0;
            if (t > 9999) {
              s2[cur++] = t / 10000 + '0';
              t %= 10000;
              f = 1;
            }
            if (t > 999 || f) {
              s2[cur++] = t / 1000 + '0';
              t %= 1000;
              f = 1;
            }
            if (t > 99 || f) {
              s2[cur++] = t / 100 + '0';
              t %= 100;
              f = 1;
            }
            if (t > 9 || f) {
              s2[cur++] = t / 10 + '0';
              t %= 10;
            }
            s2[cur++] = t + '0';
            s2[cur++] = m + '0';
            m = s[j] - '0';
            t = 1;
          }
        }
      }
      int f = 0;
      if (t > 9999) {
        s2[cur++] = t / 10000 + '0';
        t %= 10000;
        f = 1;
      }
      if (t > 999 || f) {
        s2[cur++] = t / 1000 + '0';
        t %= 1000;
        f = 1;
      }
      if (t > 99 || f) {
        s2[cur++] = t / 100 + '0';
        t %= 100;
        f = 1;
      }
      if (t > 9 || f) {
        s2[cur++] = t / 10 + '0';
        t %= 10;
      }
      s2[cur++] = t + '0';
      s2[cur++] = m + '0';
      for (int i = 0; i < cur; i++)
        s[i] = s2[i];
      l = cur;
    }
    for (int i = 0; i < l; i++)
      cout << s[i];
    cout << endl;
  }
}