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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<string> v;
signed main(void) {
  cin.sync_with_stdio(false);
  while (true) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    const int n = s.size();
    v.clear();
    set<string> st;
    string t;
    REP(i, 1 << n) {
      t = s;
      REP(j, n) {
        if ((1 << j) & i && s[j] != 'z') {
          t[j] = t[j] + 1;
        }
        // cout << t << " i:" << j << " s[j]:" << s[j] << endl;
      }
      // cout << "t:" << t << endl;
      string tmp = t;
      bool f[26] = {};
      REP(j, n) {
        if (t[j] != 'a' && !f[t[j] - 'a']) {
          f[t[j] - 'a'] = true;
          t[j] = t[j] - 1;
        }
      }
      if (t == s) {
        st.insert(tmp);
      }
    }

    for (string i : st)
      v.PB(i);
    cout << v.size() << "\\n";
    if (v.size() <= 10) {
      REP(i, v.size()) { cout << v[i] << "\\n"; }
    } else {
      REP(i, 5) cout << v[i] << "\\n";
      FOR(i, v.size() - 5, v.size()) cout << v[i] << "\\n";
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL << 30)
#define LLINF (1LL << 60)
#define PI 3.14159265359
#define EPS 1e-12
// #define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

vector<string> v;
signed main(void) {
  cin.sync_with_stdio(false);
  while (true) {
    string s;
    cin >> s;
    if (s == "#")
      break;
    const int n = s.size();
    v.clear();
    string t;
    REP(i, 1 << n) {
      t = s;
      REP(j, n) {
        if ((1 << j) & i && s[j] != 'z') {
          t[j] = t[j] + 1;
        }
        // cout << t << " i:" << j << " s[j]:" << s[j] << endl;
      }
      // cout << "t:" << t << endl;
      string tmp = t;
      bool f[26] = {};
      REP(j, n) {
        if (t[j] != 'a' && !f[t[j] - 'a']) {
          f[t[j] - 'a'] = true;
          t[j] = t[j] - 1;
        }
      }
      if (t == s) {
        v.PB(tmp);
      }
    }

    sort(ALL(v));
    cout << v.size() << "\\n";
    if (v.size() <= 10) {
      REP(i, v.size()) { cout << v[i] << "\\n"; }
    } else {
      REP(i, 5) cout << v[i] << "\\n";
      FOR(i, v.size() - 5, v.size()) cout << v[i] << "\\n";
    }
  }
  return 0;
}