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

#include <iostream>
#include <string.h>
#define M 1000003
#define L 13
using namespace std;

void insert(char *val);
bool find(char *val);
int h(int, int);
int getNumber(char);

char hashTable[M][L];
bool hashIsUsed[M];
int codes[4][L];

int main() {

  int n;
  char com[7];
  char val[L];
  int p[L];
  p[0] = 1;
  for (int i = 1; i < L; i++) {
    p[i] = p[i - 1] * 4;
  }

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < L; j++) {
      codes[i][j] = i * p[j];
    }
  }

  cin >> n;

  while (n--) {
    cin >> com;
    cin >> val;
    if (com[0] == 'i') {
      insert(val);
    } else if (com[0] == 'f') {
      if (find(val) == true) {
        cout << "yes\\n";
      } else {
        cout << "no\\n";
      }
    }
  }

  return 0;
}

void insert(char *val) {

  int code = 0;
  int p = 1;
  int len;
  int i;

  for (i = 0, len = strlen(val); i < len; i++) {
    code += codes[getNumber(*(val + i))][i];
  }

  int key;
  for (int i = 0;; i++) {
    key = h(code, i);
    if (hashIsUsed[key] != true) {
      memcpy(hashTable[key], val, len);
      hashIsUsed[key] = true;
      break;
    }
  }
}

bool find(char *val) {

  int code = 0;
  int p = 1;
  int len;
  int i;
  for (i = 0, len = strlen(val); i < len; i++) {
    code += codes[getNumber(*(val + i))][i];
  }

  int key;
  for (int i = 0;; i++) {
    key = h(code, i);
    if (hashIsUsed[key] != true) {
      return false;
    } else if (!strcmp(hashTable[key], val)) {
      return true;
    }
  }

  return false;
}

int h(int code, int i) { return ((code % M) + i * (1 + (code % (M - 1)))) % M; }

int getNumber(char val) {

  return (val == 'A')   ? 0
         : (val == 'C') ? 1
         : (val == 'G') ? 2
         : (val == 'T') ? 3
                        : -1;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <string.h>
#define M 1000003
using namespace std;

void insert(char *val);
bool find(char *val);
int h(int, int);
int getNumber(char);
char hashTable[M][13];
bool hashIsUsed[M];
bool myStrcmp(char *str1, char *str2);

int n;

int main() {

  string com;
  char val[13];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> com;
    cin >> val;
    if (com[0] == 'i') {
      insert(val);
    } else if (com[0] == 'f') {
      if (find(val) == true) {
        cout << "yes\\n";
      } else {
        cout << "no\\n";
      }
    }
  }

  return 0;
}

void insert(char *val) {

  int code = 0;
  int p = 1;
  for (int i = 0, len = strlen(val); i < len; i++) {
    code += getNumber(*(val + i)) * p;
    p *= 4;
  }

  int key;
  for (int i = 0;; i++) {
    key = h(code, i);
    if (hashIsUsed[key] != true) {
      // strcpy(hashTable[key],val);
      *(hashTable[key] + 0) = *(val + 0);
      *(hashTable[key] + 1) = *(val + 1);
      *(hashTable[key] + 2) = *(val + 2);
      *(hashTable[key] + 3) = *(val + 3);
      *(hashTable[key] + 4) = *(val + 4);
      *(hashTable[key] + 5) = *(val + 5);
      *(hashTable[key] + 6) = *(val + 6);
      *(hashTable[key] + 7) = *(val + 7);
      *(hashTable[key] + 8) = *(val + 8);
      *(hashTable[key] + 9) = *(val + 9);
      *(hashTable[key] + 10) = *(val + 10);
      *(hashTable[key] + 11) = *(val + 11);
      *(hashTable[key] + 12) = *(val + 12);
      hashIsUsed[key] = true;
      break;
    }
  }
}

bool find(char *val) {

  int code = 0;
  int p = 1;
  for (int i = 0, len = strlen(val); i < len; i++) {
    code += getNumber(*(val + i)) * p;
    p *= 4;
  }

  int key;
  for (int i = 0;; i++) {
    key = h(code, i);
    if (!strcmp(hashTable[key], val)) {
      return true;
    } else if (hashIsUsed[key] != true) {
      return false;
    }
  }

  return false;
}

int h(int code, int i) { return ((code % M) + i * (1 + (code % (M - 2)))) % M; }

int getNumber(char val) {

  if (val == 'A')
    return 0;
  if (val == 'C')
    return 1;
  if (val == 'G')
    return 2;
  if (val == 'T')
    return 3;

  return 9999;
}