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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  string s;
  while (cin >> s, s != "#") {
    vector<string> ans;
    int n = s.size();
    REP(i, 1 << n) {
      string tmp = s;
      bool f = true;
      REP(j, n) {
        if ((i >> j) & 1) {
          if (s[j] == 'z') {
            f = false;
            break;
          } else
            tmp[j]++;
        }
      }
      if (f) {
        string cp = tmp;
        bool used[26] = {};
        REP(i, cp.size()) {
          if (!used[cp[i] - 97] && cp[i] != 'a') {
            used[cp[i] - 97] = true;
            cp[i]--;
          }
          if (cp[i] != s[i])
            break;
        }
        if (s == cp)
          ans.push_back(tmp);
      }
    }
    sort(ALL(ans));
    int sz = ans.size();
    printf("%d\\n", sz);
    if (sz > 10) {
      REP(i, 5) printf("%s\\n", ans[i].c_str());
      FOR(i, sz - 5, sz) printf("%s\\n", ans[i].c_str());
    } else {
      REP(i, sz) printf("%s\\n", ans[i].c_str());
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  string s;
  while (cin >> s, s != "#") {
    vector<string> ans;
    int n = s.size();
    REP(i, 1 << n) {
      string tmp = s;
      bool f = true;
      REP(j, n) {
        if ((i >> j) & 1) {
          if (s[j] == 'z') {
            f = false;
            break;
          } else
            tmp[j]++;
        }
      }
      if (f) {
        string cp = tmp;
        bool used[26] = {};
        REP(i, cp.size()) {
          if (!used[cp[i] - 97] && cp[i] != 'a') {
            used[cp[i] - 97] = true;
            cp[i]--;
          }
        }
        if (s == cp)
          ans.push_back(tmp);
      }
    }
    sort(ALL(ans));
    int sz = ans.size();
    printf("%d\\n", sz);
    if (sz > 10) {
      REP(i, 5) printf("%s\\n", ans[i].c_str());
      FOR(i, sz - 5, sz) printf("%s\\n", ans[i].c_str());
    } else {
      REP(i, sz) printf("%s\\n", ans[i].c_str());
    }
  }
  return 0;
}