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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#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;

bool check[1000];
char ans[30000], mid[31];

int main() {

  int N, L;
  scanf("%d %d", &N, &L);

  for (int i = 0; i < N; i++)
    check[i] = false;

  vector<string> V;

  string tmp;

  for (int i = 0; i < N; i++) {
    cin >> tmp;
    V.push_back(tmp);
  }

  sort(V.begin(), V.end());

  int mid_len = 0, index = 0;

  bool FLG;

  for (int i = 0; i < N - 1; i++) {
    if (check[i])
      continue;

    for (int k = i + 1; k < N; k++) {
      if (check[k])
        continue;

      FLG = true;

      for (int p = 0; p < L; p++) {
        if (V[i][p] != V[k][L - 1 - p]) {
          FLG = false;
          break;
        }
      }

      if (FLG) {
        check[i] = true;
        check[k] = true;
        for (int p = 0; p < L; p++) {
          ans[index++] = V[i][p];
        }
        break;
      }
    }
  }

  if (L == 1) {

    for (int i = 0; i < N; i++) {
      if (check[i])
        continue;

      mid[0] = V[i][0];
      mid_len = 1;
      break;
    }

  } else {

    for (int i = 0; i < N; i++) {
      if (check[i])
        continue;

      FLG = true;

      for (int k = 0; k < L / 2; k++) {
        if (V[i][k] != V[i][L - 1 - k]) {
          FLG = false;
          break;
        }
      }

      if (FLG) {
        for (int k = 0; k < L; k++)
          mid[k] = V[i][k];
        mid_len = L;
        break;
      }
    }
  }

  if (index == 0 && mid_len == 0) {
    printf("\\n");
  } else if (index > 0 && mid_len == 0) {
    for (int i = 0; i < index; i++) {
      printf("%c", ans[i]);
    }
    for (int i = index - 1; i >= 0; i--) {
      printf("%c", ans[i]);
    }
    printf("\\n");
  } else if (index == 0 && mid_len > 0) {

    for (int i = 0; i < mid_len; i++) {
      printf("%c", mid[i]);
    }
    printf("\\n");
  } else if (index > 0 && mid_len > 0) {
    for (int i = 0; i < index; i++) {
      printf("%c", ans[i]);
    }
    for (int i = 0; i < mid_len; i++) {
      printf("%c", mid[i]);
    }
    for (int i = index - 1; i >= 0; i--) {
      printf("%c", ans[i]);
    }
    printf("\\n");
  }

  return 0;
}

Error: 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 <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;

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

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

int strCmp2(const char left[31], const char right[31]) {
  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;
  }
}

struct Info {
  bool operator<(const struct Info &arg) const {

    if (strCmp2(buf, arg.buf) == 2) {
      return false;
    } else {
      return true;
    }
  };

  char buf[31];
};

Info *info;
bool check[1000];
char ans[30000], mid[31];

int main() {

  int N, L;
  scanf("%d %d", &N, &L);

  info = (Info *)malloc(sizeof(Info) * N);

  for (int i = 0; i < N; i++)
    check[i] = false;

  for (int i = 0; i < N; i++) {
    scanf("%s", info[i].buf);
  }

  sort(info, info + N);

  int mid_len = 0, index = 0;

  bool FLG;

  for (int i = 0; i < N - 1; i++) {
    if (check[i])
      continue;

    for (int k = i + 1; k < N; k++) {
      if (check[k])
        continue;

      FLG = true;

      for (int p = 0; p < L; p++) {
        if (info[i].buf[p] != info[k].buf[L - 1 - p]) {
          FLG = false;
          break;
        }
      }

      if (FLG) {
        check[i] = true;
        check[k] = true;
        for (int p = 0; p < L; p++) {
          ans[index++] = info[i].buf[p];
        }
        break;
      }
    }
  }

  if (L == 1) {

    for (int i = 0; i < N; i++) {
      if (check[i])
        continue;

      mid[0] = info[i].buf[0];
      mid_len = 1;
      break;
    }

  } else {

    for (int i = 0; i < N; i++) {
      if (check[i])
        continue;

      FLG = true;

      for (int k = 0; k < L / 2; k++) {
        if (info[i].buf[k] != info[i].buf[L - 1 - k]) {
          FLG = false;
          break;
        }
      }

      if (FLG) {
        for (int k = 0; k < L; k++)
          mid[k] = info[i].buf[k];
        mid_len = L;
        break;
      }
    }
  }

  if (index == 0 && mid_len == 0) {
    printf("\\n");
  } else if (index > 0 && mid_len == 0) {
    for (int i = 0; i < index; i++) {
      printf("%c", ans[i]);
    }
    for (int i = index - 1; i >= 0; i--) {
      printf("%c", ans[i]);
    }
    printf("\\n");
  } else if (index == 0 && mid_len > 0) {

    for (int i = 0; i < mid_len; i++) {
      printf("%c", mid[i]);
    }
    printf("\\n");
  } else if (index > 0 && mid_len > 0) {
    for (int i = 0; i < index; i++) {
      printf("%c", ans[i]);
    }
    for (int i = 0; i < mid_len; i++) {
      printf("%c", mid[i]);
    }
    for (int i = index - 1; i >= 0; i--) {
      printf("%c", ans[i]);
    }
    printf("\\n");
  }

  return 0;
}