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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

#define N 2000000

using namespace std;

template <typename T_n> int linearSearch(vector<T_n> &v, T_n key) {
  int n = v.size();
  if (n == 0)
    return -1;
  int i = 0;
  while (v[i] != key) {
    i++;
    if (i == n)
      return -1;
  }
  return i;
}

int binarySearch(vector<int> &v, int key) {
  int left = 0;
  int right = v.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (v[mid] == key)
      return mid;
    else {
      if (key < v[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  return -1;
}

class hashTable {
public:
  int hashSize1;
  int hashSize2;
  int interval;
  vector<int> table;

  hashTable() {
    hashSize1 = 2000000;
    hashSize2 = 1000;
    interval = hashSize1 / hashSize2;
    for (int i = 0; i < N; i++)
      table.push_back(0);
  };

  ~hashTable() { table.clear(); };

  void insert(string str);
  bool find(string str);
  int convertStr(string str);
};

void hashTable::insert(string str) {
  int hash = convertStr(str);
  int num = (hash % hashSize2) * interval;
  while (table[num] != 0)
    num++;
  table[num] = hash;
}

bool hashTable::find(string str) {
  int hash_key = convertStr(str);
  int num = (hash_key % hashSize2) * interval;
  while (num < hashSize1) {
    if (table[num] == hash_key)
      return true;
    if (table[num] == 0)
      return false;
    num++;
  }
  return false;
}

int hashTable::convertStr(string str) {
  int n = str.size();
  int hash = 0, d = 1;
  for (int i = 0; i < n; i++) {
    switch (str[i]) {
    case 'A':
      hash += d;
      break;
    case 'C':
      hash += d * 2;
      break;
    case 'G':
      hash += d * 3;
      break;
    case 'T':
      hash += d * 4;
      break;
    default:
      break;
    }
    d *= 5;
  }
  return hash;
}

int main() {
  int n;
  string str, cmd;
  hashTable dict;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    cin >> str;
    if (cmd == "insert") {
      dict.insert(str);
    }
    if (cmd == "find") {
      // sort(hash_dict.begin(),hash_dict.end());
      cout << (dict.find(str) ? "yes" : "no") << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

template <typename T_n> int linearSearch(vector<T_n> &v, T_n key) {
  int n = v.size();
  if (n == 0)
    return -1;
  int i = 0;
  while (v[i] != key) {
    i++;
    if (i == n)
      return -1;
  }
  return i;
}

int binarySearch(vector<int> &v, int key) {
  int left = 0;
  int right = v.size();
  while (left < right) {
    int mid = (left + right) / 2;
    if (v[mid] == key)
      return mid;
    else {
      if (key < v[mid])
        right = mid;
      else
        left = mid + 1;
    }
  }
  return -1;
}

int convertStr(string str) {
  int n = str.size();
  int hash = 0, d = 1;
  for (int i = 0; i < n; i++) {
    switch (str[i]) {
    case 'A':
      hash += d;
      break;
    case 'C':
      hash += d * 2;
      break;
    case 'G':
      hash += d * 3;
      break;
    case 'T':
      hash += d * 4;
      break;
    default:
      break;
    }
    d *= 5;
  }
  return hash;
}

int main() {
  int n, hash;
  string str, cmd;
  vector<int> hash_dict;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> cmd;
    cin >> str;
    hash = convertStr(str);
    if (cmd == "insert") {
      hash_dict.push_back(hash);
    }
    if (cmd == "find") {
      sort(hash_dict.begin(), hash_dict.end());
      cout << ((binarySearch(hash_dict, hash) != -1) ? "yes" : "no") << endl;
    }
  }
  return 0;
}