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

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

int canbe(char s[]) {
  int cnt = 0, cur, cur2, len;
  char *sabc;
  int i;
  int ap, bp, cp;
  len = strlen(s);
  // printf("called:%s",s);
  // ABC?????????????´¢????????¨????????????????????§??????????????????
  ap = bp = cp = 1;
  for (i = 0; i < len; i++) {
    if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C')
      cnt++;
    if (s[i] == 'A' && (s[i + 1] != 'B' || s[i + 2] != 'C'))
      ap = 0;
    if (s[i] == 'B' && (s[i - 1] != 'A' || s[i + 1] != 'C'))
      bp = 0;
    if (s[i] == 'C' && (s[i - 2] != 'A' || s[i - 1] != 'B'))
      cp = 0;
  }
  // printf("ap=%d bp=%d cp=%d\\n",ap,bp,cp);
  sabc = (char *)calloc(len - 2 * cnt + 5, sizeof(char));
  //??°???????????§?????????
  if (len == 4 && cnt == 1) {
    // printf("%s");
    return 1;
  } else {
    if (cnt) {
      // ABC???A?????????
      if (ap) {
        cur = 0;
        for (i = 0; i < len; i++) {
          if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
            sabc[cur] = 'A';
            i += 2;
          } else {
            sabc[cur] = s[i];
          }
          cur++;
        }
        sabc[cur] = '\\0';
        if (canbe(sabc)) {
          // printf("A\\n");
          return 1;
        }
      }
      // ABC???B?????????
      if (bp) {
        cur = 0;
        for (i = 0; i < len; i++) {
          if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
            sabc[cur] = 'B';
            i += 2;
          } else {
            sabc[cur] = s[i];
          }
          cur++;
        }
        sabc[cur] = '\\0';
        if (canbe(sabc)) {
          // printf("B\\n");
          return 1;
        }
      }
      // ABC???C?????????
      if (cp) {
        cur = 0;
        for (i = 0; i < len; i++) {
          if (s[i] == 'A' && s[i + 1] == 'B' && s[i + 2] == 'C') {
            sabc[cur] = 'C';
            i += 2;
          } else {
            sabc[cur] = s[i];
          }
          cur++;
        }
        sabc[cur] = '\\0';
        if (canbe(sabc)) {
          // printf("C\\n");
          return 1;
        } else {
          return 0;
        }
      }
      return 0;
    } else {
      return 0;
    }
  }
}

int main() {
  char s[5005];
  fgets(s, 5005, stdin);
  printf(canbe(s) ? "Yes\\n" : "No\\n");
}

Error: -11

Fixed submission:

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

int len;
int lv = 0;

int canbe(char s[]) {
  int cnt, cur, cur2, leneff;
  int tr[1670][3];
  int i;
  cnt = leneff = 0;
  // ABC?????????????´¢
  for (i = 0; i < len; i++) {
    if (s[i] != '-') {
      leneff++;
      if (s[i] == 'A') {
        cur = i + 1;
        while (s[cur] == '-')
          cur++;
        if (s[cur] == 'B') {
          cur2 = cur + 1;
          while (s[cur2] == '-')
            cur2++;
          if (s[cur2] == 'C') {
            tr[cnt][0] = i;
            tr[cnt][1] = cur;
            tr[cnt][2] = cur2;
            cnt++;
          }
        }
      }
    }
  }
  //??°???????????§?????????
  if (leneff == 4 && cnt == 1) {
    return 1;
  } else {
    if (cnt) {
      // ABC???A?????????
      for (i = 0; i < cnt; i++) {
        s[tr[i][1]] = '-';
        s[tr[i][2]] = '-';
      }
      if (canbe(s)) {
        //?????¶?????°
        for (i = 0; i < cnt; i++) {
          s[tr[i][1]] = 'B';
          s[tr[i][2]] = 'C';
        }
        return 1;
      } else {
        // ABC???B?????????
        for (i = 0; i < cnt; i++) {
          s[tr[i][0]] = '-';
          s[tr[i][1]] = 'B';
        }
        if (canbe(s)) {
          //?????¶?????°
          for (i = 0; i < cnt; i++) {
            s[tr[i][0]] = 'A';
            s[tr[i][2]] = 'C';
          }
          return 1;
        } else {
          // ABC???C?????????
          for (i = 0; i < cnt; i++) {
            s[tr[i][1]] = '-';
            s[tr[i][2]] = 'C';
          }
          if (canbe(s)) {
            //?????¶?????°
            for (i = 0; i < cnt; i++) {
              s[tr[i][0]] = 'A';
              s[tr[i][1]] = 'B';
            }
            return 1;
          } else {
            //?????¶?????°
            for (i = 0; i < cnt; i++) {
              s[tr[i][0]] = 'A';
              s[tr[i][1]] = 'B';
            }
            return 0;
          }
        }
      }
    } else {
      return 0;
    }
  }
}

int main() {
  int i = 0;
  char c;
  char s[5005];
  while ((c = getchar()) != NULL) {
    s[i] = c;
    i++;
  }
  len = i;
  printf(canbe(s) ? "Yes\\n" : "No\\n");
}