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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

struct Trie {
  int val;
  unordered_map<char, Trie *> ch;
  Trie() : val(0) {}
  ~Trie() {
    for (auto &e : ch)
      delete e.second;
  }
};

void insert(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      cur->val++;
      break;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        nxt = new Trie();
      cur = nxt;
      s++;
    }
  }
}

bool erase(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      cur->val--;
      return true;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        return false;
      cur = nxt;
      s++;
    }
  }
}

bool find(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      return cur->val > 0;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        return false;
      cur = nxt;
      s++;
    }
  }
}

int main() {
  int n;
  cin >> n;
  Trie *root = new Trie();
  rep(i, n) {
    char com[15], str[15];
    cin >> com >> str;
    if (com[0] == 'i') {
      insert(str, root);
    } else {
      puts(find(str, root) ? "yes" : "no");
    }
  }
  delete root;
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define int ll
// #define endl "\\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
template <class T> inline bool chmax(T &x, T const &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool chmin(T &x, T const &y) {
  return x > y ? x = y, true : false;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif
// ll const mod = 1000000007;
// ll const inf = 1LL<<60;

struct Trie {
  int val;
  Trie *ch[256];
  // map<char,Trie*> ch;
  Trie() : val(0) { rep(i, 256) ch[i] = nullptr; }
  ~Trie() {
    // for(auto & e : ch) delete e.second;
    rep(i, 256) delete ch[i];
  }
};

void insert(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      cur->val++;
      break;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        nxt = new Trie();
      cur = nxt;
      s++;
    }
  }
}

bool erase(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      cur->val--;
      return true;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        return false;
      cur = nxt;
      s++;
    }
  }
}

bool find(char *s, Trie *t) {
  Trie *cur = t;
  while (1) {
    if (*s == '\\0') {
      return cur->val > 0;
    } else {
      Trie *&nxt = cur->ch[*s];
      if (nxt == nullptr)
        return false;
      cur = nxt;
      s++;
    }
  }
}

int main() {
  int n;
  cin >> n;
  Trie *root = new Trie();
  rep(i, n) {
    char com[15], str[15];
    cin >> com >> str;
    if (com[0] == 'i') {
      insert(str, root);
    } else {
      puts(find(str, root) ? "yes" : "no");
    }
  }
  delete root;
}