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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n;
  while (cin >> n, n) {
    int d;
    cin >> d;
    set<string> ss;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      ss.emplace(s);
    }
    using P = pair<string, string>;
    set<P> ans;
    auto calc = [](string s) {
      int n = s.size();
      set<string> ss;
      for (int i = 0; i < n; i++) {
        string t = s;
        t.erase(t.begin() + i);
        ss.emplace(t);
      }
      for (int i = 0; i <= n; i++) {
        for (char c = 'a'; c <= 'z'; c++) {
          string t = s;
          t.insert(t.begin() + i, c);
          ss.emplace(t);
        }
      }
      for (int i = 0; i < n; i++) {
        for (char c = 'a'; c <= 'z'; c++) {
          string t = s;
          t[i] = c;
          ss.emplace(t);
        }
      }
      for (int i = 0; i < n - 1; i++) {
        swap(s[i], s[i + 1]);
        ss.emplace(s);
        swap(s[i], s[i + 1]);
      }
      return ss;
    };
    map<string, vector<string>> ms;
    for (string s : ss) {
      if (d == 1) {
        map<string, int> dp;
        queue<string> q;
        dp[s] = 0;
        q.emplace(s);
        while (!q.empty()) {
          string t = q.front();
          q.pop();
          if (dp[t] >= d)
            continue;
          auto xs = calc(t);
          for (string x : xs) {
            if (dp.count(x))
              continue;
            dp[x] = dp[t] + 1;
            q.emplace(x);
          }
        }
        for (auto p : dp) {
          if (p.first == s || p.second > d)
            continue;
          if (ss.count(p.first))
            ans.emplace(minmax({s, p.first}));
        }
      }
      if (d == 2) {
        map<string, int> dp;
        queue<string> q;
        dp[s] = 0;
        q.emplace(s);
        while (!q.empty()) {
          string t = q.front();
          q.pop();
          if (dp[t] >= 1)
            continue;
          auto xs = calc(t);
          for (string x : xs) {
            if (dp.count(x))
              continue;
            dp[x] = dp[t] + 1;
            q.emplace(x);
          }
        }
        for (auto p : dp) {
          if (p.second == 1) {
            for (auto x : ms[p.first])
              ans.emplace(minmax({s, x}));

            ms[p.first].emplace_back(s);
          }
          if (p.first == s || p.second > d)
            continue;
          if (ss.count(p.first))
            ans.emplace(minmax({s, p.first}));
        }
      }
    }
    for (P p : ans)
      cout << p.first << "," << p.second << endl;
    cout << ans.size() << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n;
  while (cin >> n, n) {
    int d;
    cin >> d;
    set<string> ss;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      ss.emplace(s);
    }
    using P = pair<string, string>;
    set<P> ans;
    auto calc = [](string s) {
      int n = s.size();
      set<string> ss;
      for (int i = 0; i < n; i++) {
        string t = s;
        t.erase(t.begin() + i);
        ss.emplace(t);
      }
      for (int i = 0; i < n; i++) {
        for (char c = 'a'; c <= 'z'; c++) {
          string t = s;
          t[i] = c;
          ss.emplace(t);
        }
      }
      for (int i = 0; i < n - 1; i++) {
        swap(s[i], s[i + 1]);
        ss.emplace(s);
        swap(s[i], s[i + 1]);
      }
      return ss;
    };
    map<string, vector<string>> ms;
    for (string s : ss) {
      if (d == 1) {
        map<string, int> dp;
        queue<string> q;
        dp[s] = 0;
        q.emplace(s);
        while (!q.empty()) {
          string t = q.front();
          q.pop();
          if (dp[t] >= d)
            continue;
          auto xs = calc(t);
          for (string x : xs) {
            if (dp.count(x))
              continue;
            dp[x] = dp[t] + 1;
            q.emplace(x);
          }
        }
        for (auto p : dp) {
          if (p.first == s || p.second > d)
            continue;
          if (ss.count(p.first))
            ans.emplace(minmax({s, p.first}));
        }
      }
      if (d == 2) {
        map<string, int> dp;
        queue<string> q;
        dp[s] = 0;
        q.emplace(s);
        while (!q.empty()) {
          string t = q.front();
          q.pop();
          if (dp[t] >= 2)
            continue;
          auto xs = calc(t);
          for (string x : xs) {
            if (dp.count(x))
              continue;
            dp[x] = dp[t] + 1;
            q.emplace(x);
          }
        }
        continue;
        for (auto p : dp) {
          if (p.second == 1) {
            for (auto x : ms[p.first])
              ans.emplace(minmax({s, x}));

            ms[p.first].emplace_back(s);
          }
          if (p.first == s || p.second > d)
            continue;
          if (ss.count(p.first))
            ans.emplace(minmax({s, p.first}));
        }
      }
    }
    for (P p : ans)
      cout << p.first << "," << p.second << endl;
    cout << ans.size() << endl;
  }
  return 0;
}