alexjercan/bugnet huggingface dataset:
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
//alds1_4_c:Dictionary
//算法:开放地址法散列表
//Time: 2018/1/8 星期一
*/
#include <stdio.h>
#include <string.h>

const int M = 1000003;
const int L = 14;

char H[M][L];

// 对于每个字符返回的定义值
int getChar(char ch) {
  if (ch == 'A')
    return 1;
  if (ch == 'C')
    return 2;
  if (ch == 'D')
    return 3;
  if (ch == 'T')
    return 4;
  return 0;
}
// 对于字符串返回的初始散列值
long long getKey(char str[]) {
  long long len = strlen(str), sum = 0, p = 1;
  for (int i = 0; i < len; ++i) {
    sum += p * getChar(str[i]);
    // 每次获取定义值后p*5,相当于转换成五进制,不会冲突
    p *= 5;
  }
  return sum;
}

// 开放式散列值计算式: h(k,i)=(h1(k)+i*h2(k))%M
int h1(int key) { return key % M; }
// 为了保证不会递归冲突(即往下算结果始终相同),必须使h2(key)与M互素
// TLE最好的情况就是改这个函数= =
// 目前可以AC的: 1+(key%(M-1))
//(1+key)%(M-1)
int h2(int key) { return (1 + key) % (M - 1); }

// 查找
//-1表示找到
// h表示找到第一个可插入点
int find(char str[]) {
  long long key = getKey(str), i, h;
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return -1;
    else if (strlen(H[h]) == 0)
      return h;
  }
  return 0;
}

// 插入
void insert(char str[]) {
  int key = find(str);
  if (key != -1)
    strcpy(H[key], str);
}

int main() {
  for (int i = 0; i < M; ++i)
    H[M][0] = '\\0';
  char str[L], com[L];
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str) == -1)
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

/*
//alds1_4_c:Dictionary
//算法:开放地址法散列表
//Time: 2018/1/8 星期一
*/
#include <stdio.h>
#include <string.h>

const int M = 1000003;
const int L = 14;

char H[M][L];

// 对于每个字符返回的定义值
int getChar(char ch) {
  if (ch == 'A')
    return 1;
  if (ch == 'C')
    return 2;
  if (ch == 'D')
    return 3;
  if (ch == 'T')
    return 4;
  return 0;
}
// 对于字符串返回的初始散列值
long long getKey(char str[]) {
  long long len = strlen(str), sum = 0, p = 1;
  for (int i = 0; i < len; ++i) {
    sum += p * getChar(str[i]);
    // 每次获取定义值后p*5,相当于转换成五进制,不会冲突
    p *= 5;
  }
  return sum;
}

// 开放式散列值计算式: h(k,i)=(h1(k)+i*h2(k))%M
int h1(int key) { return key % M; }
// 为了保证不会递归冲突(即往下算结果始终相同),必须使h2(key)与M互素
int h2(int key) { return (key % (M - 1)); }

// 查找
//-1表示找到
// h表示找到第一个可插入点
int find(char str[]) {
  long long key = getKey(str), i, h;
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return -1;
    else if (strlen(H[h]) == 0)
      return h;
  }
  return 0;
}

// 插入
void insert(char str[]) {
  int key = find(str);
  if (key != -1)
    strcpy(H[key], str);
}

int main() {
  for (int i = 0; i < M; ++i)
    H[M][0] = '\\0';
  char str[L], com[L];
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str) == -1)
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

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

int h2(int key) { return 1 + key % (M - 1); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>
#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p * 5;
  }
  return sum;
}

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

int h2(int key) { return 1 + key % (M - 1); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define M 1046527
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[1]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M; }
int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M 104627
#define NIL (-1)
#define L 14

char H[M][L];

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[1]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M; }
int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <stdio.h>
#include <string.h>

#define M 2000000
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M; }
int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {

  long long key, i, h;
  key = getKey(str);
  for (int i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <stdio.h>
#include <string.h>

#define M 20000000
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
}

/* convert a string into an integer value */
long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M; }
int h2(int key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, i, h;
  key = getKey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
  return 0;
}

int insert(char str[]) {

  long long key, i, h;
  key = getKey(str);
  for (int i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const unsigned long M = 1046527;
const int L = 14;

char H[M][L];

long long getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[]) {
  long long sum = 0, p = 1;
  for (int i = 0; i < strlen(str); ++i) {
    sum += p * getChar(str[i]);
    p *= 5;
  }
  return sum;
}

int h1(long long key) { return key % M; }
int h2(long long key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, h;
  key = getKey(str);
  for (long long i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
}

int insert(char str[]) {
  long long key, h;
  key = getKey(str);
  for (long long i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
}

int main() {
  for (int i = 0; i < M; ++i)
    H[i][0] = '\\0';

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    char cmd[L];
    char wrd[L];
    scanf("%s %s", cmd, wrd);

    if (cmd[0] == 'i') {
      insert(wrd);
    } else {
      cout << (find(wrd) ? "yes" : "no") << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const unsigned long M = 1046527;
const int L = 14;

char H[M][L];

long long getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

long long getKey(char str[]) {
  long long sum = 0, p = 1;
  for (int i = 0; i < strlen(str); ++i) {
    sum += p * getChar(str[i]);
    p *= 5;
  }
  return sum;
}

int h1(long long key) { return key % M; }
int h2(long long key) { return 1 + (key % (M - 1)); }

int find(char str[]) {
  long long key, h;
  key = getKey(str);
  for (long long i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0)
      return 0;
  }
}

int insert(char str[]) {
  long long key, h;
  key = getKey(str);
  for (long long i = 0;; ++i) {
    h = (h1(key) + i * h2(key));
    if (strcmp(H[h], str) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], str);
      return 0;
    }
  }
}

int main() {
  for (int i = 0; i < M; ++i)
    H[i][0] = '\\0';

  int n;
  scanf("%d", &n);

  for (int i = 0; i < n; ++i) {
    char cmd[L];
    char wrd[L];
    scanf("%s %s", cmd, wrd);

    if (cmd[0] == 'i') {
      insert(wrd);
    } else {
      cout << (find(wrd) ? "yes" : "no") << endl;
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

/*
filename:4_C-Dictionary.c
created on 17.9.2015 by charis
last edited on 17.9.2015 by charis
*/

#include <cstdio>
#include <cstring>

int m;

int getkey(char str[]) {
  int sum = 0, p = 1;
  for (int k = 0; k < strlen(str); k++) {
    if (str[k] == 'A')
      sum += p * 1;
    else if (str[k] == 'C')
      sum += p * 2;
    else if (str[k] == 'G')
      sum += p * 3;
    else if (str[k] == 'T')
      sum += p * 4;
    p *= 5;
  }
  return sum;
}
int h1(int key) { return key % m; }
int h2(int key) { return 1 + (key % (m - 1)); }
int h(int key, int i) { return (h1(key) + i * h2(key)) % m; }
void insert(int T[], int key) {
  int i = 0;
  while (1) {
    int j = h(key, i);
    if (T[j] == 0) {
      T[j] = key;
      break;
    } else
      i++;
  }
}
bool search(int T[], int key) {
  int i = 0;
  while (1) {
    int j = h(key, i);
    if (T[j] == key)
      return true;
    else if (T[j] == 0 || i >= m)
      return false;
    else
      i++;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  m = 1046527;
  int T[m];
  for (int k = 0; k < m; k++) {
    T[k] = 0;
  }

  for (int k = 0; k < n; k++) {
    char com[9], str[13];
    scanf("%s %s", com, str);
    int key = getkey(str);
    if (com[0] == 'i')
      insert(T, key);
    else if (com[0] == 'f') {
      if (search(T, key))
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

/*
filename:4_C-Dictionary.c
created on 17.9.2015 by charis
last edited on 17.9.2015 by charis
*/

#include <cstdio>
#include <cstring>

int m;

int getkey(char str[]) {
  int sum = 0, p = 1;
  for (int k = 0; k < strlen(str); k++) {
    if (str[k] == 'A')
      sum += p * 1;
    else if (str[k] == 'C')
      sum += p * 2;
    else if (str[k] == 'G')
      sum += p * 3;
    else if (str[k] == 'T')
      sum += p * 4;
    p *= 5;
  }
  return sum;
}
int h1(int key) { return key % m; }
int h2(int key) { return 1 + (key % (m - 1)); }
int h(int key, int i) { return (h1(key) + i * h2(key)) % m; }
void insert(int T[], int key) {
  int i = 0;
  while (1) {
    int j = h(key, i);
    if (T[j] == 0) {
      T[j] = key;
      break;
    } else
      i++;
  }
}
bool search(int T[], int key) {
  int i = 0;
  while (1) {
    int j = h(key, i);
    if (T[j] == key)
      return true;
    else if (T[j] == 0 || i >= m)
      return false;
    else
      i++;
  }
}

int main() {
  int n;
  scanf("%d", &n);
  m = 1000000;
  int T[m];
  for (int k = 0; k < m; k++) {
    T[k] = 0;
  }

  for (int k = 0; k < n; k++) {
    char com[9], str[13];
    scanf("%s %s", com, str);
    int key = getkey(str);
    if (com[0] == 'i')
      insert(T, key);
    else if (com[0] == 'f') {
      if (search(T, key))
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <string>

class Dictionary {

public:
  void insert(std::string word) { strList.insert(word); }

  void find(std::string word) {
    auto elemItr = strList.find(word);
    if (elemItr == strList.cend()) {
      std::cout << "no";
    } else {
      std::cout << "yes";
    }
    std::cout << std::endl;
  }

private:
  std::set<std::string> strList;
};

void useDictionary(Dictionary &dictionary, std::string command,
                   std::string word) {
  if (command == "insert") {
    dictionary.insert(word);
  } else if (command == "find") {
    dictionary.find(word);
  }
}

int main() {

  std::size_t n;
  std::cin >> n;

  Dictionary dictionary;
  while (n--) {
    std::string command, word;
    std::cin >> command >> word;
    useDictionary(dictionary, command, word);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>

class Dictionary {

public:
  void insert(std::string word) { strList.insert(word); }

  void find(std::string word) {
    auto elemItr = std::find(strList.cbegin(), strList.cend(), word);
    if (elemItr == strList.cend()) {
      std::cout << "no";
    } else {
      std::cout << "yes";
    }
    std::cout << std::endl;
  }

private:
  std::set<std::string> strList;
};

void useDictionary(Dictionary &dictionary, std::string command,
                   std::string word) {
  if (command == "insert") {
    dictionary.insert(word);
  } else if (command == "find") {
    dictionary.find(word);
  }
}

int main() {

  std::size_t n;
  std::cin >> n;

  Dictionary dictionary;
  while (n--) {
    std::string command, word;
    std::cin >> command >> word;
    useDictionary(dictionary, command, word);
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <fstream>
#include <iostream>
#include <string.h>
#include <string>
using namespace std;

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 0;
  return 3;
}

long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < ::strlen(str); i++) {
    sum *= 4;
    sum += getChar(str[i]);
  }
  return sum;
}
std::bitset<16 * 1024 * 1024> flg;

int main() {
  int n;
  std::istream &c_in = cin;
  string s;
  getline(c_in, s);
  sscanf(s.c_str(), "%d", &n);

  for (int i = 0; i < n; ++i) {
    getline(c_in, s);

    int value;
    if (s[0] == 'i') { //"insert x"
      sscanf(&(s[6]), "%d", &value);
      flg.set(::getKey(&(s[6])));
    } else //{						//find x
      if (flg.test(::getKey(&(s[4]))))
        printf("yes\\n");
      else
        printf("no\\n");
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

#define M (4700000 + 1) /* your task*/
#define NIL (-1)
#define L 14

char H[M][L]; /* Hash Table */

int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  return 0;
}

/* convert a string into an integer value */
long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int h1(int key) { return key % M /* your task */; }
int h2(int key) { return key % 13 /* your task */; }

int find(char str[]) {
  /* your task */
  long long k = ::getKey(str);
  int _h1 = h1((int)k * 17);
  int _h2 = h2((int)k);
  for (int i = 0, j = _h1; i < M; ++i, j = (j + _h2) % M) {
    if (::H[j][0] == '\\0')
      return 0;
    if (strcmp(::H[j], str) == 0) {
      return 1;
    }
  }
  return 0;
}

int insert(char str[]) {

  /* your task */
  long long k = ::getKey(str);
  int _h1 = h1((int)k * 17);
  int _h2 = h2((int)k);
  for (int i = 0, j = _h1; i < M; ++i, j = (j + _h2) % M) {
    if (::H[j][0] == '\\0') {
      ::strcpy(H[j], str);
      return j;
    }
    if (strcmp(::H[j], str) == 0) {
      return j;
    }
  }
  return 0;
}

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';

  scanf("%d", &n);

  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;

int main(void) {
  int n;
  string word;
  typedef unordered_set<string> set_t;
  set_t A;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> word;
    if (word == "find") {
      string S;
      cin >> S;
      if (A.count(S)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else {
      string S;
      cin >> S;
      A.insert(S);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:


#include <iostream>

#include <set>
#include <string>
using namespace std;

int main(void) {
  int n;
  string word;
  typedef set<string> set_t;
  set_t A;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> word;
    if (word == "find") {
      string S;
      cin >> S;
      if (A.count(S)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    } else {
      string S;
      cin >> S;
      A.insert(S);
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  set<string> st;
  int n;
  cin >> n;
  while (n--) {
    string BUFF, c;
    cin >> BUFF >> c;
    if (BUFF == "insert")
      st.insert(c);
    else
      cout << (st.find(c) != st.end() ? "yes" : "no") << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  set<string> st;
  int n;
  cin >> n;
  while (n--) {
    string BUFF, c;
    cin >> BUFF >> c;
    if (BUFF == "insert")
      st.insert(c);
    else
      cout << (st.find(c) != st.end() ? "yes" : "no") << endl;
  }
}