Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

const int max_key_length = 12;

class Dictionary {
  std::vector<int> _keys;
  int _char_to_id(char);
  int _to_hash(std::string);

public:
  Dictionary();
  ~Dictionary();
  void insert(std::string);
  bool find(std::string);
};

Dictionary::Dictionary() {
  int max_hashsize = 0;
  int w = 1;
  for (int i = 0; i < max_key_length; i++) {
    max_hashsize += 4 * w;
    w *= 4;
  }
  _keys.resize(max_hashsize);
}

Dictionary::~Dictionary() { ; }

int Dictionary::_char_to_id(char c) {
  switch (c) {
  case 'A':
    return 1;
  case 'C':
    return 2;
  case 'G':
    return 3;
  case 'T':
    return 4;
  }

  // default
  return 0;
}

int Dictionary::_to_hash(std::string word) {
  int val = 0;
  int w = 1;
  for (int i = 0; i < word.length(); i++) {
    val += _char_to_id(word[i]) * w;
    w *= 4;
  }

  return val;
}

void Dictionary::insert(std::string word) { _keys[_to_hash(word)] = true; }

bool Dictionary::find(std::string word) { return _keys[_to_hash(word)]; }

int main(int argc, char const *argv[]) {
  int n;
  std::cin >> n;

  Dictionary dict;
  std::string op;
  std::string word;
  for (int i = 0; i < n; i++) {
    std::cin >> op;
    std::cin >> word;
    if (op[0] == 'i') {
      dict.insert(word);
    } else if (op[0] == 'f') {
      if (dict.find(word))
        std::cout << "yes" << std::endl;
      else
        std::cout << "no" << std::endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>

int binarysearch(std::string str, std::vector<std::string> &array, int left,
                 int right) {
  if (right < left) {
    // std::cout << "Not Found" << std::endl;
    return -1;
  } else {
    int center = left + (right - left) / 2;
    if (array[center] > str)
      return binarysearch(str, array, left, center - 1);
    else if (array[center] < str)
      return binarysearch(str, array, center + 1, right);
    else {
      // std::cout << "Found at " << center << std::endl;
      return center;
    }
  }
}

int main(int argc, char const *argv[]) {
  int n;
  std::cin >> n;
  std::vector<std::string> dic;
  std::string op;
  std::string word;

  for (int i = 0; i < n; i++) {
    std::cin >> op;
    std::cin >> word;
    // std::cout << "operation = " << op << std::endl;
    if (op[0] == 'i') {
      // std::cout << "==== insert ====" << std::endl;
      int loc = 0;

      // ???????????§??????????????????
      for (int j = 0; j < word.length(); j++) {
        if (dic.size() > 0) {
          while (j < dic[loc].length() && word[j] > dic[loc][j]) {
            // std::cout << word[j] << " > " << dic[loc][j] << std::endl;
            loc++;
          }
          // std::cout << "loc = " << loc << std::endl;
        }
      }

      // std::cout << "insert \\"" << word << "\\" in index " << loc << std::endl;
      dic.insert(dic.begin() + loc, word);
    } else if (op[0] == 'f') {
      if (dic.size() > 0) {
        int res = binarysearch(word, dic, 0, dic.size());
        // std::cout << "search result = " << res << std::endl;
        if (res != -1)
          std::cout << "yes" << std::endl;
        else
          std::cout << "no" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    }

    // for (int i = 0; i < dic.size(); i++) {
    //     std::cout << dic[i] << " ";
    // }
    // std::cout << std::endl;
  }

  return 0;
}