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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

ull B = 10000007;
int n, d, len;
char s[200][17];
ull t[200];
int G[200][200];
bool f[200][200];
map<ull, bool> mp[200];

struct data {
  int fi, se;
  bool operator<(const data &d) const {
    return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);
  }
};

vector<data> v;

ull Deleting(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++)
    if (i != x)
      res = res * B + ch[i];
  return res;
}

ull Inserting(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i <= len; i++) {
    if (i == x)
      res = res * B + y;
    if (i < len)
      res = res * B + ch[i];
  }
  return res;
}

ull Replacing(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + y;
    else
      res = res * B + ch[i];
  }
  return res;
}

ull Swapping(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + ch[i + 1];
    else if (i == x + 1)
      res = res * B + ch[x];
    else
      res = res * B + ch[i];
  }
  return res;
}

void init() {
  for (int i = 0; i < 200; i++) {
    mp[i].clear();
  }
  v.clear();
  memset(f, false, sizeof(f));
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    init();
    scanf("%d", &d);

    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      len = strlen(s[i]);

      t[i] = Swapping(-10, s[i]);

      for (int j = 0; j < len; j++) {
        mp[i][Deleting(j, s[i])] = true;
      }

      for (int j = 0; j <= len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          mp[i][Inserting(j, k, s[i])] = true;
        }
      }

      for (int j = 0; j < len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          if (s[i][j] != k) {
            mp[i][Replacing(j, k, s[i])] = true;
          }
        }
      }

      for (int j = 0; j + 1 < len; j++) {
        mp[i][Swapping(j, s[i])] = true;
      }
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j] = strcmp(s[i], s[j]);

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int a = i, b = j;
        if (G[a][b] > 0)
          swap(a, b);
        if (mp[a].count(t[b]) && !f[a][b]) {
          v.push_back((data){a, b});
          f[a][b] = true;
        }
        if (d == 1)
          continue;
        map<ull, bool>::iterator it;
        for (it = mp[b].begin(); it != mp[b].end(); it++) {
          if (mp[a].count(it->first) && !f[a][b]) {
            v.push_back((data){a, b});
            f[a][b] = true;
          }
        }
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      printf("%s,%s\\n", s[v[i].fi], s[v[i].se]);
    }
    printf("%d\\n", (int)v.size());
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

ull B = 10000007;
int n, d, len;
char s[200][15];
ull t[200];
int G[200][200];
bool f[200][200];
map<ull, bool> mp[200];

struct data {
  int fi, se;
  bool operator<(const data &d) const {
    return (G[fi][d.fi] != 0 ? G[fi][d.fi] < 0 : G[se][d.se] < 0);
  }
};

vector<data> v;

ull Deleting(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++)
    if (i != x)
      res = res * B + ch[i];
  return res;
}

ull Inserting(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i <= len; i++) {
    if (i == x)
      res = res * B + y;
    if (i < len)
      res = res * B + ch[i];
  }
  return res;
}

ull Replacing(int x, char y, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + y;
    else
      res = res * B + ch[i];
  }
  return res;
}

ull Swapping(int x, char *ch) {
  ull res = 0;
  for (int i = 0; i < len; i++) {
    if (i == x)
      res = res * B + ch[i + 1];
    else if (i == x + 1)
      res = res * B + ch[x];
    else
      res = res * B + ch[i];
  }
  return res;
}

void init() {
  for (int i = 0; i < 200; i++) {
    mp[i].clear();
  }
  v.clear();
  memset(f, false, sizeof(f));
}

int main() {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    init();
    scanf("%d", &d);

    for (int i = 0; i < n; i++) {
      scanf("%s", s[i]);
      len = strlen(s[i]);

      t[i] = Swapping(-10, s[i]);

      for (int j = 0; j < len; j++) {
        mp[i][Deleting(j, s[i])] = true;
      }

      for (int j = 0; j <= len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          mp[i][Inserting(j, k, s[i])] = true;
        }
      }

      for (int j = 0; j < len; j++) {
        for (char k = 'a'; k <= 'z'; k++) {
          if (s[i][j] != k) {
            mp[i][Replacing(j, k, s[i])] = true;
          }
        }
      }

      for (int j = 0; j + 1 < len; j++) {
        mp[i][Swapping(j, s[i])] = true;
      }
    }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        G[i][j] = strcmp(s[i], s[j]);

    for (int i = 1; i < n; i++) {
      for (int j = 0; j < i; j++) {
        int a = i, b = j;
        if (G[a][b] > 0)
          swap(a, b);
        if (mp[a][t[b]] == true && !f[a][b]) {
          v.push_back((data){a, b});
          f[a][b] = true;
        }
        if (d == 1)
          continue;
        map<ull, bool>::iterator it;
        for (it = mp[b].begin(); it != mp[b].end(); it++) {
          if (mp[a][it->first] == true && !f[a][b]) {
            v.push_back((data){a, b});
            f[a][b] = true;
          }
        }
      }
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); i++) {
      printf("%s,%s\\n", s[v[i].fi], s[v[i].se]);
    }
    printf("%d\\n", (int)v.size());
  }
  return 0;
}