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

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{";                                                                 \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

template <class T> T MIN(const T &a, const T &b) { return a < b ? a : b; }
template <class T> T MAX(const T &a, const T &b) { return a > b ? a : b; }
template <class T> void MIN_UPDATE(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void MAX_UPDATE(T &a, const T &b) {
  if (a < b)
    a = b;
}

string str;
set<string> ans;
//?????????????????£???????????£????????????

void change(string tmp, char rule) {
  if (rule == 'a') {
    ans.insert(tmp);
    return;
  }

  rep(i, tmp.size()) {
    if (tmp[i] == rule)
      return;
    if (tmp[i] == rule - 1) {
      string ctmp = tmp;
      ctmp[i]++;
      change(ctmp, rule - 1);
    }
  }

  change(tmp, rule - 1);
}

int main() {
  while (1) {

    cin >> str;
    ans.clear();
    if (str == "#") {
      break;
    }
    change(str, 'z');

    cout << ans.size() << endl;
    if (ans.size() > 10) {
      vector<string> vec(ans.begin(), ans.end());
      for (int i = 0; i < 5; i++) {
        cout << vec[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << vec[i] << endl;
      }

    } else if (ans.size() > 0) {
      vector<string> vec(ans.begin(), ans.end());
      rep(i, ans.size()) { cout << vec[i] << endl; }
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{";                                                                 \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

template <class T> T MIN(const T &a, const T &b) { return a < b ? a : b; }
template <class T> T MAX(const T &a, const T &b) { return a > b ? a : b; }
template <class T> void MIN_UPDATE(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void MAX_UPDATE(T &a, const T &b) {
  if (a < b)
    a = b;
}

string str;
set<string> ans;
//?????????????????£???????????£????????????

void change(string tmp, char rule) {
  if (rule == 'a') {
    ans.insert(tmp);
    return;
  }

  rep(i, tmp.size()) {
    if (tmp[i] == rule)
      return;
    if (tmp[i] == rule - 1) {
      string ctmp = tmp;
      ctmp[i]++;
      change(ctmp, rule - 1);
    }
  }

  change(tmp, rule - 1);
}

int main() {
  while (1) {

    cin >> str;
    ans.clear();
    if (str == "#") {
      break;
    }
    change(str, 'z');

    cout << ans.size() << endl;
    if (ans.size() > 5) {
      vector<string> vec(ans.begin(), ans.end());
      for (int i = 0; i < 5; i++) {
        cout << vec[i] << endl;
      }
      for (int i = ans.size() - 5; i <= ans.size(); i++) {
        cout << vec[i] << endl;
      }

    } else {
      vector<string> vec(ans.begin(), ans.end());
      rep(i, ans.size()) { cout << vec[i] << endl; }
    }
  }
}