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

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  char ss[25] = "";
  while (cin >> ss, ss[0] != '#') {
    string str = ss;
    vector<string> prevCond(6, string(25, 'z')), aftCond(6, string(1, '0'));
    auto restore = [&](string s) -> bool {
      bool used[26] = {true};
      bool ok = true;
      REP(i, 0, s.size()) {
        if (!used[s[i] - 'a']) {
          used[s[i] - 'a'] = true;
          s[i]--;
        }
        ok &= (s[i] == str[i]);
      }
      return ok;
    };
    int ans = 0;
    int msk = 0;
    REP(i, 0, str.size()) {
      if (str[i] == 'z') {
        msk |= (1 << i);
      }
    }
    REP(i, 0, 1 << str.size()) {
      if ((i & msk) != 0)
        continue;
      string work = str;
      REP(j, 0, work.size()) {
        if (((i >> j) & 1) == 1)
          work[j]++;
      }
      if (restore(work)) {
        ans++;
        int t;
        for (t = 5; t > 0; t--) {
          if (work < prevCond[t - 1]) {
            prevCond[t] = prevCond[t - 1];
          } else {
            break;
          }
        }
        prevCond[t] = work;

        for (t = 5; t > 0; t--) {
          if (work > aftCond[t - 1]) {
            aftCond[t] = aftCond[t - 1];
          } else {
            break;
          }
        }
        aftCond[t] = work;
      }
    }
    cout << ans << endl;
    reverse(begin(aftCond), end(aftCond) - 1);
    if (ans <= 5) {
      REP(i, 0, ans) { cout << prevCond[i] << endl; }
    } else {
      REP(i, 0, 5) { cout << prevCond[i] << endl; }
      REP(i, max(10 - ans, 0), 5) { cout << aftCond[i] << endl; }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template <typename T> vector<T> make_v(size_t a, T b) {
  return vector<T>(a, b);
}

template <typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string str;
  while (cin >> str, str != "#") {
    vector<string> prevCond(6, string(25, 'z')), aftCond(6, string(1, '0'));
    auto restore = [&](string s) -> bool {
      bool used[26] = {true};
      REP(i, 0, s.size()) {
        if (!used[s[i] - 'a']) {
          used[s[i] - 'a'] = true;
          s[i]--;
        }
      }
      return (s == str);

      /*
      for (char c = 'b'; c <= 'z'; c++) {
          REP(i, 0, s.size()) {
              if (s[i] == c) {
                  s[i]--;
                  break;
              }
          }
      }
      return (s == str);
      */
    };
    int ans = 0;
    int msk = 0;
    REP(i, 0, str.size()) {
      if (str[i] == 'z') {
        msk |= (1 << i);
      }
    }
    REP(i, 0, 1 << str.size()) {
      if ((i & msk) != 0)
        continue;
      string work = str;
      REP(j, 0, work.size()) {
        if (((i >> j) & 1) == 1)
          work[j]++;
      }
      if (restore(work)) {
        ans++;
        prevCond[5] = work;
        for (int t = 5; t > 0; t--) {
          if (prevCond[t] < prevCond[t - 1]) {
            swap(prevCond[t], prevCond[t - 1]);
          }
        }
        aftCond[5] = work;
        for (int t = 5; t > 0; t--) {
          if (aftCond[t] > aftCond[t - 1]) {
            swap(aftCond[t], aftCond[t - 1]);
          }
        }
      }
    }
    cout << ans << endl;
    reverse(begin(aftCond), end(aftCond) - 1);
    if (ans <= 5) {
      REP(i, 0, ans) { cout << prevCond[i] << endl; }
    } else {
      REP(i, 0, 5) { cout << prevCond[i] << endl; }
      REP(i, max(10 - ans, 0), 5) { cout << aftCond[i] << endl; }
    }
  }
  return 0;
}