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

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

// ll n, m, k, ans = 0, sum = 0, cnt = 0;
// string s;
// char c;

/*--------------------template--------------------*/

vector<string> ans;
bool ap[30];
string s;
int sum;

void dfs(int n, char c, string now) {
  // cout<<"dfs(int "<<n<<",char "<<c<<",string "<<now<<")"<<endl;
  if (n == s.size()) {
    sum++;
    ans.push_back(now);
    return;
  }
  if (ap[c - 'a'] == true || c == 'a') {
    bool bac = ap[c - 'a'];
    ap[c - 'a'] = true;
    dfs(n + 1, s[n + 1], now + c);
    ap[c - 'a'] = bac;
  }
  if (ap[c - 'a' + 1] == false && c != 'z') {
    bool bac = ap[c - 'a' + 1];
    ap[c - 'a' + 1] = true;
    dfs(n + 1, s[n + 1], now + (char)(c + 1));
    ap[c - 'a' + 1] = bac;
  }
}

int main() {
  while (1) {
    while (!ans.empty())
      ans.pop_back();
    sum = 0;
    cin >> s;
    if (s == "#")
      break;
    dfs(0, s[0], "");
    cout << sum << endl;

    if (sum <= 10) {
      for (int i = 0; i < sum; i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/

#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, d, n) for (int i = (d); i < (n); i++)
int main() {
  while (true) {
    string str;
    cin >> str;
    if (str == "#")
      break;
    set<string> ans;
    REP(i, (1 << str.length())) {
      string use = str;
      bool change = false;
      REP(j, str.length()) {
        if (((1 << j) & i) && use[j] != 'z') {
          change = true;
          use[j]++;
        }
      }
      if (!change && i != 0) {
        continue;
      }
      string bufstr = use;
      REP(j, 25) {
        REP(k, use.size()) {
          if (use[k] == 'a' + j + 1) {
            use[k]--;
            break;
          }
        }
      }
      if (use == str) {
        ans.insert(bufstr);
      }
    }
    vector<string> ans2(ALL(ans));
    cout << ans2.size() << endl;
    if (ans2.size() <= 10) {
      REP(i, ans2.size()) { cout << ans2[i] << endl; }
    } else {
      REP(i, 5) { cout << ans2[i] << endl; }
      REP2(i, ans2.size() - 5, ans2.size()) { cout << ans2[i] << endl; }
    }
  }
}