Problem p00831 (C++) — buggy submission, verdict: Memory 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: MLE

Fixed submission:

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

typedef pair<int, int> P;

int n, d;
string t[202];

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

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

  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);

    int cnt = 0;

    vector<string> v[n];
    set<string> st[n];
    map<string, set<int>> idx;

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

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

    set<P> ans;

    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (st[i].find(t[j]) != st[i].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;
  }
}