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

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

typedef unsigned long long ulli;

const ulli B = 1000000007ULL;

ulli getHash(const string &s) {
  int n = s.size();
  ulli res = 0;
  for (int i = 0; i < n; ++i) {
    res = res * B + s[i];
  }
  return res;
}

int main() {
  for (int n, d; cin >> n && n;) {
    cin >> d;
    vector<string> ss(n);
    for (int i = 0; i < n; ++i) {
      cin >> ss[i];
    }

    sort(ss.begin(), ss.end());

    vector<set<ulli>> nextHash(n);
    for (int k = 0; k < n; ++k) {
      string s = ss[k];
      // Deleting
      for (int i = 0; i < s.size(); ++i) {
        char tmp = s[i];
        s.erase(i, 1);
        nextHash[k].insert(getHash(s));
        s.insert(i, string(1, tmp));
      }
      // Inserting
      for (int i = 0; i <= s.size(); ++i) {
        for (char c = 'a'; c <= 'z'; ++c) {
          s.insert(i, string(1, c));
          nextHash[k].insert(getHash(s));
          s.erase(i, 1);
        }
      }
      // Replacing
      for (int i = 0; i < s.size(); ++i) {
        char tmp = s[i];
        for (char c = 'a'; c <= 'z'; ++c) {
          s[i] = c;
          nextHash[k].insert(getHash(s));
        }
        s[i] = tmp;
      }
      // Swapping
      for (int i = 0; i + 1 < s.size(); ++i) {
        int j = i + 1;
        swap(s[i], s[j]);
        nextHash[k].insert(getHash(s));
        swap(s[i], s[j]);
      }
    }

    map<ulli, int> A;
    map<ulli, set<int>> B;
    for (int i = 0; i < n; ++i) {
      A[getHash(ss[i])] = i;
      if (d <= 1)
        continue;
      for (set<ulli>::iterator it = nextHash[i].begin();
           it != nextHash[i].end(); ++it) {
        B[*it].insert(i);
      }
    }

    set<pair<int, int>> ans;
    for (int i = 0; i < n; ++i) {
      for (set<ulli>::iterator it = nextHash[i].begin();
           it != nextHash[i].end(); ++it) {
        if (A.count(*it)) {
          pair<int, int> p(i, A[*it]);
          if (p.first > p.second)
            swap(p.first, p.second);
          ans.insert(p);
        }
      }
      for (set<ulli>::iterator it = nextHash[i].begin();
           it != nextHash[i].end(); ++it) {
        if (B.count(*it)) {
          for (set<int>::iterator it2 = B[*it].begin(); it2 != B[*it].end();
               ++it2) {
            pair<int, int> p(i, *it2);
            if (p.first > p.second)
              swap(p.first, p.second);
            ans.insert(p);
          }
        }
      }
    }

    int res = 0;
    for (set<pair<int, int>>::iterator it = ans.begin(); it != ans.end();
         ++it) {
      if (it->first == it->second)
        continue;
      ++res;
      cout << ss[it->first] << "," << ss[it->second] << endl;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int n, d;
vector<string> ss;
set<pair<string, string>> ans;
set<string> sset;

void rec(string s, int i0, int d) {
  if (sset.count(s) && s != ss[i0]) {
    pair<string, string> p(s, ss[i0]);
    if (p.first > p.second)
      swap(p.first, p.second);
    ans.insert(p);
  }
  if (!d)
    return;
  // Deleting
  for (int i = 0; i < s.size(); ++i) {
    string t = s;
    t.erase(i);
    rec(t, i0, d - 1);
  }
  // Inserting
  for (int i = 0; i <= s.size(); ++i) {
    for (char c = 'a'; c <= 'z'; ++c) {
      string t = s;
      t.insert(i, string(1, c));
      rec(t, i0, d - 1);
    }
  }
  // Replacing
  for (int i = 0; i < s.size(); ++i) {
    char tmp = s[i];
    for (char c = 'a'; c <= 'z'; ++c) {
      s[i] = c;
      rec(s, i0, d - 1);
    }
    s[i] = tmp;
  }
  // Swapping
  for (int i = 0; i < s.size(); ++i) {
    for (int j = i + 1; j < s.size(); ++j) {
      swap(s[i], s[j]);
      rec(s, i0, d - 1);
      swap(s[i], s[j]);
    }
  }
}

int main() {
  while (cin >> n && n) {
    cin >> d;
    ss.resize(n);
    sset.clear();
    ans.clear();
    for (int i = 0; i < n; ++i) {
      cin >> ss[i];
      sset.insert(ss[i]);
    }
    for (int i = 0; i < n; ++i) {
      rec(ss[i], i, d);
    }
    for (set<pair<string, string>>::iterator it = ans.begin(); it != ans.end();
         ++it) {
      cout << it->first << "," << it->second << endl;
    }
    cout << ans.size() << endl;
  }
  return 0;
}