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

#include <cmath>
#include <iostream>
#include <string.h>

#define MAX_NUM 1000001
#define MAX_CHAR 12
#define DEPTH 7

char data[MAX_NUM][MAX_CHAR] = {};
int p[DEPTH];
int separate = pow(5, DEPTH);

void setPow() {
  for (int i = 0; i < DEPTH; i++) {
    p[i] = pow(5, i);
  }
}

void printData() {
  for (int i = 1; i < MAX_NUM; i++) {
    if (data[i][0] != '\\0') {
      std::cout << "i=" << i << ':' << data[i] << '\\n';
    }
  }
}

int hash(char str[]) {
  int count = 0;
  for (int i = 0; i < DEPTH; i++) {
    char tmp = str[i];
    if (tmp == 'A') {
      count += p[i] * 1;
    } else if (tmp == 'C') {
      count += p[i] * 2;
    } else if (tmp == 'G') {
      count += p[i] * 3;
    } else if (tmp == 'T') {
      count += p[i] * 4;
    } else if (tmp == '\\0') {
      return count;
    } else {
      std::cout << "errar: 指定の文字で始まっていません\\n";
      return 0;
    }
  }
  return count;
}

void insert(char str[]) {
  int h = hash(str);
  if (h == 0) {
    return;
  }
  int end = h;

  do {
    if (data[h][0] == '\\0') {
      strcpy(data[h], str);
      return;
    } else {
      h += separate;
      if (h >= 1000000) {
        h = (h + 1) % separate;
      }
    }
  } while (h != end);
}

void find(char str[]) {
  int h = hash(str);
  if (h == 0) {
    return;
  };
  int end = h;
  do {
    if (strcmp(data[h], str) == 0) {
      std::cout << "yes\\n";
      return;
    } else if (data[h][0] == '\\0') {
      std::cout << "no\\n";
      return;
    } else {
      h += separate;
      if (h >= 1000000) {
        h = (h + 1) % separate;
      }
    }
  } while (h != end);
}

void Command(char com[]) {
  if (strcmp(com, "insert") == 0) {
    char str[MAX_CHAR];
    std::cin >> str;
    insert(str);
  } else if (strcmp(com, "find") == 0) {
    char str[MAX_CHAR];
    std::cin >> str;
    find(str);
  } else {
    std::cout << "errar : 定義されていないコマンドです\\n";
  }
}

int main() {
  setPow();
  int n;
  char com[10];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    // std::cout << "i=" << i << '\\n';
    std::cin >> com;
    Command(com);
  }
  // printData();

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string.h>

#define MAX_NUM 1000001
#define MAX_CHAR 12
#define DEPTH 7

char data[MAX_NUM][MAX_CHAR] = {};
int separate = pow(5, DEPTH);

void printData() {
  for (int i = 1; i < MAX_NUM; i++) {
    if (data[i][0] != '\\0') {
      std::cout << "i=" << i << ':' << data[i] << '\\n';
    }
  }
}

int hash(char str[]) {
  int count = 0;
  for (int i = 0; i < DEPTH; i++) {
    int p = pow(5, i);
    char tmp = str[i];
    if (tmp == 'A') {
      count += p * 1;
    } else if (tmp == 'C') {
      count += p * 2;
    } else if (tmp == 'G') {
      count += p * 3;
    } else if (tmp == 'T') {
      count += p * 4;
    } else if (tmp == '\\0') {
      return count;
    } else {
      std::cout << "errar: 指定の文字で始まっていません\\n";
      return 0;
    }
  }
  return count;
}

void insert(char str[]) {
  int h = hash(str);
  if (h == 0) {
    return;
  }
  int end = h;

  do {
    if (data[h][0] == '\\0') {
      strcpy(data[h], str);
      return;
    } else {
      h += separate;
      if (h >= 1000000) {
        h = (h + 1) % separate;
      }
    }
  } while (h != end);
}

void find(char str[]) {
  int h = hash(str);
  if (h == 0) {
    return;
  };
  int end = h;
  do {
    if (strcmp(data[h], str) == 0) {
      std::cout << "yes\\n";
      return;
    } else if (data[h][0] == '\\0') {
      std::cout << "no\\n";
      return;
    } else {
      h += separate;
      if (h >= 1000000) {
        h = (h + 1) % separate;
      }
    }
  } while (h != end);
}

void Command(char com[]) {
  if (strcmp(com, "insert") == 0) {
    char str[MAX_CHAR];
    std::cin >> str;
    insert(str);
  } else if (strcmp(com, "find") == 0) {
    char str[MAX_CHAR];
    std::cin >> str;
    find(str);
  } else {
    std::cout << "errar : 定義されていないコマンドです\\n";
  }
}

int main() {
  int n;
  char com[10];
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    // std::cout << "i=" << i << '\\n';
    std::cin >> com;
    Command(com);
  }
  // printData();

  return 0;
}