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

#include <cassert>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Dictionary {
private:
  int size;
  vector<string> table;
  const int P;

public:
  Dictionary(int n);
  int hash(string str);
  void insert(string str);
  bool find(string str);
};

Dictionary::Dictionary(int n) : table(n), P(7916843) { size = n; }

int Dictionary::hash(string str) {
  int S = 0;
  for (int i = 0; i < str.size(); i++) {
    int k = str[i] - 'A';
    S = S * 5 + k;
  }
  S = (S ^ P) % size;
  if (table[S].empty() || table[S] == str) {
    return S;
  } else {
    for (int i = 1;; i++) {
      int t = (S + i) % size;
      if (table[t].empty() || table[t] == str) {
        return t;
      }
    }
  }
}

void Dictionary::insert(string str) {
  int h = this->hash(str);
  if (table[h].empty()) {
    table[h] = str;
    return;
  }
}

bool Dictionary::find(string str) {
  int h = this->hash(str);
  if (table[h].empty())
    return false;
  else {
    assert(table[h] == str);
    return true;
  }
}

int main() {
  int n;
  cin >> n;
  Dictionary dic(1000000);
  for (int i = 0; i < n; i++) {
    string command, str;
    cin >> command >> str;
    // cout << i << endl;
    if (command == "insert")
      dic.insert(str);
    else if (command == "find") {
      if (dic.find(str))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

class DNode {
private:
  string key;
  DNode *left, *right;

public:
  DNode(string str);
  ~DNode();
  DNode *insert(string str);
  bool find(string str);
};

DNode::DNode(string str) {
  key = str;
  left = nullptr;
  right = nullptr;
}

DNode::~DNode() {
  if (left != nullptr)
    delete left;
  if (right != nullptr)
    delete right;
}

DNode *DNode::insert(string str) {
  if (str == key)
    return this;
  else if (str < key) {
    if (left == nullptr) {
      left = new DNode(str);
      return left;
    } else
      return left->insert(str);
  } else {
    if (right == nullptr) {
      right = new DNode(str);
      return right;
    } else
      return right->insert(str);
  }
}

bool DNode::find(string str) {
  if (str == key)
    return true;
  else if (str < key) {
    if (left == nullptr)
      return false;
    else
      return left->find(str);
  } else {
    if (right == nullptr)
      return false;
    else
      return right->find(str);
  }
}

class Dictionary {
private:
  DNode *root;
  const string central;
  bool ct_flag;

public:
  Dictionary(string ct);
  ~Dictionary();
  DNode *insert(string str);
  bool find(string str);
};

Dictionary::Dictionary(string ct) : central(ct) {
  root = new DNode(central);
  ct_flag = false;
}

Dictionary::~Dictionary() { delete root; }

DNode *Dictionary::insert(string str) {
  if (str == central) {
    ct_flag = true;
    return root;
  } else
    return root->insert(str);
}

bool Dictionary::find(string str) {
  if (str == central)
    return ct_flag;
  else
    return root->find(str);
}

int main() {
  int n;
  cin >> n;
  Dictionary dic("CTTT");
  for (int i = 0; i < n; i++) {
    string command, str;
    cin >> command >> str;
    if (command == "insert")
      dic.insert(str);
    else if (command == "find") {
      if (dic.find(str))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}