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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

struct Info {
  Info() { num = 0; }
  char word[21];
  int num;
};

// base??¨comp??????????????????????????¢??°
bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

//???????????????????????????3,left?????????????????´??????1,right?????????????????¨??????2
int strCmp2(char *left, char *right) {
  int i;

  if (strCmp(left, right))
    return 3;

  for (i = 0; left[i] != '\\0' && right[i] != '\\0'; i++) {
    if (left[i] != right[i]) {
      if (left[i] < right[i])
        return 1;
      else {
        return 2;
      }
    }
  }
  if (left[i] == '\\0')
    return 1;
  else {
    return 2;
  }
}

int main() {

  Info **info = (Info **)malloc(sizeof(Info *) * 26);
  for (int i = 0; i < 26; i++) {
    info[i] = (Info *)malloc(sizeof(Info) * 10000);
  }

  char buf[2050], work[21], key[2];
  bool Found;

  int n, index, num_of_each[26], tmp, limit;

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

    for (int i = 0; i < 26; i++) {
      for (int k = 0; k < 10000; k++)
        info[i][k].num = 0;
      num_of_each[i] = 0;
    }

    getchar();

    for (int i = 0; i < n; i++) {
      fgets(buf, 2050, stdin);

      index = 0;
      for (int k = 0; buf[k] != '\\n' && buf[k] != '\\0'; k++) {
        if (buf[k] == ' ') {
          //??¢???????????°????¢?????????????????????????num???1??§????????????
          tmp = work[0] - 'a'; //????????????????????????

          if (num_of_each[tmp] == 0) {
            info[tmp][0].num = 1;
            strcpy(info[tmp][0].word, work);
            num_of_each[tmp]++;
          } else {
            Found = false;
            for (int p = 0; p < num_of_each[tmp]; p++) {
              //??¢????????´???
              if (strCmp(work, info[tmp][p].word)) {
                info[tmp][p].num++;
                Found = true;
                break;
              }
            }
            if (!Found) {
              info[tmp][num_of_each[tmp]].num = 1;
              strcpy(info[tmp][num_of_each[tmp]].word, work);
              num_of_each[tmp]++;
            }
          }
          index = 0;
        } else {
          work[index++] = buf[k];
          work[index] = '\\0';

          if (buf[k + 1] == '\\0' || buf[k + 1] == '\\n') {
            //??¢???????????°????¢?????????????????????????num???1??§????????????
            tmp = work[0] - 'a';
            if (num_of_each[tmp] == 0) {
              info[tmp][0].num = 1;
              strcpy(info[tmp][0].word, work);
              num_of_each[tmp]++;
            } else {
              Found = false;
              for (int p = 0; p < num_of_each[tmp]; p++) {
                //??¢????????´???
                if (strCmp(work, info[tmp][p].word)) {
                  info[tmp][p].num++;
                  Found = true;
                  break;
                }
              }
              if (!Found) {
                info[tmp][num_of_each[tmp]].num = 1;
                strcpy(info[tmp][num_of_each[tmp]].word, work);
                num_of_each[tmp]++;
              }
            }
          }
        }
      }
    }
    scanf("%s", key);

    tmp = key[0] - 'a';

    limit = num_of_each[tmp];

    for (int i = 1; i <= limit - 1; i++) {
      for (int k = limit - 1; k >= i; k--) {
        if (info[tmp][k].num > info[tmp][k - 1].num)
          swap(info[tmp][k], info[tmp][k - 1]);
        else if (info[tmp][k].num == info[tmp][k - 1].num &&
                 strCmp2(info[tmp][k].word, info[tmp][k - 1].word) == 1)
          swap(info[tmp][k], info[tmp][k - 1]);
      }
    }

    for (int i = 0; i < min(limit, 5); i++) {
      if (i == 0)
        printf("%s", info[tmp][i].word);
      else {
        printf(" %s", info[tmp][i].word);
      }
    }
    if (limit != 0)
      printf("\\n");
    if (limit == 0)
      printf("NA\\n");
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;

struct Info {
  Info() { num = 0; }
  char word[25];
  int num;
};

bool strCmp(char *base, char *comp) {
  int length1 = 0, length2 = 0;
  for (int i = 0; base[i] != '\\0'; i++)
    length1++;
  for (int i = 0; comp[i] != '\\0'; i++)
    length2++;
  if (length1 != length2)
    return false;

  for (int i = 0; base[i] != '\\0'; i++) {
    if (base[i] != comp[i])
      return false;
  }
  return true;
}

void strcpy(char *to, char *str) {
  for (int i = 0; str[i] != '\\0'; i++) {
    to[i] = str[i];
    to[i + 1] = '\\0';
  }
}

int strCmp2(char *left, char *right) {
  int i;

  if (strCmp(left, right))
    return 3;

  for (i = 0; left[i] != '\\0' && right[i] != '\\0'; i++) {
    if (left[i] != right[i]) {
      if (left[i] < right[i])
        return 1;
      else {
        return 2;
      }
    }
  }
  if (left[i] == '\\0')
    return 1;
  else {
    return 2;
  }
}

void func(int n) {

  vector<Info> V[25];

  char buf[2050], work[25], key[2];
  bool Found;

  int index, num_of_each[25], tmp, limit;

  for (int i = 0; i < 25; i++) {
    num_of_each[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    fgets(buf, 2050, stdin);

    index = 0;
    for (int k = 0; buf[k] != '\\n' && buf[k] != '\\0'; k++) {
      if (buf[k] == ' ') {
        tmp = work[0] - 'a';

        if (num_of_each[tmp] == 0) {
          V[tmp].push_back(Info());
          V[tmp][0].num = 1;
          strcpy(V[tmp][0].word, work);
          num_of_each[tmp]++;
        } else {
          Found = false;
          for (int p = 0; p < num_of_each[tmp]; p++) {

            if (strCmp(work, V[tmp][p].word)) {
              V[tmp][p].num++;
              Found = true;
              break;
            }
          }
          if (!Found) {
            V[tmp].push_back(Info());
            V[tmp][num_of_each[tmp]].num = 1;
            strcpy(V[tmp][num_of_each[tmp]].word, work);
            num_of_each[tmp]++;
          }
        }
        index = 0;
      } else {
        work[index++] = buf[k];
        work[index] = '\\0';

        if (buf[k + 1] == '\\0' || buf[k + 1] == '\\n') {

          tmp = work[0] - 'a';

          if (num_of_each[tmp] == 0) {
            V[tmp].push_back(Info());
            V[tmp][0].num = 1;
            strcpy(V[tmp][0].word, work);
            num_of_each[tmp]++;
          } else {
            Found = false;
            for (int p = 0; p < num_of_each[tmp]; p++) {

              if (strCmp(work, V[tmp][p].word)) {
                V[tmp][p].num++;
                Found = true;
                break;
              }
            }
            if (!Found) {
              V[tmp].push_back(Info());
              V[tmp][num_of_each[tmp]].num = 1;
              strcpy(V[tmp][num_of_each[tmp]].word, work);
              num_of_each[tmp]++;
            }
          }
          index = 0;
        }
      }
    }
  }
  scanf("%s", key);

  tmp = key[0] - 'a';

  limit = num_of_each[tmp];

  for (int i = 1; i <= limit - 1; i++) {
    for (int k = limit - 1; k >= i; k--) {
      if (V[tmp][k].num > V[tmp][k - 1].num)
        swap(V[tmp][k], V[tmp][k - 1]);
      else if (V[tmp][k].num == V[tmp][k - 1].num &&
               strCmp2(V[tmp][k].word, V[tmp][k - 1].word) == 1)
        swap(V[tmp][k], V[tmp][k - 1]);
    }
  }

  for (int i = 0; i < min(limit, 5); i++) {
    if (i == 0)
      printf("%s", V[tmp][i].word);
    else {
      printf(" %s", V[tmp][i].word);
    }
  }
  if (limit != 0)
    printf("\\n");
  if (limit == 0)
    printf("NA\\n");
}

int main() {

  int n;

  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    getchar();
    func(n);
  }

  return 0;
}