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

#include <math.h>
#include <stdio.h>
#include <string.h>
int main(void) {
  int a, s, d[6], g, h, i, j, c[6], q, w, v, aa;
  long long int f;
  int z[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  char x[15];
  while (1) {
    scanf("%s", x);
    if (x[0] == '#')
      break;
    a = 0;
    f = 0;
    s = 0;
    q = 365;
    for (i = 0; i < strlen(x); i++)
      if (x[i] == '.')
        a++;
    for (i = 0; i < 5; i++) {
      d[i] = 0;
      c[i] = 0;
    }
    if (a == 2) {
      f = 10;
      for (i = 0; i < strlen(x); i++) {
        if (x[i] == '.')
          s++;
        else {
          d[s] *= 10;
          d[s] += x[i] - '0';
        }
      }
      if (d[0] == 2012)
        printf("0.0.0.0.%d\\n", d[2] - 21);
      else {
        f = 10;
        g = d[0];
        if (d[1] > 2)
          g++;
        for (i = 2013; i < d[0]; i++)
          f += 365;
        for (i = 2013; i < g; i++)
          if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
            f += 1;
        for (i = 0; i < d[1] - 1; i++)
          f += z[i];
        f += d[2];
        c[0] = f % 20;
        c[1] = f / 20;
        c[2] = c[1] / 18;
        c[1] = c[1] % 18;
        c[3] = c[2] / 20;
        c[2] = c[2] % 20;
        c[4] = c[3] / 20;
        c[3] = c[3] % 20;
        c[4] %= 13;
        for (i = 4; i >= 0; i--) {
          printf("%d", c[i]);
          if (i != 0)
            printf(".");
        }
        printf("\\n");
      }
    } else if (a == 4) {
      for (i = 0; i < strlen(x); i++) {
        if (x[i] == '.')
          s++;
        else {
          d[s] *= 10;
          d[s] += x[i] - '0';
        }
      }
      f = d[4];
      f += d[3] * 20;
      f += d[2] * 20 * 18;
      f += d[1] * 20 * 18 * 20;
      f += d[0] * 20 * 18 * 20 * 20;
      if (f <= 10)
        printf("2012.12.%d\\n", 21 + f);
      else {
        f -= 10;
        c[0] = 2013;
        while (1) {
          if (c[0] % 4 == 0 && c[0] % 100 != 0 || c[0] % 400 == 0)
            q = 366;
          else
            q = 365;
          if (f <= q)
            break;
          f -= q;
          c[0]++;
        }
        for (i = 0; i < 12; i++) {
          aa = z[i];
          if (q == 366 && i == 1)
            aa++;
          if (f <= aa)
            break;
          f -= aa;
          c[1] = i + 1;
        }
        c[1]++;
        c[2] = f;
        for (i = 0; i < 3; i++) {
          printf("%d", c[i]);
          if (i != 2)
            printf(".");
        }
        printf("\\n");
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h> //????§£?±?
#include <string.h>
int main(void) {
  int a, s, d[100], f, g, h, i, j, k, c, x[3], o;
  char z[2];
  while (1) {
    scanf("%d %d", &a, &s);
    if (a == 0 && s == 0)
      break;
    f = a;
    for (i = 0; i < a; i++)
      d[i] = 0;
    for (i = 0; i < s; i++) {
      scanf("%s", z);
      if (z[0] == 'A') {
        for (j = 0; j < a; j++) {
          if (d[j] == 0) {
            d[j] = 1;
            f--;
            break;
          }
        }
      } else if (z[0] == 'B') {
        g = 0;
        for (j = a - 1; j >= 0; j--) {
          if (d[j] == 0 && d[j + 1] != 1 && d[j - 1] != 1) {
            d[j] = 2;
            f--;
            break;
          }
          if (d[j] == 0)
            g++;
          if (f == g) {
            d[j] = 2;
            f--;
            break;
          }
        }
      } else if (z[0] == 'C') {
        if (a == f) {
          if (a % 2 == 1) {
            d[((a + 1) / 2 - 1)] = 3;
            f--;
          } else if (a % 2 == 0) {
            d[a / 2] = 3;
            f--;
          }
        } else if (a > f) {
          for (j = 0; j < a; j++) {
            if (d[j] > 0) {
              if (d[j - 1] == 0 && j - 1 >= 0) {
                d[j - 1] = 3;
                f--;
                break;
              } else if (d[j + 1] == 0 && j + 1 < a) {
                d[j + 1] = 3;
                f--;
                break;
              }
            }
          }
        }
      } else if (z[0] == 'D') {
        h = -1;
        c = -1;
        int v = 0;
        x[0] = 0;
        x[1] = 0;
        for (j = a - 1; j >= 0; j--) {
          if (d[j] > 0 && v == 0) {
            h = j;
            if (d[0] == 0) {
              x[0] = h - 1;
              x[1] = 0;
            }
            v = 1;
          } else if (d[j] > 0 && v == 1) {
            c = j;
          }
          if (c > -1 && h > -1) {
            if ((h - c) % 2 == 1) {
              if (x[0] <= ((h - c) / 2 - 1)) {
                x[0] = (h - c) / 2 - 1;
                x[1] = h - (h - c) / 2 - 1;
              }
            } else if ((h - c) % 2 == 0) {
              if (x[0] <= (h - c) / 2) {
                x[0] = (h - c) / 2;
                x[1] = h - (h - c) / 2;
              }
            }
          }
          if (c > -1)
            h = c;
        }
        if (d[a - 1] == 0) {
          if (x[0] < (a - 1) - c) {
            x[0] = (a - 1) - c;
            x[1] = a - 1;
          }
        }
        if (h == -1)
          x[1] = 0;
        else if (c == -1) {
          x[0] = (a - 1) - h;
          x[1] = a - 1;
          if (x[0] < h)
            x[1] = 0;
        }
        d[x[1]] = 4;
        f--;
      }
    }
    for (i = 0; i < a; i++) {
      if (d[i] == 0)
        printf("#");
      else if (d[i] == 1)
        printf("A");
      else if (d[i] == 2)
        printf("B");
      else if (d[i] == 3)
        printf("C");
      else if (d[i] == 4)
        printf("D");
    }
    printf("\\n");
  }
  return 0;
}