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

#include <iostream>
#include <set>
#include <string>
#define F(A, B) for (int A = 0; A < B; A++)
using namespace std;

string enc(string str) {
  for (char ch = 'b'; ch <= 'z'; ch++) {
    F(i, str.length()) {
      if (str[i] == ch) {
        str[i] -= 1;
        break;
      }
    }
  }
  return str;
}

/*string enc(string str){
        int enc_log[30]={0};
        for(int i=0; i<str.length(); i++){
                if(enc_log[str[i]-'a']==0 && str[i]!='a'){
                        enc_log[str[i]-'a'] = 1;
                        str[i]--;
                }
        }
        return str;
}*/

void rec(set<string> &ans, const string &ori, string str, int &cnt) {
  if (str.length() == ori.length()) {
    ans.insert(str);
    cnt++;
    if (ans.size() == 11) {
      set<string>::iterator it = ans.begin();
      F(i, 5) it++;
      ans.erase(it);
    }
    return;
  }
  // for(int i=pos; i<ori.length(); i++){
  if (str.length() != ori.length()) {
    char tmp = ori[str.length()];
    if (ori.substr(0, str.length() + 1) == enc(str + tmp)) {
      rec(ans, ori, str + tmp, cnt);
    }
    tmp++;
    if (tmp != 'z' + 1 && ori.substr(0, str.length() + 1) == enc(str + tmp)) {
      rec(ans, ori, str + tmp, cnt);
    }
  }
  return;
}

int main() {
  string str;
  cin >> str;
  while (str != "#") {
    int cnt = 0, pos = 0;
    set<string> ans;
    rec(ans, str, "", cnt);
    cout << cnt << endl;
    for (set<string>::iterator it = ans.begin(); it != ans.end(); it++) {
      cout << *it << endl;
    }
    cin >> str;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#define F(A, B) for (int A = 0; A < B; A++)
using namespace std;

/*string enc(string str){
        int index;
        for(char ch='b'; ch<='z'; ch++){
                index = str.find(ch);
                if(index != string::npos){
                        str[index] -= 1;
                }
        }
        return str;
}*/

string enc(string str) {
  int enc_log[30] = {0};
  for (int i = 0; i < str.length(); i++) {
    if (enc_log[str[i] - 'a'] == 0 && str[i] != 'a') {
      enc_log[str[i] - 'a'] = 1;
      str[i]--;
    }
  }
  return str;
}

void rec(vector<string> &ans, const string &ori, string str, int pos, int &cnt,
         int log[30]) {
  if (ori == enc(str)) {
    ans.push_back(str);
    cnt++;
    /*if(ans.size() == 11){
            vector<string>::iterator it=ans.begin();
            F(i, 5)it++;
            ans.erase(it);
    }*/
  }
  for (int i = pos; i < ori.length(); i++) {
    string tmp = str;
    if (tmp[i] != 'z' && log[tmp[i] - 'a'] == 0) {
      int log_c[30];
      memcpy(log_c, log, 30 * sizeof(int));
      log_c[tmp[i] - 'a'] = 1;
      tmp[i]++;
      rec(ans, ori, tmp, i + 1, cnt, log_c);
    }
  }
  return;
}

int main() {
  string str;
  cin >> str;
  while (str != "#") {
    int cnt = 0, pos = 0, log[30] = {0};
    vector<string> ans;
    rec(ans, str, str, pos, cnt, log);
    sort(ans.begin(), ans.end());
    cout << cnt << endl;
    if (ans.size() < 10) {
      for (vector<string>::iterator it = ans.begin(); it != ans.end(); it++) {
        cout << *it << endl;
      }
    } else {
      vector<string>::iterator it = ans.begin();
      F(i, 5) {
        cout << *it << endl;
        it++;
      }
      it = ans.end();
      F(i, 5) { it--; }
      F(i, 5) {
        cout << *it << endl;
        it++;
      }
    }
    cin >> str;
  }
}