Problem p00831 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

void transforms(const string &s1, set<string> &s) {
  string tmp;
  for (int i = 0; i < s1.length(); i++) {
    tmp = s1;
    tmp.erase(tmp.begin() + i);
    s.insert(tmp);
  }
  for (int i = 0; i <= s1.length(); i++) {
    for (int j = 0; j < 26; j++) {
      tmp = s1;
      char c = 'a' + j;
      tmp.insert(i, string(1, c));
      s.insert(tmp);
    }
  }
  for (int i = 0; i < s1.length(); i++) {
    for (int j = 0; j < 26; j++) {
      tmp = s1;
      char c = 'a' + j;
      tmp.replace(i, 1, string(1, c));
      s.insert(tmp);
    }
  }
  for (int i = 0; i < s1.length() - 1; i++) {
    tmp = s1;
    swap(tmp[i], tmp[i + 1]);
    s.insert(tmp);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, d;
  vector<pii> res;
  vector<string> name;
  set<string> s[201];
  while (cin >> n, n) {
    cin >> d;
    res.clear();
    name.clear();
    name.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> name[i];
    }
    sort(name.begin(), name.end());

    for (int i = 0; i < n; i++) {
      s[i].clear();
      transforms(name[i], s[i]);
    }

    if (d == 1) {
      for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
          if (s[i].find(name[j]) != s[i].end()) {
            res.push_back({i, j});
          }
        }
      }
    } else {
      for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
          for (auto tmp_s : s[i]) {
            if (s[j].find(tmp_s) != s[j].end()) {
              res.push_back({i, j});
              break;
            }
          }
        }
      }
    }

    for (auto ans : res) {
      cout << name[ans.first] << "," << name[ans.second] << endl;
    }
    cout << res.size() << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define INF 1 << 30
#define LINF 1LL << 60

void transforms(const string &s1, set<string> &s) {
  string tmp;
  for (int i = 0; i < s1.length(); i++) {
    tmp = s1;
    tmp.erase(tmp.begin() + i);
    s.insert(tmp);
  }
  for (int i = 0; i <= s1.length(); i++) {
    for (int j = 0; j < 26; j++) {
      tmp = s1;
      char c = 'a' + j;
      tmp.insert(i, string(1, c));
      s.insert(tmp);
    }
  }
  for (int i = 0; i < s1.length(); i++) {
    for (int j = 0; j < 26; j++) {
      tmp = s1;
      char c = 'a' + j;
      tmp.replace(i, 1, string(1, c));
      s.insert(tmp);
    }
  }
  for (int i = 0; i < s1.length() - 1; i++) {
    tmp = s1;
    swap(tmp[i], tmp[i + 1]);
    s.insert(tmp);
  }
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, d;
  vector<pii> res;
  vector<string> name;
  while (cin >> n, n) {
    cin >> d;
    res.clear();
    name.clear();
    name.resize(n);
    for (int i = 0; i < n; i++) {
      cin >> name[i];
    }
    sort(name.begin(), name.end());
    if (d == 1) {
      set<string> s;
      for (int i = 0; i < n - 1; i++) {
        s.clear();
        transforms(name[i], s);
        for (int j = i + 1; j < n; j++) {
          if (s.find(name[j]) != s.end()) {
            res.push_back({i, j});
          }
        }
      }
    } else {
      vector<set<string>> s;
      for (int i = 0; i < n; i++) {
        transforms(name[i], s[i]);
      }
      //          set<string> s1;
      //           set<string> s2;
      for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
          //                   s1.clear(); s2.clear();
          //                   transforms(name[i],s1);
          //                   transforms(name[j],s2);
          for (auto tmp_s : s[i]) {
            if (s[j].find(tmp_s) != s[j].end()) {
              res.push_back({i, j});
              break;
            }
          }
        }
      }
    }

    for (auto ans : res) {
      cout << name[ans.first] << "," << name[ans.second] << endl;
    }
    cout << res.size() << endl;
  }
  return 0;
}