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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#define __float128 long double
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

string s;
const int N = 1 << 21;
string v[N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> s, s != "#") {
    string org = s;
    int idx = 0;

    int n = s.size();
    rep(i, 1 << n) {
      bool ok = true;
      rep(j, n) {
        if (i >> j & 1)
          s[j] = org[j] + 1;
        else
          s[j] = org[j];

        if (s[j] > 'z') {
          ok = false;
          break;
        }
      }
      if (!ok)
        continue;
      int used[26] = {};
      used[0] = 1;
      rep(j, n) {
        if (!used[s[j] - 'a']) {
          used[s[j] - 'a'] = 1;
          if (s[j] - 1 != org[j]) {
            ok = false;
            break;
          }
        } else {
          if (s[j] != org[j]) {
            ok = false;
            break;
          }
        }
      }
      if (!ok)
        continue;
      v[idx++] = s;
    }

    sort(v, v + idx);
    cout << idx << '\\n';

    if (idx <= 10) {
      rep(i, idx) { cout << v[i] << '\\n'; }
    } else {
      rep(i, 5) { cout << v[i] << '\\n'; }
      FOR(i, idx - 5, idx) { cout << v[i] << '\\n'; }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#define __float128 long double
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

string s;
const int N = 1 << 21;
string v[N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (cin >> s, s != "#") {
    string org = s;
    int idx = 0;

    int n = s.size();
    rep(i, 1 << n) {
      bool ok = true;
      rep(j, n) {
        if (i >> j & 1)
          s[j] = org[j] + 1;
        else
          s[j] = org[j];

        if (s[j] > 'z') {
          ok = false;
          break;
        }
      }
      if (!ok)
        continue;
      string t = s;
      int used[26] = {};
      used[0] = 1;
      for (auto &c : t) {
        if (used[c - 'a'])
          continue;
        used[c - 'a'] = 1;
        --c;
      }
      if (org == t)
        v[idx++] = s;
    }

    sort(v, v + idx);
    cout << idx << '\\n';

    if (idx <= 10) {
      rep(i, idx) { cout << v[i] << '\\n'; }
    } else {
      rep(i, 5) { cout << v[i] << '\\n'; }
      FOR(i, idx - 5, idx) { cout << v[i] << '\\n'; }
    }
  }

  return 0;
}