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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

#define TABLE_SIZE 16777216
#define ORDER_LENGTH 7
#define STR_LENGTH 13

using namespace std;

int translateChar(char ch) {
  int ret;
  switch (ch) {
  case 'A':
    ret = 1;
    break;
  case 'C':
    ret = 2;
    break;
  case 'G':
    ret = 3;
    break;
  case 'T':
    ret = 4;
    break;
  }
  return ret;
}

int calc(int num) {
  if (num == 0) {
    return 1;
  } else {
    return 4 * calc(num - 1);
  }
}

int translateStr(char buf[]) {
  int sum = 0;
  for (int i = 0; buf[i] != '\\0'; i++) {
    sum += translateChar(buf[i]) * calc(i);
  }
  return sum - 1;
}

int main() {

  queue<string> Messages;
  int n;
  scanf("%d", &n);
  char order[ORDER_LENGTH], str[STR_LENGTH];

  char *check_table = (char *)malloc(TABLE_SIZE);

  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, str);
    if (strcmp(order, "insert") == 0) {
      check_table[translateStr(str)] = 'Y';
    } else {
      if (check_table[translateStr(str)] == 'Y') {
        Messages.push("yes");
      } else {
        Messages.push("no");
      }
    }
  }

  while (!Messages.empty()) {
    cout << Messages.front() << endl;
    Messages.pop();
  }

  free(check_table);
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

#define TABLE_SIZE 1000001
#define ORDER_LENGTH 7
#define STR_LENGTH 13

using namespace std;

int translateChar(char ch) {
  int ret;
  switch (ch) {
  case 'A':
    ret = 1;
    break;
  case 'C':
    ret = 2;
    break;
  case 'G':
    ret = 3;
    break;
  case 'T':
    ret = 4;
    break;
  }
  return ret;
}

int calc(int num) {
  if (num == 0) {
    return 1;
  } else {
    return 4 * calc(num - 1);
  }
}

int translateStr(char buf[]) {
  int sum = 0;
  for (int i = 0; buf[i] != '\\0'; i++) {
    sum += translateChar(buf[i]) * calc(i);
  }
  return sum - 1;
}

int main() {

  queue<string> Messages;
  int *check_table = (int *)calloc(TABLE_SIZE, sizeof(int));
  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    check_table[i] = -1;
  }

  int tmp;
  char order[ORDER_LENGTH], str[STR_LENGTH];
  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, str);
    if (strcmp(order, "insert") == 0) {
      tmp = translateStr(str) % TABLE_SIZE;
      while (true) {
        if (check_table[tmp] == -1) {
          check_table[tmp] = translateStr(str);
          break;
        } else if (check_table[tmp] == translateStr(str)) {
          break;
        } else {
          tmp = (tmp + 1) % TABLE_SIZE;
        }
      }

    } else {
      tmp = translateStr(str) % TABLE_SIZE;
      while (true) {
        if (check_table[tmp] == -1) {
          Messages.push("no");
          break;
        } else if (check_table[tmp] == translateStr(str)) {
          Messages.push("yes");
          break;
        } else {
          tmp = (tmp + 1) % TABLE_SIZE;
        }
      }
    }
  }
  free(check_table);

  while (!Messages.empty()) {
    cout << Messages.front() << endl;
    Messages.pop();
  }
}