Problem p00831 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define NUM 99999883

struct Info {
  char buf[20];
};

struct Data {
  char left[20], right[20];
};

int N, DIST;
Info info[200];
vector<Info> V[200];
map<ull, bool> MAP[200];
bool check[200][200];

bool strCmp(char *base, char *comp) {
  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;
}

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;
  }
}

ull makeCode(char buf[20]) {

  int length;
  for (length = 0; buf[length] != '\\0'; length++)
    ;

  ull ret = 0;

  for (int i = 0; i < length; i++) {
    ret = buf[i] + ret * NUM;
  }
  return ret;
}

void makeDist1(int id) {

  char work[20];

  int length, index;
  for (length = 0; info[id].buf[length] != '\\0'; length++)
    ;

  MAP[id][makeCode(info[id].buf)] = true;

  for (int del_loc = 0; del_loc < length; del_loc++) {
    index = 0;
    for (int i = 0; i < length; i++) {
      if (i == del_loc)
        continue;
      work[index++] = info[id].buf[i];
    }
    work[index] = '\\0';
    Info new_info;
    strcpy(new_info.buf, work);
    V[id].push_back(new_info);
  }

  for (int word = 0; word < 26; word++) {
    for (int loc = 0; loc <= length; loc++) {
      index = 0;
      for (int i = 0; i < loc; i++) {
        work[index++] = info[id].buf[i];
      }
      work[index++] = 'a' + word;
      for (int i = loc; i < length; i++) {
        work[index++] = info[id].buf[i];
      }
      work[index] = '\\0';
      Info new_info;
      strcpy(new_info.buf, work);
      V[id].push_back(new_info);
    }
  }

  for (int word = 0; word < 26; word++) {
    for (int loc = 0; loc < length; loc++) {
      index = 0;
      for (int i = 0; i < length; i++) {
        if (i != loc) {
          work[index++] = info[id].buf[i];
        } else {
          work[index++] = 'a' + word;
        }
      }
      work[index] = '\\0';
      Info new_info;
      strcpy(new_info.buf, work);
      V[id].push_back(new_info);
    }
  }

  for (int a = 0; a < length - 1; a++) {
    int b = a + 1;
    index = 0;
    for (int i = 0; i < length; i++) {
      work[index++] = info[id].buf[i];
    }
    swap(work[a], work[b]);
    work[index] = '\\0';
    Info new_info;
    strcpy(new_info.buf, work);
    V[id].push_back(new_info);
  }

  for (int i = 0; i < V[id].size(); i++) {
    MAP[id][makeCode(V[id][i].buf)] = true;
  }
}

void func() {

  scanf("%d", &DIST);

  for (int i = 0; i < N; i++) {
    V[i].clear();
    MAP[i].clear();
  }

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

  vector<Data> ANS;

  for (int i = 0; i < N - 1; i++) {
    for (int k = i + 1; k < N; k++)
      check[i][k] = false;
  }

  if (DIST == 1) {

    for (int i = 0; i < N - 1; i++) {
      for (int p = i + 1; p < N; p++) {
        for (int k = 0; k < V[i].size(); k++) {
          if (strCmp(V[i][k].buf, info[p].buf)) {
            check[i][p] = true;
            break;
          }
        }
      }
    }

  } else {

    for (int i = 0; i < N - 1; i++) {
      for (int p = i + 1; p < N; p++) {
        for (int k = 0; k < V[i].size(); k++) {
          auto at = MAP[p].find(makeCode(V[i][k].buf));
          if (at != MAP[p].end()) {
            check[i][p] = true;
            break;
          }
        }
      }
    }
  }

  for (int i = 0; i < N - 1; i++) {
    for (int p = i + 1; p < N; p++) {
      if (check[i][p]) {
        Data data;
        if (strCmp2(info[i].buf, info[p].buf) == 1) {
          strcpy(data.left, info[i].buf);
          strcpy(data.right, info[p].buf);
        } else {
          strcpy(data.right, info[i].buf);
          strcpy(data.left, info[p].buf);
        }
        ANS.push_back(data);
      }
    }
  }

  for (int i = 1; i < ANS.size(); i++) {
    for (int k = ANS.size() - 1; k >= i; k--) {
      if (strCmp2(ANS[k].left, ANS[k - 1].left) == 1) {
        swap(ANS[k], ANS[k - 1]);
      } else if (strCmp2(ANS[k].left, ANS[k - 1].left) == 3 &&
                 strCmp2(ANS[k].right, ANS[k - 1].right) == 1) {
        swap(ANS[k], ANS[k - 1]);
      }
    }
  }

  for (int i = 0; i < ANS.size(); i++) {
    printf("%s,%s\\n", ANS[i].left, ANS[i].right);
  }
  printf("%lld\\n", ANS.size());
}

int main() {

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

    func();
  }
  return 0;
}


Error: TLE

Fixed submission:

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

struct Info {
  char buf[20];
};

struct Data {
  char left[20], right[20];
};

int N, DIST;
Info info[200];
vector<Info> V[200];
bool check[200][200];

bool strCmp(char *base, char *comp) {
  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;
}

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 makeDist1(int id) {

  char work[20];

  int length, index;
  for (length = 0; info[id].buf[length] != '\\0'; length++)
    ;

  for (int del_loc = 0; del_loc < length; del_loc++) {
    index = 0;
    for (int i = 0; i < length; i++) {
      if (i == del_loc)
        continue;
      work[index++] = info[id].buf[i];
    }
    work[index] = '\\0';
    Info new_info;
    strcpy(new_info.buf, work);
    V[id].push_back(new_info);
  }

  for (int word = 0; word < 26; word++) {
    for (int loc = 0; loc <= length; loc++) {
      index = 0;
      for (int i = 0; i < loc; i++) {
        work[index++] = info[id].buf[i];
      }
      work[index++] = 'a' + word;
      for (int i = loc; i < length; i++) {
        work[index++] = info[id].buf[i];
      }
      work[index] = '\\0';
      Info new_info;
      strcpy(new_info.buf, work);
      V[id].push_back(new_info);
    }
  }

  for (int word = 0; word < 26; word++) {
    for (int loc = 0; loc < length; loc++) {
      index = 0;
      for (int i = 0; i < length; i++) {
        if (i != loc) {
          work[index++] = info[id].buf[i];
        } else {
          work[index++] = 'a' + word;
        }
      }
      work[index] = '\\0';
      Info new_info;
      strcpy(new_info.buf, work);
      V[id].push_back(new_info);
    }
  }

  for (int a = 0; a < length - 1; a++) {
    int b = a + 1;
    index = 0;
    for (int i = 0; i < length; i++) {
      work[index++] = info[id].buf[i];
    }
    swap(work[a], work[b]);
    work[index] = '\\0';
    Info new_info;
    strcpy(new_info.buf, work);
    V[id].push_back(new_info);
  }
}

void func() {

  scanf("%d", &DIST);

  for (int i = 0; i < N; i++)
    V[i].clear();

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

  vector<Data> ANS;
  bool FLG;

  for (int i = 0; i < N - 1; i++) {
    for (int k = i + 1; k < N; k++)
      check[i][k] = false;
  }

  if (DIST == 1) {

    for (int i = 0; i < N - 1; i++) {
      for (int p = i + 1; p < N; p++) {
        for (int k = 0; k < V[i].size(); k++) {
          if (strCmp(V[i][k].buf, info[p].buf)) {
            check[i][p] = true;
            break;
          }
        }
      }
    }

  } else {

    for (int i = 0; i < N - 1; i++) {
      for (int p = i + 1; p < N; p++) {
        FLG = false;
        for (int k = 0; k < V[i].size(); k++) {
          if (strCmp(V[i][k].buf, info[p].buf)) {
            check[i][p] = true;
            break;
          }
          for (int q = 0; q < V[p].size(); q++) {
            if (strCmp(V[i][k].buf, V[p][q].buf)) {
              check[i][p] = true;
              FLG = true;
              break;
            }
          }
          if (FLG)
            break;
        }
      }
    }
  }

  for (int i = 0; i < N - 1; i++) {
    for (int p = i + 1; p < N; p++) {
      if (check[i][p]) {
        Data data;
        if (strCmp2(info[i].buf, info[p].buf) == 1) {
          strcpy(data.left, info[i].buf);
          strcpy(data.right, info[p].buf);
        } else {
          strcpy(data.right, info[i].buf);
          strcpy(data.left, info[p].buf);
        }
        ANS.push_back(data);
      }
    }
  }

  for (int i = 1; i < ANS.size(); i++) {
    for (int k = ANS.size() - 1; k >= i; k--) {
      if (strCmp2(ANS[k].left, ANS[k - 1].left) == 1) {
        swap(ANS[k], ANS[k - 1]);
      } else if (strCmp2(ANS[k].left, ANS[k - 1].left) == 3 &&
                 strCmp2(ANS[k].right, ANS[k - 1].right) == 1) {
        swap(ANS[k], ANS[k - 1]);
      }
    }
  }

  for (int i = 0; i < ANS.size(); i++) {
    printf("%s,%s\\n", ANS[i].left, ANS[i].right);
  }
  printf("%lld\\n", ANS.size());
}

int main() {

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

    func();
  }
  return 0;
}