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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

string str;
vs ans;

void solve(int i, string s, string used) {
  if (i == str.size()) {
    ans.push_back(s);
    return;
  }
  if (str[i] == 'a') {
    solve(i + 1, s + str[i], used);
    if (used.find(str[i]) == -1)
      solve(i + 1, s + (char)(str[i] + 1), used + str[i]);
    return;
  }
  if (str[i] != 'z' && used.find(str[i]) == -1) {
    solve(i + 1, s + (char)(str[i] + 1), used + str[i]);
  }
  if (s.find(str[i]) != -1) {
    solve(i + 1, s + str[i], used);
  }

  return;
}

int main(void) {

  while (cin >> str, str != "#") {

    solve(0, "", "");

    sort(all(ans));
    ans.erase(unique(ans.begin(), ans.end()), ans.end());
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      rep(i, ans.size()) { cout << ans[i] << endl; }
    } else {
      rep(i, 5) { cout << ans[i] << endl; }
      rep(i, 5) { cout << ans[ans.size() - 5 + i] << endl; }
    }
    ans.clear();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;

int main(void) {
  string s;
  while (cin >> s, s != "#") {
    int n = s.size();
    vs ans;

    rep(bit, 1 << n) {
      string t = "";
      rep(i, n) {
        if (bit & (1 << i)) {
          if (s[i] == 'z')
            break;
          t += s[i] + 1;
        } else {
          t += s[i];
        }
      }
      if (t.size() != n)
        continue;
      string tmp = t;
      vi alph(26);
      rep(i, n) if (t[i] != 'a') {
        if (alph[t[i] - 'a'] == 0) {
          alph[t[i] - 'a']++;
          t[i]--;
        }
      }

      if (t == s)
        ans.push_back(tmp);
    }

    sort(all(ans));
    cout << ans.size() << endl;
    if (ans.size() <= 10) {
      rep(i, ans.size()) { cout << ans[i] << endl; }
    } else {
      rep(i, 5) { cout << ans[i] << endl; }
      rep(i, 5) { cout << ans[ans.size() - 5 + i] << endl; }
    }
  }
}