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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  for (auto x : v) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl
#define printVS(vs)                                                            \\
  for (auto x : vs) {                                                          \\
    cout << x << endl;                                                         \\
  }
#define printVV(vv)                                                            \\
  for (auto v : vv) {                                                          \\
    for (auto &&x : v) {                                                       \\
      cout << x << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp)                                                            \\
  for (auto p : vp)                                                            \\
    printP(p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

const int MAX = 1 << 20;
bool ok[MAX];
string s;
int n;

void dfs(int i, int state, int used) {
  // cout << i << " " << state << " " << used << endl;
  if (i == n) {
    ok[state] = true;
    return;
  }

  if (s[i] == 'a' || ((used >> (s[i] - 'a')) & 1)) {
    dfs(i + 1, state, used | (1 << (s[i] - 'a')));
  }
  if (s[i] != 'z' && !((used >> (s[i] - 'a' + 1)) & 1)) {
    dfs(i + 1, state | (1 << i), used | (1 << (s[i] - 'a' + 1)));
  }
}

string f(int state) {
  string t;
  rep(i, n) { t += s[i] + ((state >> i) & 1); }
  return t;
}

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

  while (cin >> s, s != "#") {
    n = s.size();

    rep(i, 1 << n) { ok[i] = false; }

    dfs(0, 0, 0);

    int cnt = 0;
    vector<int> v;
    rep(i, 1 << n) {
      if (ok[i]) {
        cnt++;
        v.emplace_back(i);
      }
    }
    cout << cnt << endl;

    vector<string> vs;
    rep(i, v.size()) { vs.emplace_back(f(v[i])); }

    sort(all(vs));

    if (cnt <= 10) {
      rep(i, vs.size()) { cout << vs[i] << endl; }
    } else {
      rep(i, 5) { cout << vs[i] << endl; }
      rep2(i, (int)vs.size() - 5, vs.size()) { cout << vs[i] << endl; }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define printV(v)                                                              \\
  for (auto x : v) {                                                           \\
    cout << x << " ";                                                          \\
  }                                                                            \\
  cout << endl
#define printVS(vs)                                                            \\
  for (auto x : vs) {                                                          \\
    cout << x << endl;                                                         \\
  }
#define printVV(vv)                                                            \\
  for (auto v : vv) {                                                          \\
    for (auto &&x : v) {                                                       \\
      cout << x << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp)                                                            \\
  for (auto p : vp)                                                            \\
    printP(p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

string f(string s, int state) {
  int n = s.size();
  string t;
  rep(i, n) { t += s[i] + ((state >> i) & 1); }
  return t;
}

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

  string s;
  while (cin >> s, s != "#") {
    int n = s.size();
    vi a(n);
    rep(i, n) a[i] = s[i] - 'a';

    int cnt = 0;
    // vector<int> v;
    // queue<int> q;
    set<string> st;

    rep(state, 1 << n) {
      vi b = a;
      bool ok = true;
      rep(i, n) {
        if ((state >> i) & 1) {
          if (b[i] == 25) {
            ok = false;
            break;
          }
          b[i]++;
        }
      }

      if (!ok) {
        continue;
      }

      rep2(c, 1, 26) {
        rep(i, n) {
          if (b[i] == c) {
            b[i]--;
            break;
          }
        }
      }

      if (a == b) {
        cnt++;
        // if (v.size() < 5) {
        //     v.emplace_back(state);
        // } else {
        //     if (q.size() == 5) {
        //         q.pop();
        //     }
        //     q.push(state);
        // }

        st.insert(f(s, state));
      }
    }

    cout << cnt << endl;
    // for (auto state : v) {
    //     cout << f(s, state) << endl;
    // }
    // while (!q.empty()) {
    //     cout << f(s, q.front()) << endl;
    //     q.pop();
    // }

    vector<string> vs(all(st));
    if (vs.size() <= 10) {
      rep(i, vs.size()) { cout << vs[i] << endl; }
    } else {
      rep(i, 5) { cout << vs[i] << endl; }
      rep2(i, (int)vs.size() - 5, vs.size()) { cout << vs[i] << endl; }
    }
  }
}