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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
typedef unsigned long long ll;
vector<ll> vc[200];
string ss[200];
bool con[200][200];
typedef pair<string, string> P;
vector<P> ans;
ll b = 1e9 + 7;
ll hash(string s) {
  ll ret = 0;
  rep(i, s.size()) {
    ret *= b;
    ret += s[i];
  }
  return ret;
}
int main() {
  while (true) {
    int n, d;
    cin >> n;
    if (n == 0)
      break;
    cin >> d;
    rep(i, n) {
      vc[i].clear();
      string s, sc;
      cin >> s;
      ss[i] = s;
      int m = s.size();
      rep(j, m) {
        sc = s;
        sc.erase(j, 1);
        vc[i].pb(hash(sc));
      }
      rep(k, m + 1) {
        rep(j, 26) {
          sc = s;
          sc.insert(sc.begin() + k, 'a' + j);
          vc[i].pb(hash(sc));
        }
      }
      rep(k, m) {
        rep(j, 26) {
          sc = s;
          sc[k] = 'a' + j;
          vc[i].pb(hash(sc));
        }
      }
      rep(j, m - 1) {
        sc = s;
        swap(sc[j], sc[j + 1]);
        vc[i].pb(hash(sc));
      }
      sort(all(vc[i]));
      vc[i].erase(unique(all(vc[i])), vc[i].end());
    }
    rep(i, n) rep(j, n) con[i][j] = 0;
    if (d == 1) {
      rep(i, n) {
        ll s = hash(ss[i]);
        rep(j, i) {
          if (binary_search(all(vc[j]), s))
            con[i][j] = true;
        }
      }
    } else {
      rep(i, n) {
        rep(k, vc[i].size()) {
          ll s = vc[i][k];
          rep(j, i) {
            if (binary_search(all(vc[j]), s))
              con[i][j] = true;
          }
        }
      }
    }
    ans.clear();
    rep(i, n) rep(j, n) {
      if (con[i][j]) {
        if (ss[i] < ss[j])
          ans.pb(P(ss[i], ss[j]));
        else
          ans.pb(P(ss[j], ss[i]));
      }
    }
    sort(all(ans));
    rep(i, ans.size()) cout << ans[i].fs << "," << ans[i].sc << endl;
    cout << ans.size() << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
vector<string> vc[200];
string ss[200];
bool con[200][200];
typedef pair<string, string> P;
vector<P> ans;
int main() {
  while (true) {
    int n, d;
    cin >> n;
    if (n == 0)
      break;
    cin >> d;
    rep(i, n) {
      vc[i].clear();
      string s, sc;
      cin >> s;
      ss[i] = s;
      int m = s.size();
      rep(j, m) {
        sc = s;
        sc.erase(j, 1);
        vc[i].pb(sc);
      }
      rep(k, m + 1) {
        rep(j, 26) {
          sc = s;
          sc.insert(sc.begin() + k, 'a' + j);
          vc[i].pb(sc);
        }
      }
      rep(k, m) {
        rep(j, 26) {
          sc = s;
          sc[k] = 'a' + j;
          vc[i].pb(sc);
        }
      }
      rep(j, m - 1) {
        sc = s;
        swap(sc[j], sc[j + 1]);
        vc[i].pb(sc);
      }
      sort(all(vc[i]));
      vc[i].erase(unique(all(vc[i])), vc[i].end());
    }
    rep(i, n) rep(j, n) con[i][j] = 0;
    if (d == 1) {
      rep(i, n) {
        string s = ss[i];
        rep(j, i) {
          if (binary_search(all(vc[j]), s))
            con[i][j] = true;
        }
      }
    } else {
      rep(i, n) {
        rep(k, vc[i].size()) {
          string s = vc[i][k];
          rep(j, i) {
            if (binary_search(all(vc[j]), s))
              con[i][j] = true;
          }
        }
      }
    }
    ans.clear();
    rep(i, n) rep(j, n) {
      if (con[i][j]) {
        if (ss[i] < ss[j])
          ans.pb(P(ss[i], ss[j]));
        else
          ans.pb(P(ss[j], ss[i]));
      }
    }
    sort(all(ans));
    rep(i, ans.size()) cout << ans[i].fs << "," << ans[i].sc << endl;
    cout << ans.size() << endl;
  }
}