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

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

typedef long long int ll;

int N, D;
vector<string> names;
//
// ll to_int(string s){
//    const int MOD = 1000000009;
//    ll res = 0;
//    for(auto c: s){
//        res = (res*26 + (c - 'a')) % MOD;
//    }
//    return res;
//}

void solve() {
  sort(names.begin(), names.end());

  map<string, set<int>> mp;
  for (int ni = 0; ni < N; ni++) {
    string name = names[ni];
    int L = name.size();

    // delete
    for (int i = 0; i < L; i++) {
      string t = name;
      t.erase(t.begin() + i);
      mp[t].insert(ni);
    }
    // insert
    for (int i = 0; i <= L; i++) {
      for (char c = 'a'; c <= 'z'; c++) {
        string t = name;
        t.insert(t.begin() + i, c);
        mp[t].insert(ni);
      }
    }
    // replace
    for (int i = 0; i < L; i++) {
      for (char c = 'a'; c <= 'z'; c++) {
        string t = name;
        t[i] = c;
        mp[t].insert(ni);
      }
    }
    // swap
    for (int i = 0; i < L - 1; i++) {
      string t = name;
      swap(t[i], t[i + 1]);
      mp[t].insert(ni);
    }
  }
  if (D == 1) {
    int cnt = 0;
    for (int ni = 0; ni < N; ni++) {
      string name = names[ni];
      if (mp[name].size() > 1) {
        for (auto v : mp[name]) {
          if (v <= ni)
            continue;
          cout << name << "," << names[v] << endl;
          cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  if (D == 2) {
    set<pair<int, int>> s;
    for (auto v : mp)
      if (v.second.size() > 1) {
        vector<int> vec;
        for (auto vv : v.second)
          vec.push_back(vv);
        for (int i = 0; i < vec.size(); i++) {
          for (int j = i + 1; j < vec.size(); j++) {
            s.insert(make_pair(vec[i], vec[j]));
          }
        }
      }
    for (auto v : s) {
      cout << names[v.first] << "," << names[v.second] << endl;
    }
    cout << s.size() << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> N, N) {
    cin >> D;
    names.resize(N);
    for (auto &v : names)
      cin >> v;
    solve();
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int N, D;
vector<string> names;

set<string> edit(string name) {
  set<string> ret;
  int L = name.size();

  // delete
  for (int i = 0; i < L; i++) {
    string t = name;
    t.erase(t.begin() + i);
    ret.insert(t);
  }
  // insert
  for (int i = 0; i < L; i++) {
    for (char c = 'a'; c <= 'z'; c++) {
      string t = name;
      t.insert(t.begin() + i, c);
      ret.insert(t);
    }
  }
  // replace
  for (int i = 0; i < L; i++) {
    for (char c = 'a'; c <= 'z'; c++) {
      string t = name;
      t[i] = c;
      ret.insert(t);
    }
  }
  // swap
  for (int i = 0; i < L - 1; i++) {
    string t = name;
    swap(t[i], t[i + 1]);
    ret.insert(t);
  }

  return ret;
}

void solve() {
  sort(names.begin(), names.end());

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    set<string> s = edit(names[i]);
    for (int j = i + 1; j < N; j++) {
      if (D == 1) {
        if (s.count(names[j])) {
          cout << names[i] << "," << names[j] << endl;
          cnt++;
        }
      }
      if (D == 2) {
        set<string> t = edit(names[j]);
        for (auto v : t)
          if (s.count(v)) {
            cout << names[i] << "," << names[j] << endl;
            cnt++;
            break;
          }
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (cin >> N, N) {
    cin >> D;
    names.resize(N);
    for (auto &v : names)
      cin >> v;
    solve();
  }

  return 0;
}