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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

unordered_set<string> cur, nx;

int main(void) {
  string in;
  while (cin >> in) {
    cur.clear(), nx.clear();
    if (in == "#")
      break;
    int n = in.size();

    cur.insert(in);

    for (int i = 24; i >= 0; --i) {
      for (auto &s : cur) {
        bool end = false;
        char b = i + 1 + 'a';
        char a = i + 'a';
        rep(i, n) {
          if (s[i] == b) {
            end = true;
            // zy?????¨????????¨???zz  yz??????????£??????????
            // before????????????????????????????????????after????????£??????????????¨???????????????
            break;
          }
          if (s[i] == a) {
            string can = s; // bbbb-> cbbb bcbb bbcb bbbc
            can[i] = b;
            nx.insert(can);
          }
        }
        if (!end) {
          nx.insert(s);
        }
      }
      cur.swap(nx);
      nx.clear();
    }

    vector<string> answers(cur.begin(), cur.end());
    sort(answers.begin(), answers.end());
    cout << answers.size() << endl;
    if (answers.size() <= 10) {
      rep(i, answers.size()) cout << answers[i] << endl;
    } else {
      rep(i, 5) cout << answers[i] << endl;
      rep(i, 5) cout << answers[answers.size() - 5 + i] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

inline string decrypt(string s) {
  int n = s.size();
  rep(i, 25) {
    char b = i + 1 + 'a';
    char a = i + 'a';
    rep(j, n) if (s[j] == b) s[j] = a, j = n;
  }
  return s;
}

unordered_set<string> s;

int main(void) {
  string in;
  while (cin >> in) {
    s.clear();
    if (in == "#")
      break;
    int n = in.size();
    rep(mask, 1 << n) {
      string cand = in;
      rep(i, n) if (mask & (1 << i)) cand[i]++;
      if (decrypt(cand) == in)
        s.insert(cand);
    }
    vector<string> answers(s.begin(), s.end());
    cout << answers.size() << endl;
    if (answers.size() <= 10) {
      rep(i, answers.size()) cout << answers[i] << endl;
    } else {
      rep(i, 5) cout << answers[i] << endl;
      rep(i, 5) cout << answers[answers.size() - 5 + i] << endl;
    }
  }
  return 0;
}