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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());

  function<set<string>(const string &)> enumerate_neighborhood =
      [&](const string &s) {
        set<string> st;
        // replace
        {
          string t = s;
          rep(i, 0, s.size()) {
            rep(j, 0, 26) {
              t[i] = 'a' + j;
              st.insert(t);
            }
            t[i] = s[i];
          }
        }
        // insert
        {
          string x;
          rep(i, 0, s.size() + 1) {
            string y = s.substr(i);
            rep(j, 0, 26) {
              x.push_back('a' + j);
              auto t = x + y;
              st.insert(t);
              x.pop_back();
            }
            x += s[i];
          }
        }
        // delete
        {
          string x;
          rep(i, 0, s.size()) {
            string t = x + s.substr(i + 1);
            st.insert(t);
            x += s[i];
          }
        }
        // swap adjacent
        {
          string t = s;
          rep(i, 0, s.size() - 1) {
            swap(t[i], t[i + 1]);
            st.insert(t);
            swap(t[i], t[i + 1]);
          }
        }
        return st;
      };

  vector<set<string>> us(n);
  rep(i, 0, n) us[i] = enumerate_neighborhood(name[i]);

  int count = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      if (d == 1) {
        if (us[i].find(name[j]) == us[i].end())
          continue;
      } else {
        vector<string> v;
        set_intersection(us[i].begin(), us[i].end(), us[j].begin(), us[j].end(),
                         back_inserter(v));
        if (v.empty())
          continue;
      }
      cout << name[i] << "," << name[j] << endl;
      ++count;
    }
  }
  cout << count << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve(int n) {
  int d;
  vector<string> name(n);
  cin >> d;
  rep(i, 0, n) cin >> name[i];
  sort(name.begin(), name.end());

  function<void(const int, const string &, unordered_set<string> &)>
      enumerate_neighborhood =
          [&](int d, const string &s, unordered_set<string> &st) {
            if (d == 0) {
              st.insert(s);
              return;
            }
            // replace
            {
              string t = s;
              rep(i, 0, s.size()) {
                rep(j, 0, 26) {
                  t[i] = 'a' + j;
                  enumerate_neighborhood(d - 1, t, st);
                }
                t[i] = s[i];
              }
            }
            // insert
            {
              string x;
              rep(i, 0, s.size() + 1) {
                string y = s.substr(i);
                rep(j, 0, 26) {
                  x.push_back('a' + j);
                  auto t = x + y;
                  enumerate_neighborhood(d - 1, t, st);
                  x.pop_back();
                }
                x += s[i];
              }
            }
            // delete
            {
              string x;
              rep(i, 0, s.size()) {
                string t = x + s.substr(i + 1);
                enumerate_neighborhood(d - 1, t, st);
                x += s[i];
              }
            }
            // swap adjacent
            {
              string t = s;
              rep(i, 0, s.size() - 1) {
                swap(t[i], t[i + 1]);
                enumerate_neighborhood(d - 1, t, st);
                swap(t[i], t[i + 1]);
              }
            }
          };

  int count = 0;
  rep(i, 0, n) {
    unordered_set<string> st;
    enumerate_neighborhood(d, name[i], st);
    rep(j, i + 1, n) {
      if (st.find(name[j]) == st.end())
        continue;
      cout << name[i] << "," << name[j] << endl;
      ++count;
    }
  }
  cout << count << endl;
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  for (;;) {
    int n;
    cin >> n;
    if (!n)
      break;
    solve(n);
  }
  return 0;
}