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

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

#define CODE 4
#define CODE_LENGTH 12
#define TABLE_SIZE 1046527

int encode(const char *code) {
  int key = 0;
  int len = strlen(code);
  key = len;
  for (int i = 0; i < len; i++) {
    switch (code[i]) {
    case 'A':
      key = key << 2;
      break;
    case 'C':
      key = (key << 2) | 1;
      break;
    case 'G':
      key = (key << 2) | 2;
      break;
    case 'T':
      key = (key << 2) | 3;
      break;
    default:
      printf("Invalid char %c\\n", code[i]);
      return 1;
    }
  }
  return key;
}

typedef struct table {
  char value[CODE_LENGTH];
} HTable;

int h1(const int key, const int m) { return key % m; }

int h2(const int key, const int m) { return 1 + key % (m - 1); }

int get_hash(const int key, const int j, const int m) {
  return (h1(key, m) + j * h2(key, m)) % m;
}

int main(void) {

  int n;
  int size = 0;
  char code[CODE_LENGTH];
  char cmd[7];
  int key;
  int m = TABLE_SIZE;
  int h;

  HTable *tbl = new HTable[m];
  for (int i = 0; i < m; i++) {
    tbl[i].value[0] = '\\0';
  }

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%s", code);
      key = encode(code);
      for (int j = 0;; j++) {
        h = get_hash(key, j, m);
        if (tbl[h].value[0] == '\\0')
          break;
      }
      strcpy(tbl[h].value, code);
    } else if (strcmp(cmd, "find") == 0) {
      scanf("%s", code);
      key = encode(code);
      for (int j = 0;; j++) {
        h = get_hash(key, j, m);
        if (strcmp(tbl[h].value, code) == 0) {
          printf("yes\\n");
          break;
        } else if (tbl[h].value[0] == '\\0') {
          printf("no\\n");
          break;
        }
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;

#define CODE 4
#define CODE_LENGTH 12

int encode(const char *code) {
  int enc = 0;
  int len = strlen(code);
  enc = len;
  for (int i = 0; i < len; i++) {
    switch (code[i]) {
    case 'A':
      enc = enc << 2;
      break;
    case 'C':
      enc = (enc << 2) | 1;
      break;
    case 'G':
      enc = (enc << 2) | 2;
      break;
    case 'T':
      enc = (enc << 2) | 3;
      break;
    default:
      printf("Invalid char %c\\n", code[i]);
      return 1;
    }
  }
  return enc;
}

int binary_search_insert(const int n, int S[], const int key) {
  int start = 0;
  int end = n;
  int mid;
  bool found = false;
  int size = n;

  while (end - start > 0) {
    mid = (start + end) / 2;
    //        printf("s %d e %d mid %d S[mid] %d\\n", start, end, mid, S[mid]);
    if (S[mid] < key) {
      start = mid + 1;
    } else if (S[mid] > key) {
      end = mid;
    } else {
      found = true;
      break;
    }
  }

  if (!found) {
    for (int i = size; i > start; i--) {
      S[i] = S[i - 1];
    }
    S[start] = key;
    //        printf("insert %d at S[%d]\\n", key, start);
    size++;
  }

  return size;
}

bool binary_search(const int n, int S[], const int key) {
  int start = 0;
  int end = n;
  int mid;
  bool res = false;

  while (end - start > 0) {
    mid = (start + end) / 2;
    //        printf("s %d e %d mid %d S[mid] %d\\n", start, end, mid, S[mid]);
    if (S[mid] < key) {
      start = mid + 1;
    } else if (S[mid] > key) {
      end = mid;
    } else {
      res = true;
      break;
    }
  }
  return res;
}

int main(void) {

  int n;
  int size = 0;
  char code[CODE_LENGTH];
  char cmd[7];
  int enc;

  scanf("%d", &n);
  int *buf = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%s", cmd);
    if (strcmp(cmd, "insert") == 0) {
      scanf("%s", code);
      enc = encode(code);
      size = binary_search_insert(size, buf, enc);
    } else if (strcmp(cmd, "find") == 0) {
      scanf("%s", code);
      enc = encode(code);
      if (binary_search(size, buf, enc))
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

  return 0;
}