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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, 0, -1, 1, -1, -1, 1};
const int dy[] = {0, 1, -1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
set<string> memo;
char str[32];
void dfs(string s, int bit, int idx, int len) {
  // cout <<idx << ":" << str << "\\n";
  if (s.size() == len) {
    memo.insert(s);
    return;
  }
  if (str[idx] == 'z') {
    if (bit & 1 << ('z' - 'a'))
      dfs(s + "z", bit | 1 << ('z' - 'a'), idx + 1, len);
  } else {
    if (bit & 1 << (str[idx] - 'a')) {
      dfs(s + str[idx], bit | 1 << (str[idx] - 'a'), idx + 1, len);
    }
    if ((bit & 1 << (str[idx] - 'a' + 1)) == 0) {
      dfs(s + (char)(str[idx] + 1), bit | 1 << (str[idx] - 'a' + 1), idx + 1,
          len);
    }
  }
  return;
}
int main() {
  while (true) {
    scanf("%s", str);
    if (strcmp(str, "#") == 0) {
      break;
    }
    memo.clear();
    int length = strlen(str);
    dfs("", 1, 0, length);
    set<string>::iterator it;
    printf("%d\\n", memo.size());
    if (memo.size() < 10) {
      for (it = memo.begin(); it != memo.end(); it++) {
        printf("%s\\n", (*(it)).c_str());
      }
    } else {
      int cnt = 0;
      for (it = memo.begin(); it != memo.end(); it++) {
        if (cnt < 5 || cnt >= memo.size() - 5)
          printf("%s\\n", (*(it)).c_str());
        cnt++;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, 0, -1, 1, -1, -1, 1};
const int dy[] = {0, 1, -1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long ll;
set<string> memo;
int main() {
  char str[32];
  while (true) {
    scanf("%s", str);
    if (strcmp(str, "#") == 0) {
      break;
    }
    memo.clear();
    int length = strlen(str);
    rep(i, 1 << length) {
      char tmp[32];
      strcpy(tmp, str);
      int bit = 0;
      rep(j, length) {
        if (1 & (i >> j)) {
          if (tmp[j] != 'z') {
            if (bit & 1 << (tmp[j] - 'a'))
              continue;
            bit |= 1 << (tmp[j] - 'a');
            tmp[j]++;
          }
        }
      }
      char p[32];
      strcpy(p, tmp);
      /*bit = 0;
      rep(i,length){
              bit |= 1<<(p[i]-'a');
      }*/

      if (memo.find(p) != memo.end())
        continue;

      reps(i, 'b', 'z' + 1) {
        // if(1<<i & bit == 0)continue;
        rep(j, length) {
          if (tmp[j] == i) {
            tmp[j]--;
            break;
          }
        }
      }
      if (strcmp(str, tmp) == 0) {
        memo.insert(p);
      }
    }
    set<string>::iterator it;
    printf("%d\\n", memo.size());
    if (memo.size() < 10) {
      for (it = memo.begin(); it != memo.end(); it++) {
        printf("%s\\n", (*(it)).c_str());
      }
    } else {
      int cnt = 0;
      for (it = memo.begin(); it != memo.end(); it++) {
        if (cnt < 5 || cnt >= memo.size() - 5)
          printf("%s\\n", (*(it)).c_str());
        cnt++;
      }
    }
  }
  return 0;
}