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

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

static const int M = 1046527; //?´???°????????¶
static const int Q = 1901;
static const int MAX_OP_LEN = 7;   //?????????????????????
static const int MAX_STR_LEN = 13; //?????????????????????

char H[M][MAX_STR_LEN];
int c_collision = 0;

int convert_str(char *str);
int h(int key);
int g(int key);
void linerProving_insert(int num, char *str);  //?????¢??°??????
int linerProving_find(int num, char *str);     //?????¢??°??????
void doubleHashing_insert(int num, char *str); //?????????????????\\???
int doubleHashing_find(int num, char *str);    //?????????????????\\???

int main() {
  int n, num;
  char op[MAX_OP_LEN], word[MAX_STR_LEN];

  //???????????\\????????????????????????
  memset(H, '\\0', sizeof(H));

  cin >> n;
  while (n > 0) {
    scanf("%s%s", op, word);

    num = convert_str(word);

    if (op[0] == 'i') {
      doubleHashing_insert(num, word);
      //			cout << "collision = " << c_collision << endl;
    } else {
      if (doubleHashing_find(num, word))
        printf("yes\\n");
      else
        printf("no\\n");
    }
    n--;
  }

  //	cout << "collision = " << c_collision << endl;
  return 0;
}

int convert_str(char *str) {
  int num, p;
  //??????????????°????????????
  num = 0;
  p = 1;
  for (int i = 0; str[i] != '\\0' && i < MAX_STR_LEN; i++) {
    switch (str[i]) {
    case 'A':
      num += 1 * p;
      break;
    case 'C':
      num += 2 * p;
      break;
    case 'G':
      num += 3 * p;
      break;
    case 'T':
      num += 4 * p;
      break;
    }
    p *= 5;
  }
  return num;
}

int h(int key) { return key % M; }

int g(int key) { return Q - (h(key) % Q) + 1; }

//?????????????????\\???
void doubleHashing_insert(int num, char *str) {
  int idx, j;

  for (j = 0; j < M; j++) {
    idx = (h(num) + j * g(num)) % M;

    //?????§?????????????????????????????°???????????????????????????
    if (strncmp(str, H[idx], MAX_STR_LEN) == 0)
      return;
    else if (strlen(H[idx]) == 0)
      break;
    //		c_collision++;
  }
  strcpy(H[idx], str);
}

//?????????????????\\???
int doubleHashing_find(int num, char *str) {
  int idx, j;

  for (j = 0; j < M; j++) {
    idx = (h(num) + j * g(num)) % M;

    // printf("idx = %d\\n", idx);
    // cout << str << endl;
    // cout << H[idx] << endl;
    if (strncmp(str, H[idx], MAX_STR_LEN) == 0)
      return 1;
    else if (strlen(H[idx]) == 0)
      return 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

// static const int M = 1000000;
static const int M = 1046527; //?´???°????????¶
// static const int M = 83;  //?´???°????????¶
static const int Q = 7;
static const int MAX_OP_LEN = 7;   //?????????????????????
static const int MAX_STR_LEN = 13; //?????????????????????

// string dic[M] = {""};
char H[M][MAX_STR_LEN];
int c_collision = 0;

int convert_str(char *str);
int h(int key);
int g(int key);
void linerProving_insert(int num, char *str);  //?????¢??°??????
int linerProving_find(int num, char *str);     //?????¢??°??????
void doubleHashing_insert(int num, char *str); //?????????????????\\???
int doubleHashing_find(int num, char *str);    //?????????????????\\???

int main() {
  int n, ans, num;
  char op[MAX_OP_LEN], word[MAX_STR_LEN];

  //???????????\\????????????????????????
  memset(H, '\\0', sizeof(H));

  cin >> n;
  while (n > 0) {
    scanf("%s%s", op, word);
    //		cout << op << ' ' << word << endl; break;

    // printf("%s\\n", op);
    // printf("%s\\n", word);

    num = convert_str(word);

    if (strncmp(op, "insert", MAX_OP_LEN) == 0) {
      //			cout << "insert : " << word << " : " << num <<
      //endl;
      doubleHashing_insert(num, word);
    } else {
      //			cout << "find : " << word << " : " << num <<
      //endl;
      if (doubleHashing_find(num, word))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
    n--;
  }

  //	cout << "collision = " << c_collision << endl;
  return 0;
}

int convert_str(char *str) {
  int num;
  //??????????????°????????????
  num = 0;
  for (int i = 0; str[i] != '\\0' && i < MAX_STR_LEN; i++) {
    //		printf("str[%d] = %c\\n", i, str[i]);
    switch (str[i]) {
    case 'A':
      num += 1 * 4 ^ i;
      break;
    case 'C':
      num += 2 * 4 ^ i;
      break;
    case 'G':
      num += 3 * 4 ^ i;
      break;
    case 'T':
      num += 4 * 4 ^ i;
      break;
    }
  }
  return num;
}

int h(int key) { return key % M; }

int g(int key) { return Q - (h(key) % Q); }

//?????????????????\\???
void doubleHashing_insert(int num, char *str) {
  int idx, j;

  for (j = 0; j < M; j++) {
    idx = (h(num) + j * g(num)) % M;

    //?????§?????????????????????????????°???????????????????????????
    if (strncmp(str, H[idx], MAX_STR_LEN) == 0)
      return;
    else if (strlen(H[idx]) == 0)
      break;
    //		c_collision++;
  }
  strcpy(H[idx], str);
}

//?????????????????\\???
int doubleHashing_find(int num, char *str) {
  int idx, j;

  for (j = 0; j < M; j++) {
    idx = (h(num) + j * g(num)) % M;

    // printf("idx = %d\\n", idx);
    // cout << str << endl;
    // cout << H[idx] << endl;
    if (strncmp(str, H[idx], MAX_STR_LEN) == 0)
      return 1;
    else if (strlen(H[idx]) == 0)
      return 0;
  }
  return 0;
}