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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

string s;
vector<string> vs;
bool used[100];

string changes(ll n) {
  string t = s;
  // cout << n << endl;
  rep(i, s.size()) {
    // cout << (n&(1 << (s.size() - 1 - i)));
    if (n & (1 << i))
      t[i]++;
  }
  // cout << endl;
  return t;
}

int main() {
  while (cin >> s && s != "#") {
    int ans = 0;
    rep(i, (1 << s.size())) {
      bool f = 1;
      fill(used, used + 100, 0);
      rep(j, s.size()) {
        if (i & (1 << j)) {
          if (s[j] == 'z')
            f = 0;
          if (used[s[j] - 'a' + 1])
            f = 0;
          used[s[j] - 'a' + 1] = 1;
        } else {
          if (s[j] != 'a' && !used[s[j] - 'a'])
            f = 0;
        }
      }
      if (f)
        vs.push_back(changes(i));
    }
    cout << vs.size() << endl;
    sort(ALL(vs));
    if (vs.size() > 10) {
      rep(i, 5) { cout << vs[i] << endl; }
      rep(i, 5) { cout << vs[vs.size() - 5 + i] << endl; }
    } else {
      rep(i, vs.size()) { cout << vs[i] << endl; }
    }
    vs.clear();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e12;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num * num; }
ll mod_pow(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return x % MOD;
  ll res = sq(mod_pow(x, n / 2));
  res %= MOD;
  if (n % 2 == 1) {
    res *= x;
    res %= MOD;
  }
  return res;
}
ll mod_add(ll a, ll b) { return (a + b) % MOD; }
ll mod_sub(ll a, ll b) { return (a - b + MOD) % MOD; }
ll mod_mul(ll a, ll b) { return a * b % MOD; }

string s;
string t;
priority_queue<string, vector<string>, greater<string>> lque;
priority_queue<string> sque;

string conv(int num) {
  string res = s;
  rep(i, s.size()) { res[i] += ((num >> s.size() - 1 - i) & 1); }
  return res;
}

int main() {
  while (cin >> s && s != "#") {
    while (!lque.empty())
      lque.pop();
    while (!sque.empty())
      sque.pop();
    int cnt = 0;
    rep(i, (1 << s.size())) {
      t = conv(i);
      bool F[100] = {};
      bool f = 1;
      FOR(i, 1, 26) {
        rep(j, t.size()) {
          if (t[j] < 'a' || 'z' < t[j]) {
            f = 0;
            break;
          }
          if (t[j] == (char)('a' + i)) {
            F[j] = 1;
            if (s[j] != (char)('a' + i - 1))
              f = 0;
            break;
          }
        }
        if (!f)
          break;
      }
      rep(j, t.size()) {
        if (!F[j]) {
          if (s[j] != t[j])
            f = 0;
        }
      }
      if (f) {
        cnt++;
        if (lque.size() < 5)
          lque.push(t);
        else {
          if (lque.top() < t) {
            lque.pop();
            lque.push(t);
          }
        }
        if (sque.size() < 10)
          sque.push(t);
        else {
          if (sque.top() > t) {
            sque.pop();
            sque.push(t);
          }
        }
      }
    }
    cout << cnt << endl;
    vector<string> vs;
    while (!sque.empty()) {
      vs.push_back(sque.top());
      sque.pop();
    }
    if (cnt >= 10) {
      while (!lque.empty()) {
        if (find(ALL(vs), t) == vs.end())
          vs.push_back(lque.top());
        lque.pop();
      }
    }
    sort(ALL(vs));
    if (cnt <= 10) {
      rep(i, cnt) { cout << vs[i] << endl; }
    } else {
      rep(i, 5) { cout << vs[i] << endl; }
      for (int i = 4; i >= 0; i--) {
        cout << vs[vs.size() - 1 - i] << endl;
      }
    }
  }
}