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

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;

typedef pair<int, int> P;

int n, d;
string t[202];
map<string, set<int>> idx;

vector<string> getV(string v) {
  vector<string> res;
  string s = v;

  for (int a = 0; a < s.length() - 1; a++) {
    swap(s[a], s[a + 1]);
    res.push_back(s);
    swap(s[a], s[a + 1]);
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    for (int j = 0; j < v.length(); j++) {
      s = v;
      s[j] = ch;
      res.push_back(s);
      s[j] = v[j];
    }

    for (int j = 0; j < v.length() + 1; j++) {
      s = v.substr(0, j) + ch + v.substr(j);
      res.push_back(s);
    }
  }

  for (int j = 0; j < v.length(); j++) {
    s = v.substr(0, j) + v.substr(j + 1);
    res.push_back(s);
  }

  return res;
}

int main() {
  while (cin >> n, n) {
    cin >> d;

    for (int i = 0; i < n; i++) {
      cin >> t[i];
    }
    sort(t, t + n);

    idx.clear();

    for (int i = 0; i < n; i++) {
      vector<string> v = getV(t[i]);

      for (int j = 0; j < v.size(); j++) {
        idx[v[j]].insert(i);
      }
    }

    set<P> ans;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (idx[t[j]].find(i) != idx[t[j]].end()) {
          ans.insert(P(i, j));
        }
      }
    }

    if (d == 2) {
      for (map<string, set<int>>::iterator iter = idx.begin();
           iter != idx.end(); iter++) {
        set<int> stt = iter->second;
        for (set<int>::iterator iter2 = stt.begin(); iter2 != stt.end();
             iter2++) {
          for (set<int>::iterator iter3 = stt.begin(); iter3 != stt.end();
               iter3++) {
            if (*iter2 < *iter3)
              ans.insert(P(*iter2, *iter3));
          }
        }
      }
    }

    for (set<P>::iterator iter = ans.begin(); iter != ans.end(); iter++) {
      P p = *iter;
      cout << t[p.first] << "," << t[p.second] << endl;
    }

    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;

typedef pair<string, string> P;

int N, D;
string t[202];
vector<string> vecs[202];

void change(string s, vector<string> &st) {
  st.clear();

  int n = s.size();

  // delete
  for (int i = 0; i < n; i++) {
    string s2 = s;
    s2.erase(s2.begin() + i);
    st.push_back(s2);
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    // insert
    for (int i = 0; i <= n; i++) {
      string s2 = s;
      s2.insert(s2.begin() + i, ch);
      st.push_back(s2);
    }

    // replace
    for (int i = 0; i < n; i++) {
      string s2 = s;
      s2[i] = ch;
      st.push_back(s2);
    }
  }

  // swap
  for (int i = 0; i < n - 1; i++) {
    string s2 = s;
    swap(s2[i], s2[i + 1]);
    st.push_back(s2);
  }
}

int main() {
  while (cin >> N >> D) {
    for (int i = 0; i < N; i++) {
      cin >> t[i];
      change(t[i], vecs[i]);
      sort(vecs[i].begin(), vecs[i].end());
    }

    vector<P> vt;

    for (int i = 0; i < N; i++) {
      for (int j = i + 1; j < N; j++) {
        bool flg = false;

        if (binary_search(vecs[j].begin(), vecs[j].end(), t[i])) {
          flg = true;
        }

        if (!flg && D == 2) {
          for (int k = 0; k < vecs[i].size(); k++) {
            if (binary_search(vecs[j].begin(), vecs[j].end(), vecs[i][k])) {
              flg = true;
              break;
            }
          }
        }

        if (flg) {
          if (t[i] < t[j])
            vt.push_back(P(t[i], t[j]));
          else
            vt.push_back(P(t[j], t[i]));
        }
      }
    }

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

    for (int i = 0; i < vt.size(); i++) {
      cout << vt[i].first << "," << vt[i].second << endl;
    }
    cout << vt.size() << endl;
  }
}