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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int LevenshteinDist(const string &s, const string &t) {
  int n = s.size();
  int m = t.size();
  vector<vector<int>> minCost(n + 2, vector<int>(m + 2, INT_MAX));
  minCost[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      minCost[i + 1][j] = min(minCost[i + 1][j], minCost[i][j] + 1);
      minCost[i][j + 1] = min(minCost[i][j + 1], minCost[i][j] + 1);
      if (i < n && j < m && s[i] == t[j])
        minCost[i + 1][j + 1] = min(minCost[i + 1][j + 1], minCost[i][j]);
      else
        minCost[i + 1][j + 1] = min(minCost[i + 1][j + 1], minCost[i][j] + 1);
    }
  }
  return minCost[n][m];
}

int main() {
  for (;;) {
    int n, d;
    cin >> n >> d;
    if (n == 0)
      return 0;

    vector<string> s(n);
    for (int i = 0; i < n; ++i)
      cin >> s[i];
    sort(s.begin(), s.end());

    vector<set<string>> vss(n);
    for (int i = 0; i < n; ++i) {
      int m = s[i].size();
      for (int j = 0; j < m - 1; ++j) {
        string t = s[i];
        swap(t[j], t[j + 1]);
        vss[i].insert(t);
      }
    }

    int ret = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        bool confuse = false;
        int ld = LevenshteinDist(s[i], s[j]);
        if (ld > d * 2)
          continue;

        if (ld <= d)
          confuse = true;

        set<string>::iterator it1, it2;
        for (it1 = vss[i].begin(); it1 != vss[i].end(); ++it1) {
          if (LevenshteinDist(*it1, s[j]) <= d - 1) {
            confuse = true;
            break;
          }
        }
        for (it2 = vss[j].begin(); it2 != vss[j].end(); ++it2) {
          if (LevenshteinDist(s[i], *it2) <= d - 1) {
            confuse = true;
            break;
          }
        }

        if (d == 2) {
          it1 = vss[i].begin();
          it2 = vss[j].begin();
          while (it1 != vss[i].end() && it2 != vss[j].end()) {
            if (*it1 == *it2) {
              confuse = true;
              break;
            }
            if (*it1 < *it2)
              ++it1;
            else
              ++it2;
          }
        }

        if (confuse) {
          cout << s[i] << ',' << s[j] << endl;
          ++ret;
        }
      }
    }

    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int LevenshteinDist(const string &s, const string &t) {
  int n = s.size();
  int m = t.size();
  vector<vector<int>> minCost(n + 2, vector<int>(m + 2, INT_MAX));
  minCost[0][0] = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j <= m; ++j) {
      minCost[i + 1][j] = min(minCost[i + 1][j], minCost[i][j] + 1);
      minCost[i][j + 1] = min(minCost[i][j + 1], minCost[i][j] + 1);
      if (i < n && j < m && s[i] == t[j])
        minCost[i + 1][j + 1] = min(minCost[i + 1][j + 1], minCost[i][j]);
      else
        minCost[i + 1][j + 1] = min(minCost[i + 1][j + 1], minCost[i][j] + 1);
    }
  }
  return minCost[n][m];
}

int main() {
  for (;;) {
    int n, d;
    cin >> n >> d;
    if (n == 0)
      return 0;

    vector<string> s(n);
    for (int i = 0; i < n; ++i)
      cin >> s[i];
    sort(s.begin(), s.end());

    vector<set<string>> vss(n);
    for (int i = 0; i < n; ++i) {
      int m = s[i].size();
      /* swap */
      for (int j = 0; j < m - 1; ++j) {
        string t = s[i];
        swap(t[j], t[j + 1]);
        vss[i].insert(t);
      }
    }

    int ret = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        bool confuse = false;
        if (LevenshteinDist(s[i], s[j]) <= d)
          confuse = true;

        set<string>::iterator it1, it2;
        for (it1 = vss[i].begin(); it1 != vss[i].end(); ++it1) {
          if (LevenshteinDist(*it1, s[j]) <= d - 1) {
            confuse = true;
            break;
          }
        }
        if (!confuse) {
          for (it2 = vss[j].begin(); it2 != vss[j].end(); ++it2) {
            if (LevenshteinDist(s[i], *it2) <= d - 1) {
              confuse = true;
              break;
            }
          }
        }

        if (d == 2 && !confuse) {
          it1 = vss[i].begin();
          it2 = vss[j].begin();
          while (it1 != vss[i].end() && it2 != vss[j].end()) {
            if (*it1 == *it2) {
              confuse = true;
              break;
            }
            if (*it1 < *it2)
              ++it1;
            else
              ++it2;
          }
        }

        if (confuse) {
          cout << s[i] << ',' << s[j] << endl;
          ++ret;
        }
      }
    }
    cout << ret << endl;
  }
}