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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

string ch;
vector<string> vs;
vector<bool> used;

void dfs(string s, int index) {
  if (index == (int)s.size()) {
    vs.pb(s);
    return;
  }
  if (s[index] == 'a' || used[s[index] - 'a']) {
    dfs(s, index + 1);
  }
  if (s[index] != 'z' && !used[s[index] + 1 - 'a']) {
    s[index]++;
    used[s[index] - 'a'] = true;
    dfs(s, index + 1);
    used[s[index] - 'a'] = false;
    s[index]--;
  }
}

void solve(string str) {
  used.assign(25, false);
  dfs(str, 0);
  cout << vs.size() << endl;
  if (vs.size() <= 10) {
    for (int i = 0; i < vs.size(); i++)
      cout << vs[i] << endl;
  } else {
    for (int i = 0; i < 5; i++)
      cout << vs[i] << endl;
    for (int i = vs.size() - 5; i < vs.size(); i++)
      cout << vs[i] << endl;
  }
}

int main() {
  string s;
  while (cin >> s) {
    ch = s;
    if (s == "#")
      break;
    vs.clear();
    solve(s);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;

/*---------------------------------------------------*/

string ch;
vector<string> vs;

bool check(string s) {
  vector<bool> used(25, false);
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != 'a' && !used[s[i] - 'a']) {
      used[s[i] - 'a'] = true;
      s[i] = s[i] - 1;
    }
  }

  return s == ch;
}

void dfs(string s, int index) {
  if (index == -1) {
    if (check(s)) {
      vs.pb(s);
    }
    return;
  }
  // cout<<s<<" "<<index<<endl;
  if (s[index] != 'z') {
    dfs(s, index - 1);
    s[index]++;
    dfs(s, index - 1);
    s[index]--;
  } else {
    dfs(s, index - 1);
  }
  return;
}

void solve(string str) {
  dfs(str, str.size() - 1);
  // sort(all(vs));
  cout << vs.size() << endl;
  if (vs.size() <= 10) {
    for (int i = 0; i < vs.size(); i++)
      cout << vs[i] << endl;
  } else {
    for (int i = 0; i < 5; i++)
      cout << vs[i] << endl;
    for (int i = vs.size() - 5; i < vs.size(); i++)
      cout << vs[i] << endl;
  }
}

int main() {
  string s;
  while (cin >> s) {
    ch = s;
    if (s == "#")
      break;
    vs.clear();
    solve(s);
  }
  return 0;
}