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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

set<string> anses;

string orig;
int N;

bool is_ok(string s) {
  for (char c = 'b'; c <= 'z'; c++) {
    auto pos = s.find(c);
    if (pos != string::npos) {
      s[pos]--;
    }
  }
  return orig == s;
}

void dfs(string &s, int step, int cnt, bool must_find) {
  if (step == 25 || cnt == N) {
    if (is_ok(s)) {
      anses.insert(s);
    }
    return;
  }

  bool is_find = false;
  rep(i, N) {
    if (s[i] == 'y' - step) {
      string ns(s);
      ns[i]++;
      dfs(ns, step + 1, cnt + 1, false);
      is_find = true;
    }
  }
  /*
  auto pos = s.find('y' - step);
  bool is_find = pos != string::npos;
  if (is_find) {
      string ns(s);
      ns[pos]++;
      dfs(ns, step + 1, cnt + 1, false);
  }*/

  if ((must_find && is_find) || !must_find) {
    dfs(s, step + 1, cnt, is_find);
  }
}

int main(void) {
  while (cin >> orig, orig != "#") {
    N = orig.size();
    anses.clear();
    dfs(orig, 0, 0, false);

    VS oks;
    for (auto s : anses) {
      oks.PB(s);
    }

    cout << oks.size() << endl;

    if (oks.size() <= 10) {
      for (auto s : oks) {
        cout << s << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << oks[i] << endl;
      }
      for (int i = 4; i >= 0; i--) {
        cout << oks[oks.size() - 1 - i] << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
// #define Y first
// #define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define rep(i, N) for (int i = 0; i < (N); i++)
#define rep2(i, j, N, M) rep(i, N) rep(j, M)
#define rep3(i, j, k, N, M, L) rep(i, N) rep(j, M) rep(k, L)
#define REP(i, init, N) for (int i = (init); i < (N); i++)

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const double DINF = 0x3f3f3f3f;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

set<string> anses;

string orig;
int N;

bool is_ok(string s) {
  for (char c = 'b'; c <= 'z'; c++) {
    auto pos = s.find(c);
    if (pos != string::npos) {
      s[pos]--;
    }
  }
  return orig == s;
}

void dfs(string &s, int step, int cnt) {
  if (step == 25 || cnt == N) {
    if (is_ok(s)) {
      anses.insert(s);
    }
    return;
  }

  auto pos = s.find('y' - step);
  if (pos != string::npos) {
    string ns(s);
    ns[pos]++;
    dfs(ns, step + 1, cnt + 1);
  }

  dfs(s, step + 1, cnt);
}

int main(void) {
  while (cin >> orig, orig != "#") {
    N = orig.size();
    anses.clear();
    dfs(orig, 0, 0);

    VS oks;
    for (auto s : anses) {
      oks.PB(s);
    }

    cout << oks.size() << endl;

    if (oks.size() <= 10) {
      for (auto s : oks) {
        cout << s << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << oks[i] << endl;
      }
      for (int i = 4; i >= 0; i--) {
        cout << oks[oks.size() - 1 - i] << endl;
      }
    }
  }

  return 0;
}