Problem p00776 (C++) — buggy submission, verdict: Memory 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;

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: MLE

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;

int main() {

  ios::sync_with_stdio(false);
  string s;

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

    set<string> ss;
    vector<string> ans;
    int n = s.size();
    ULL N = (1ULL << n);
    for (ULL state = 0; state < N; ++state) {
      string str = s;
      for (char i = 0; i < n; ++i) {
        if ((1 << i) & state && str[i] != 'z')
          str[i]++;
      }

      ss.insert(str);
    }

    for (auto it = ss.begin(); it != ss.end(); ++it) {
      string str = (*it);
      bool app[26] = {false};
      for (char i = 0; i < n; ++i) {
        char c = str[i] - 'a';
        if (!app[c] && c != 0) {
          --str[i];
          app[c] = true;
        }
      }

      if (str == s)
        ans.push_back((*it) + "\\n");
    }
    sort(ans.begin(), ans.end());

    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;
}