Problem p01908 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

class trie {
public:
  trie *next[26];
  trie() { fill(next, next + 26, (trie *)0); }
  void insert(const string &str) { insert(str, 0); }
  bool find(const string &str) { return find(str, 0); }
  void insert(const string &str, const int idx) {
    if (idx == str.size())
      return;
    if (this->next[str[idx] - 'a'] == NULL)
      this->next[str[idx] - 'a'] = new trie();
    this->next[str[idx] - 'a']->insert(str, idx + 1);
  }
  bool find(const string &str, const int idx) {
    if (idx == str.size())
      return true;
    if (this->next[str[idx] - 'a'] == NULL)
      return false;
    return this->next[str[idx] - 'a']->find(str, idx + 1);
  }
};

void solve() {
  int n;
  cin >> n;
  set<string> st;
  rep(i, 0, n) {
    string s;
    cin >> s;
    rep(j, 0, s.size()) {
      rep(k, 1, 5) {
        if (j + k > s.size())
          break;
        st.insert(s.substr(j, k));
      }
    }
  }

  queue<string> que;
  rep(i, 0, 26) que.push(string(1, 'a' + i));
  while (true) {
    auto s = que.front();
    que.pop();
    if (st.find(s) == st.end()) {
      cout << s << endl;
      return;
    }
    if (s.size() == 4)
      continue;
    rep(i, 0, 26) {
      s.push_back('a' + i);
      que.push(s);
      s.pop_back();
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define print(x)
#endif

const int inf = 1e9;
const int64_t inf64 = 1e18;
const double eps = 1e-9;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

using i64 = int64_t;

class trie {
public:
  trie *next[26];
  trie() { fill(next, next + 26, (trie *)0); }
  void insert(const string &str) { insert(str, 0); }
  bool find(const string &str) { return find(str, 0); }
  void insert(const string &str, const int idx) {
    if (idx == str.size())
      return;
    if (this->next[str[idx] - 'a'] == NULL)
      this->next[str[idx] - 'a'] = new trie();
    this->next[str[idx] - 'a']->insert(str, idx + 1);
  }
  bool find(const string &str, const int idx) {
    if (idx == str.size())
      return true;
    if (this->next[str[idx] - 'a'] == NULL)
      return false;
    return this->next[str[idx] - 'a']->find(str, idx + 1);
  }
};

void solve() {
  int n;
  cin >> n;
  set<string> st;
  rep(i, 0, n) {
    string s;
    cin >> s;
    rep(j, 0, s.size()) {
      rep(k, 1, 5) {
        if (j + k > s.size())
          break;
        st.insert(s.substr(j, k));
      }
    }
  }

  queue<string> que;
  rep(i, 0, 26) que.push(string(1, 'a' + i));
  while (true) {
    auto s = que.front();
    que.pop();
    if (st.find(s) == st.end()) {
      cout << s << endl;
      return;
    }
    rep(i, 0, 26) {
      s.push_back('a' + i);
      que.push(s);
      s.pop_back();
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  solve();
  return 0;
}