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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> istream &operator>>(istream &is, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, vector<T> const &v);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  const tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 const tuple<T...> &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &t) {
  _ot<0>(os, t);
  return os;
}
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _it(istream &, tuple<T...> &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _it(istream &is, tuple<T...> &t) {
  is >> get<n>(t);
  _it<n + 1>(is, t);
}
template <class... T> istream &operator>>(istream &is, tuple<T...> &t) {
  _it<0>(is, t);
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) is >> v[i];
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

string ans[1 << 20];
char s[32];
char t[32];

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
#endif
  while (cin >> s && s[0] != '#') {
    int sz = 0;
    int n = strlen(s);
    rep(i, n) t[i] = s[i];
    t[n] = 0;
    int change[32];
    rep(mask, 1 << n) {
      bool ok = true;
      rep(i, n) if (mask >> i & 1) if (s[i] == 'z') ok = false;
      if (!ok)
        continue;
      rep(i, n) { t[i] = s[i] + (mask >> i & 1); }
      rep(i, 26) { change[i] = 30; }
      rep(i, n) { change[t[n - i - 1] - 'a'] = n - i - 1; }
      loop(i, 1, 26) t[change[i]]--;
      if (strcmp(t, s) == 0) {
        rep(i, n) { t[i] = s[i] + (mask >> i & 1); }
        ans[sz++] = t;
      }
    }
    sort(ans, ans + sz);
    printf("%d\\n", sz);
    if (sz > 10) {
      rep(i, 5) printf("%s\\n", ans[i].c_str());
      rep(i, 5) printf("%s\\n", ans[sz - 5 + i].c_str());
    } else {
      rep(i, sz) { printf("%s\\n", ans[i].c_str()); }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

void doit(int &len, string v[]) {
  nth_element(v, v + 5, v + len);
  nth_element(v + 5, v + 10, v + len, greater<string>());
  len = 10;
}

string v[1 << 20];

int main() {
  string s, t, u;
  while (cin >> s && s != "#") {
    int cnt = 0, len = 0;
    int n = s.size();
    for (int i = 0; i < (1 << n); i++) {
      t = s;
      bool ok = true;
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1)) {
          if (s[j] != 'z') {
            t[j]++;
          } else {
            ok = false;
            break;
          }
        }
      }
      if (!ok)
        break;
      u = t;
      static int beg[26];
      memset(beg, -1, sizeof(beg));
      for (int j = 0; j < n; j++) {
        if (beg[t[j] - 'a'] == -1)
          beg[t[j] - 'a'] = j;
      }
      for (int j = 'b'; j <= 'z'; j++) {
        if (beg[j - 'a'] != -1)
          t[beg[j - 'a']]--;
        if (t[beg[j - 'a']] != s[beg[j - 'a']]) {
          ok = false;
          break;
        }
      }
      if (ok && t == s) {
        cnt++;
        v[len++] = u;
        if (len >= 20000) {
          doit(len, v);
        }
      }
    }
    sort(v, v + len);
    cout << cnt << endl;
    if (cnt > 10) {
      for (int i = 0; i < 5; i++)
        printf("%s\\n", v[i].c_str());
      for (int i = 0; i < 5; i++)
        printf("%s\\n", v[len - 1 - i].c_str());
    } else {
      for (int i = 0; i < cnt; i++) {
        printf("%s\\n", v[i].c_str());
      }
    }
  }
}