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

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

int N, K;
char A[11][10];
int used[11];
char num[100];
int size, arr[6000];
int ans = 0;

void rec(int k) {
  int length = strlen(num);
  if (k == K) {
    arr[size++] = atoi(num);
    return;
  }
  for (int i = 0; i < N; ++i) {
    if (!used[i]) {
      used[i] = 1;
      strcat(num, A[i]);
      rec(k + 1);
      used[i] = 0;
      num[length] = '\\0';
    }
  }
}

void bubbleSort() {
  for (int i = 0; i < size; ++i) {
    for (int j = 1; j < size - i; ++j) {
      if (arr[j - 1] > arr[j]) {
        std::swap(arr[j - 1], arr[j]);
      }
    }
  }
}

int main(void) {
  while (true) {
    scanf("%d%d", &N, &K);
    if (N == 0 && K == 0) {
      break;
    }
    for (int i = 0; i < N; ++i) {
      scanf("%s", &A[i]);
    }

    for (int i = 0; i < N; ++i) {
      used[i] = 0;
    }
    strcpy(num, "");
    size = 0;
    rec(0);
    bubbleSort();
    ans = 0;
    for (int i = 0; i < size; ++i) {
      if (i == 0 || arr[i - 1] != arr[i]) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: WA

Fixed submission:

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

int N, K;
char A[11][3];
char tmp[10], cards[211][10], ok[211][10];
bool ans_flag = true, ok_flag = true;
int ans = 0;

int main(void) {
  while (true) {
    ans = 0;
    scanf("%d%d", &N, &K);
    if (N == 0 && K == 0) {
      break;
    }
    for (int i = 0; i < N; ++i) {
      scanf("%s", &A[i]);
    }

    if (K == 2) {
      for (int i = 0; i < N; ++i) {
        strcat(tmp, A[i]);
        for (int j = 0; j < N; ++j) {
          if (i != j) {
            strcat(tmp, A[j]);
            strcpy(cards[ans], tmp);
            ans++;
            int str_long = strlen(tmp) - 1;
            for (int z = 0; z < strlen(A[j]); ++z) {
              tmp[str_long - z] = '\\0';
            }
          }
        }
        memset(tmp, '\\0', sizeof(tmp));
      }
    } else if (K == 3) {
      for (int i = 0; i < N; ++i) {
        strcat(tmp, A[i]);
        for (int j = 0; j < N; ++j) {
          if (i != j) {
            strcat(tmp, A[j]);
            for (int k = 0; k < N; ++k) {
              if (j != k) {
                strcat(tmp, A[k]);
                strcpy(cards[ans], tmp);
                ans++;
                memset(tmp, '\\0', sizeof(tmp));
              }
            }
          }
        }
      }
    } else {
      for (int i = 0; i < N; ++i) {
        strcat(tmp, A[i]);
        for (int j = 0; j < N; ++j) {
          if (i != j) {
            strcat(tmp, A[j]);
            for (int k = 0; k < N; ++k) {
              if (j != k) {
                strcat(tmp, A[k]);
                for (int l = 0; l < N; ++l) {
                  if (k != l) {
                    strcat(tmp, A[j]);
                    strcpy(cards[ans], tmp);
                    ans++;
                    memset(tmp, '\\0', sizeof(tmp));
                  }
                }
              }
            }
          }
        }
      }
    }

    int f = ans;
    ans = 0;
    for (int i = 0; i < f; ++i) {
      for (int j = 0; j < ans; ++j) {
        if (strcmp(cards[i], ok[j]) == 0) {
          ok_flag = false;
          break;
        }
      }
      if (ok_flag == true) {
        strcpy(ok[ans], cards[i]);
        ans++;
      }
      ok_flag = true;
    }
    printf("%d\\n", ans);
  }
  return 0;
}