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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

string s;

bool isvalid(string t) {
  bool used[30] = {};
  rep(i, 0, t.length()) {
    int v = t[i] - 'a';
    if (used[v])
      continue;
    used[v] = true;
    if (v != 0)
      t[i]--;
  }
  return s == t;
}

void solve(int p, string &cur, set<string> &st) {
  if (p == s.length()) {
    if (isvalid(cur))
      st.insert(cur);
  } else {
    cur += s[p];
    solve(p + 1, cur, st);
    cur.pop_back();
    if (s[p] != 'z') {
      cur += (char)(s[p] + 1);
      solve(p + 1, cur, st);
      cur.pop_back();
    }
  }
}

signed main() {
  while (cin >> s) {
    if (s == "#")
      break;
    set<string> st;
    string cur = "";
    solve(0, cur, st);
    auto it = st.begin();
    int n = st.size();
    cout << n << endl;
    if (n <= 10)
      for (auto x : st)
        cout << x << endl;
    else {
      rep(i, 0, 5) cout << *it << endl, it++;
      it = st.end();
      rep(i, 0, 5) it--;
      rep(i, 0, 5) cout << *it << endl, it++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

string s;

bool isvalid(string t) {
  bool used[30] = {};
  rep(i, 0, t.length()) {
    int v = t[i] - 'a';
    if (used[v])
      continue;
    used[v] = true;
    if (v != 0)
      t[i]--;
  }
  return s == t;
}

void solve(int p, string cur, set<string> &st) {
  if (p == s.length()) {
    if (isvalid(cur))
      st.insert(cur);
  } else {
    solve(p + 1, cur + s[p], st);
    if (s[p] != 'z')
      solve(p + 1, cur + (char)(s[p] + 1), st);
  }
}

signed main() {
  while (cin >> s, s != "#") {
    set<string> st;
    solve(0, "", st);
    auto it = st.begin();
    int n = st.size();
    cout << n << endl;
    if (n <= 10)
      for (auto x : st)
        cout << x << endl;
    else {
      rep(i, 0, 5) cout << *it << endl, it++;
      it = st.end();
      rep(i, 0, 5) it--;
      rep(i, 0, 5) cout << *it << endl, it++;
    }
  }
  return 0;
}