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

#include <iostream>
using namespace std;
#include <string.h>
#include <string>

// study of Hash table.

#define M 1020001 // large prime number.
#define L 14      // length of term.

char H[M][L]; // Hash table.

int getnum(char ch) // character to number.
{
  if (ch == 'A') {
    return 1;
  } else if (ch == 'C') {
    return 2;
  } else if (ch == 'G') {
    return 3;
  } else {
    return 4;
  }
  return 0;
}

long long getkey(char word[]) // string to key.
{
  int sum = 0, p = 1, i;
  for (i = 0; word[i]; i++) {
    sum = sum + p * getnum(word[i]);
    p = p * 5;
  }
  return sum;
}

int h1(int k) { return k % M; }
int h2(int k) { return 1 + (k % (M - 1)); }

int insert(char str[]) {
  int key, h, i;
  key = getkey(str);

  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M; // hash.
    if (strcmp(H[h], str)) {
      if (strlen(H[h]) == 0) {
        strcpy(H[h], str);
        return 1;
      }
    } else {
      return 0;
    }
  }
  return 0;
}

int find(char str[]) {
  int key, h, i;
  key = getkey(str);

  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str)) {
      if (strlen(H[h]) == 0) {
        return 0;
      }
      continue;
    } else {
      return 1;
    }
  }
  return 0;
}

int main() {
  char com[9], word[14];
  int NumOfCom;
  cin >> NumOfCom;

  for (int i = 0; i < M; i++)
    H[i][L] = '\\0'; // initialize.

  while (NumOfCom) {
    cin >> com >> word;
    if (com[0] == 'i') {
      insert(word);
    } else {
      if (find(word)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
    NumOfCom--;
  };
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

bool bigger(char *s, char *t) {
  int len1 = 0, len2 = 0, i = 0, d;
  while (s[len1])
    len1++;
  while (t[len2])
    len2++;

  while (i < len1 && i < len2) {
    d = (t[i] - 'A') - (s[i] - 'A');
    if (d > 0) {
      return true;
    } else if (d < 0) {
      return false;
    } else {
      i++;
    }
  };
  if (len1 < len2) {
    return true;
  } else {
    return false;
  }
}

bool is_exist(char D[500000][12], int *ind, int N, char *key) {
  int left, right, mid;
  char *s;
  char *t;

  if (N == 0) {
    ind[0] = 0;
    return false;
  } // not found.

  if (bigger(key, D[ind[0]])) {
    return false;
  }
  if (bigger(D[ind[N - 1]], key)) {
    return false;
  }

  left = 0;
  right = N - 1;
  while (left < right) {
    mid = (left + right) / 2;
    s = D[ind[mid]];
    t = D[ind[mid + 1]];
    if (!bigger(s, key) && !bigger(key, s)) {
      return true;
    } else if (bigger(key, s)) {
      right = mid;
    } else if (bigger(key, t)) {
      return false;
    } else {
      left = mid + 1;
    }
  };

  return true;
}

int ins_posi(char D[500000][12], int *ind, int N) {
  int left, right, mid;
  char *s;
  char *t;
  char *key;

  key = D[N];

  if (N == 0) {
    ind[0] = 0;
    return 0;
  } // not found.

  if (bigger(key, D[ind[0]])) {
    return 0;
  }
  if (bigger(D[ind[N - 1]], key)) {
    return N;
  }

  left = 0;
  right = N - 1;
  while (left < right) {
    mid = (left + right) / 2;
    s = D[ind[mid]];
    t = D[ind[mid + 1]];
    if (s == key) {
      return mid;
    } else if (bigger(key, s)) {
      right = mid;
    } else if (bigger(key, t)) {
      return mid + 1;
    } else {
      left = mid + 1;
    }
  };

  return left + 1;
}

void insert(char D[500000][12], int *ind, int &N, int posi) {
  // insert on posi.
  int j = N;
  while (j > posi) {
    ind[j] = ind[j - 1];
    j--;
  }
  ind[posi] = N;
  N++;
}

int main() {
  int posi, N, NumOfOrder;
  int index[500000];
  char Dict[500000][12];
  static char word[12];
  static char order[6];
  bool yes_no;
  cin >> NumOfOrder;
  N = 0;

  while (NumOfOrder) {
    cin >> order;

    if (order[0] == 'i') {
      cin >> Dict[N];
      posi = ins_posi(Dict, index, N);
      insert(Dict, index, N, posi);
    } else {
      cin >> word;
      yes_no = is_exist(Dict, index, N, word);
      if (yes_no) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
    NumOfOrder--;
  }
  return 0;
}