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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define eb emplace_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

// cout vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\\t";
  }
  return s;
}

// cout 2-dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

vi search(char c, string s) {
  int len = (int)s.length();
  vi ret;
  rep(i, len) {
    if (s[i] == c) {
      ret.eb(i);
    }
  }
  return ret;
}

int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == "#") {
      break;
    }
    vv<string> cand(26);
    cand[0].eb(s);
    rep(step, 25) {
      char prev = 'z' - step;
      char curr = prev - 1;
      for (auto str : cand[step]) {
        vi pre = search(prev, str);
        vi cur = search(curr, str);
        if ((int)pre.size() > 0) {
          if (cur.empty()) {
            continue;
          }
          if (cur[0] > pre[0]) {
            continue;
          }
          string can = str;
          can[cur[0]] += 1;
          cand[step + 1].eb(can);
        } else {
          cand[step + 1].eb(str);
          rep(i, (int)cur.size()) {
            string can = str;
            can[cur[i]] += 1;
            cand[step + 1].eb(can);
          }
        }
      }
    }
    vector<string> ans;
    ans = cand[25];
    sort(all(ans));
    ans.erase(unique(all(ans)), end(ans));
    int n = ans.size();
    printf("%d\\n", n);
    if (n <= 10) {
      rep(i, n) { cout << ans[i] << "\\n"; }
    } else {
      rep(i, 5) { cout << ans[i] << "\\n"; }
      rep(i, 5) { cout << ans[n - 5 + i] << "\\n"; }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

// cout vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\\t";
  }
  return s;
}

// cout 2-dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

string encrypt(string s) {
  int len = s.length();
  for (char c = 'b'; c <= 'z'; ++c) {
    rep(i, len) {
      if (s[i] == c) {
        s[i] -= 1;
        break;
      }
    }
  }
  return s;
}

int pow2(int n) {
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return 2;
  }
  int m = pow2(n / 2);
  m *= m;
  if (n % 2 == 1) {
    m *= 2;
  }
  return m;
}

int main() {
  while (true) {
    string s;
    cin >> s;
    if (s == "#") {
      break;
    }
    int len = s.length();
    int imax = pow2(len);
    string copy = s;
    vector<string> ans;
    rep(i, imax) {
      rep(j, len) {
        if ((i >> j) % 2 == 1) {
          if (s[j] == 'z') {
            break;
          }
          s[j] += 1;
        }
      }
      if (encrypt(s) == copy) {
        ans.pb(s);
      }
      s = copy;
    }
    int n = ans.size();
    printf("%d\\n", n);
    if (n <= 10) {
      rep(i, n) { cout << ans[i] << "\\n"; }
    } else {
      rep(i, 5) { cout << ans[i] << "\\n"; }
      rep(i, 5) { cout << ans[n - 5 + i] << "\\n"; }
    }
  }

  return 0;
}