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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

string S;
int f[26];
int Ssize;

inline string parse(string s, int b) {
  rep(i, Ssize) {
    if ((b >> i) & 1 /* && s[i] < 'z'*/) {
      ++s[i];
    }
  }
  return s;
}

inline int C(string s) {
  memset(f, 0, sizeof(f));
  rep(i, Ssize) {
    if (!f[s[i] - 'a'] && 'a' < s[i]) {
      f[s[i] - 'a'] = 1;
      --s[i];
    }
  }
  return s == S;
}

int main() {
  while (cin >> S, S != "#") {
    Ssize = S.size();
    //    set<string>Se;
    vector<string> Se;
    int ret = 0;
    int bitset = 0;
    rep(i, Ssize) {
      if (S[i] == 'z')
        bitset |= 1 << i;
    }
    rep(i, 1 << Ssize) {
      if (i & bitset)
        continue;
      string s = parse(S, i);
      if (C(s)) {
        //	Se.insert(s);
        Se.push_back(s);
      }
    }

    sort(Se.begin(), Se.end());
    //    Se.erase(unique(Se.begin(), Se.end()), Se.end());
    int Sesize = Se.size();
    cout << Sesize << endl;
    if (Sesize < 10) {
      for (auto s : Se)
        cout << s << endl;
    } else {
      auto it = Se.begin();
      rep(i, 5) {
        cout << *it << endl;
        it++;
      }
      it = Se.end();
      rep(i, 5) it--;
      rep(i, 5) {
        cout << *it << endl;
        it++;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

string S;
int f[26];
int Ssize;

inline string parse(string s, int b) {
  rep(i, Ssize) {
    if ((b >> i) & 1 && s[i] < 'z') {
      ++s[i];
    }
  }
  return s;
}

inline int C(string s) {
  memset(f, 0, sizeof(f));
  rep(i, Ssize) {
    if (!f[s[i] - 'a'] && 'a' < s[i]) {
      f[s[i] - 'a'] = 1;
      --s[i];
    }
  }
  return s == S;
}

int main() {
  while (cin >> S, S != "#") {
    Ssize = S.size();
    //    set<string>Se;
    vector<string> Se;
    int ret = 0;
    rep(i, 1 << S.size()) {
      string s = parse(S, i);
      if (C(s))
        //	Se.insert(s);
        Se.push_back(s);
    }

    sort(Se.begin(), Se.end());
    Se.erase(unique(Se.begin(), Se.end()), Se.end());
    int Sesize = Se.size();
    cout << Sesize << endl;
    if (Sesize < 10) {
      for (auto s : Se)
        cout << s << endl;
    } else {
      auto it = Se.begin();
      rep(i, 5) {
        cout << *it << endl;
        it++;
      }
      it = Se.end();
      rep(i, 5) it--;
      rep(i, 5) {
        cout << *it << endl;
        it++;
      }
    }
  }
  return 0;
}