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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

/*
string enc(string s){

        bool app[26]={false};

        string str=s;
        char sz=str.size();
        for(char i=0;i<sz;++i){
                char c=s[i]-'a';
                if(!app[c] && c!=0){
                        --str[i];
                        app[c]=true;
                }
        }

        return str;

}
*/
int main() {

  ios::sync_with_stdio(false);
  string s;

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

    set<string> ss1, ss2;
    vector<string> ans;
    int n = s.size() / 2;
    int m = s.size() - n;
    ULL N = (1ULL << n);
    ULL M = (1ULL << m);
    for (ULL state = 0; state < N; ++state) {
      string str = s.substr(0, n);
      for (char i = 0; i < n; ++i) {
        if ((1 << i) & state && str[i] != 'z')
          str[i]++;
      }

      ss1.insert(str);
    }

    for (ULL state = 0; state < M; ++state) {
      string str = s.substr(n);
      for (char i = 0; i < m; ++i) {
        if ((1 << i) & state && str[i] != 'z')
          str[i]++;
      }

      ss2.insert(str);
    }

    for (auto it1 = ss1.begin(); it1 != ss1.end(); ++it1) {
      for (auto it2 = ss2.begin(); it2 != ss2.end(); it2++) {
        //				if(enc(str1+(*it2))==s)
        //ans.push_back(str1+(*it2)+"\\n");
        string str = (*it1) + (*it2);
        bool app[26] = {false};
        for (char i = 0; i < n + m; ++i) {
          char c = str[i] - 'a';
          if (!app[c] && c != 0) {
            --str[i];
            app[c] = true;
          }
        }

        if (str == s)
          ans.push_back((*it1) + (*it2) + "\\n");
      }
    }

    const ULL sz = ans.size();
    cout << sz << endl;

    if (sz <= 10) {
      for (char i = 0; i < sz; i++)
        cout << ans[i];
    } else {
      cout << ans[0] + ans[1] + ans[2] + ans[3] + ans[4] + ans[sz - 5] +
                  ans[sz - 4] + ans[sz - 3] + ans[sz - 2]
           << ans[sz - 1];
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define MP make_pair
typedef unsigned long long ULL;

string enc(string s) {

  bool app[26] = {false};

  string str = s;
  char sz = str.size();
  for (char i = 0; i < sz; ++i) {
    char c = s[i] - 'a';
    if (!app[c] && c != 0) {
      --str[i];
      app[c] = true;
    }
  }

  return str;
}

int main() {

  ios::sync_with_stdio(false);
  string s;

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

    set<string> ss1, ss2;
    vector<string> ans;
    int n = s.size() / 2;
    int m = s.size() - n;
    ULL N = (1ULL << n);
    ULL M = (1ULL << m);
    for (ULL state = 0; state < N; ++state) {
      string str = s.substr(0, n);
      char sz = str.size();
      for (char i = 0; i < sz; ++i) {
        if ((1 << i) & state && str[i] != 'z')
          str[i]++;
      }

      ss1.insert(str);
    }

    for (ULL state = 0; state < M; ++state) {
      string str = s.substr(n);
      char sz = str.size();
      for (char i = 0; i < sz; ++i) {
        if ((1 << i) & state && str[i] != 'z')
          str[i]++;
      }

      ss2.insert(str);
    }

    for (auto it1 = ss1.begin(); it1 != ss1.end(); ++it1) {
      string str1 = (*it1);
      for (auto it2 = ss2.begin(); it2 != ss2.end(); it2++) {
        if (enc(str1 + (*it2)) == s)
          ans.push_back(str1 + (*it2) + "\\n");
      }
    }

    const ULL sz = ans.size();
    cout << sz << endl;

    if (sz <= 10) {
      for (char i = 0; i < sz; i++)
        cout << ans[i];
    } else {
      cout << ans[0] + ans[1] + ans[2] + ans[3] + ans[4] + ans[sz - 5] +
                  ans[sz - 4] + ans[sz - 3] + ans[sz - 2]
           << ans[sz - 1];
    }
  }

  return 0;
}