prithivMLmods/Coder-Stat huggingface dataset:
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
using namespace std;

#define LIM 1430400
#define LEN 13

char H[LIM][LEN];

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

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

int h1(long long key) { return key % LIM; }
int h2(long long key) { return 1 + (key % (LIM - 1)); }
long long hashValue(long long key, int i) { return h1(key) + i * h2(key); }

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

int main() {
  int n;
  char com[6], str[12];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>
using namespace std;

#define LIM 48828125
#define LEN 13

char H[LIM][LEN];

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

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

int h1(long long key) { return key % LIM; }
int h2(long long key) { return 1 + (key % (LIM - 1)); }
long long hashValue(long long key, int i) { return h1(key) + i * h2(key); }

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

int main() {
  int n;
  char com[6], str[12];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

typedef int INT;
set<INT> S;

inline INT get(char c) {
  char T[5] = "ACGT";
  for (INT i = 0; i < 4; ++i) {
    if (c == T[i])
      return i + 1;
  }
  return 0;
}

inline INT get(char *s) {
  INT n = strlen(s);
  INT sum = 0;
  INT t = 1;
  for (INT i = 0; i < n; ++i) {
    sum += get(s[i]) * t;
    t *= 4;
  }
  return sum;
}

inline void insert(char *s) { S.insert(get(s)); }

inline bool find(char *s) { return S.count(get(s)) > 0; }

int main() {
  INT n;
  while (scanf("%hd", &n) != EOF) {
    S.clear();
    char cmd[13], data[13];
    for (register INT i = 0; i < n; ++i) {
      scanf("%s%s", cmd, data);
      if (cmd[0] == 'i') {
        insert(data);
      } else {
        puts(find(data) ? "yes" : "no");
      }
    }
  }
  return 0;
}

Fixed submission:

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

typedef int INT;
set<INT> S;

inline INT get(char c) {
  char T[5] = "ACGT";
  for (INT i = 0; i < 4; ++i) {
    if (c == T[i])
      return i + 1;
  }
  return 0;
}

inline INT get(char *s) {
  INT n = strlen(s);
  INT sum = 0;
  INT t = 1;
  for (INT i = 0; i < n; ++i) {
    sum += get(s[i]) * t;
    t *= 4;
  }
  return sum;
}

inline void insert(char *s) { S.insert(get(s)); }

inline bool find(char *s) { return S.count(get(s)) > 0; }

int main() {
  INT n;
  while (scanf("%d", &n) != EOF) {
    S.clear();
    char cmd[13], data[13];
    for (register INT i = 0; i < n; ++i) {
      scanf("%s%s", cmd, data);
      if (cmd[0] == 'i') {
        insert(data);
      } else {
        puts(find(data) ? "yes" : "no");
      }
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  bool dic[1000000] = {0};
  long n = 0;
  long long k = 0;
  string st1, st2;

  cin >> n;

  for (long i = 0; i < n; i++) {
    cin >> st1 >> st2;
    k = 0;

    for (unsigned int j = 0; j < st2.size(); j++)
      switch (st2[j]) {
      case 'A':
        k += (long long)pow(5, j);
        break;
      case 'C':
        k += (long long)pow(5, j) * 2;
        break;
      case 'G':
        k += (long long)pow(5, j) * 3;
        break;
      case 'T':
        k += (long long)pow(5, j) * 4;
        break;
      }

    if (st1 == "insert")
      dic[k] = 1;
    else if (st1 == "find") {
      if (dic[k] == 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  bool dic[10000000] = {0};
  long n = 0;
  long long k = 0;
  string st1, st2;

  cin >> n;

  for (long i = 0; i < n; i++) {
    cin >> st1 >> st2;
    k = 0;

    for (unsigned int j = 0; j < st2.size(); j++)
      switch (st2[j]) {
      case 'A':
        k += (long long)pow(5, j);
        break;
      case 'C':
        k += (long long)pow(5, j) * 2;
        break;
      case 'G':
        k += (long long)pow(5, j) * 3;
        break;
      case 'T':
        k += (long long)pow(5, j) * 4;
        break;
      }

    if (st1 == "insert")
      dic[k] = 1;
    else if (st1 == "find") {
      if (dic[k] == 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
  set<string> st;
  set<string>::iterator it;
  string s1, s2;
  int n;
  cin >> n;

  while (n--) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      st.insert(s2);
    } else {
      it = st.find(s2);
      if (it != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  set<string> st;
  set<string>::iterator it;
  string s1, s2;
  int n;
  cin >> n;

  while (n--) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      st.insert(s2);
    } else {
      it = st.find(s2);
      if (it != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
// #include <conio.h>
using namespace std;

int main() {
  int n;
  set<string> list;
  string key;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> key;
    if (key == "insert") {
      cin >> key;
      list.insert(key);
    } else if (key == "find") {
      cin >> key;
      if (list.find(key) != list.end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
// #include <conio.h>
using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  set<string> list;
  string key;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> key;
    if (key == "insert") {
      cin >> key;
      list.insert(key);
    } else if (key == "find") {
      cin >> key;
      if (list.find(key) != list.end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string.h>

#define M 1046527 // It's a  "prime number".
#define L 14      // Why?

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);
    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 n;
  scanf("%d", &n);

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

  return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <string.h>

#define M 1046527 // It's a  "prime number".
#define L 14      // Why?

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 n;
  scanf("%d", &n);

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

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

int n;

int main() {
  map<string, bool> d;
  string q, s;
  scanf("%d ", &n);
  for (int i = 0; i < n; i++) {
    cin >> q >> s;
    //    if (q == "insert" && !d.count(s))
    if (q == "insert")
      d[s] = true;
    else if (q == "find") {
      //      cout << (d.count(s) ? "yes" : "no") << endl;
      if (d.count(s))
        printf("yes\n");
      else
        printf("no\n");
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, s, e) for ((i) = (s); (i) < (int)(e); (i)++)
#define REP(i, e) FOR(i, 0, e)

#define all(o) (o).begin(), (o).end()
#define psb(x) push_back(x)
#define mp(x, y) make_pair((x), (y))

typedef long long ll;
typedef pair<int, int> PII;

const double EPS = 1e-10;

int n;

int main() {
  map<string, bool> d;
  string q, s;
  scanf("%d ", &n);
  for (int i = 0; i < n; i++) {
    char qq[7], ss[13];
    scanf("%s%s ", qq, ss);
    q = qq;
    s = ss;
    //    cin >> q >> s;
    //    if (q == "insert" && !d.count(s))
    if (q == "insert")
      d[s] = true;
    else if (q == "find") {
      //      cout << (d.count(s) ? "yes" : "no") << endl;
      if (d.count(s))
        printf("yes\n");
      else
        printf("no\n");
    }
  }

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main(void) {
  int n;
  string o, s;
  set<string> set;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> o >> s;
    if (o == "insert") {
      set.insert(s);
    } else {
      if (set.find(s) != set.end()) {
        cout << "yes" << endl;
      } else
        cout << "no" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main(void) {
  ios_base::sync_with_stdio(false);
  int n;
  string o, s;
  set<string> set;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> o >> s;
    if (o == "insert") {
      set.insert(s);
    } else {
      if (set.find(s) != set.end()) {
        cout << "yes" << endl;
      } else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main(void) {
  int n;
  string order, str;
  set<string> set;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> order >> str;
    if (order == "insert") {
      set.insert(str);
    } else {
      if (set.find(str) != set.end()) {
        cout << "yes" << endl;
      } else
        cout << "no" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  int n;
  string order, str;
  set<string> set;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> order >> str;
    if (order == "insert") {
      set.insert(str);
    } else {
      if (set.find(str) != set.end()) {
        cout << "yes" << endl;
      } else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

#define LEN 14
#define LIM 2000000

char H[LIM][LEN];

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

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

int h1(int key) { return key % LIM; }
int h2(int key) { return 1 + key % (LIM - 1); }
int hashValue(int key, int i) { return h1(key) + i * h2(key); }

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

int find(char str[]) {
  int key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    int h = hashValue(key, i);
    if (strcmp(H[h], str) == 0) {
      return 1;
    } else if (strlen(H[h]) == 0) {
      return 0;
    }
  }
  return 0;
}

int main() {
  int n;
  char com[7], str[LEN];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else if (com[0] == 'f') {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

#define LEN 14
#define LIM 2000000

char H[LIM][LEN];

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

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

int h1(int key) { return key % LIM; }
int h2(int key) { return 1 + key % (LIM - 1); }
int hashValue(int key, int i) { return (h1(key) + i * h2(key)) % LIM; }

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

int find(char str[]) {
  int key = getKey(str);
  for (int i = 0; i < LIM; i++) {
    int h = hashValue(key, i);
    if (strcmp(H[h], str) == 0) {
      return 1;
    } else if (strlen(H[h]) == 0) {
      return 0;
    }
  }
  return 0;
}

int main() {
  int n;
  char com[7], str[LEN];
  scanf("%d", &n);

  while (scanf("%s %s", com, str) != EOF) {
    if (com[0] == 'i') {
      insert(str);
    } else if (com[0] == 'f') {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: 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 = 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;
}

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 = 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;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <typeinfo>
#include <vector>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
void trace(int A[], int N);

/*
class Target
{
public:
        vector <string> draw(int n)
        {

        }
};
*/

int main() {
  int n;
  char str[10], com[13];
  map<string, bool> T;

  cin >> n;
  REP(n) {
    cin >> com >> str;
    if (com[0] == 'i') {
      T[string(str)] = true;
    } else {
      if (T[string(str)])
        printf("yes\n");
      else
        printf("no\n");
    }
  }

  return 0;
}

void trace(int A[], int N) {
  REP(N) {
    if (i > 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
}

double distanceAB(double xa, double ya, double xb, double yb) {
  return sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <typeinfo>
#include <vector>
#define PI 3.14159265359
#define INF 99999999;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(n) rep(i, n)
#define EPS 1e-10
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

double distanceAB(double xa, double ya, double xb, double yb);
void trace(int A[], int N);

/*
class Target
{
public:
        vector <string> draw(int n)
        {

        }
};
*/

int main() {
  int n;
  char str[10], com[13];
  map<string, bool> T;

  cin >> n;
  REP(n) {
    scanf("%s%s", com, str);
    if (com[0] == 'i') {
      T[string(str)] = true;
    } else {
      if (T[string(str)])
        printf("yes\n");
      else
        printf("no\n");
    }
  }

  return 0;
}

void trace(int A[], int N) {
  REP(N) {
    if (i > 0)
      cout << " ";
    cout << A[i];
  }
  cout << endl;
}

double distanceAB(double xa, double ya, double xb, double yb) {
  return sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
int n, r[128], t[13];
bool used[22370000];
char com[8], s[14];
int main() {
  scanf("%d", &n);
  r[65] = 1, r[67] = 2, r[71] = 3;
  for (int i = 2; i < 12; i++)
    t[i] = t[i - 1] + 1 << (2 * i - 2);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, s);
    int h = 0, l = strlen(s);
    for (int j = 0; j < l; j++)
      h = h * 4 + r[s[j]];
    if (com[0] == 105)
      used[h + t[l]] = true;
    else
      puts(used[h + t[l]] ? "yes" : "no");
  }
}

Fixed submission:
#include <stdio.h>
#include <string.h>
int n, r[128], t[13];
bool used[22370000];
char com[8], s[14];
int main() {
  scanf("%d", &n);
  r[65] = 1, r[67] = 2, r[71] = 3;
  for (int i = 2; i < 13; i++)
    t[i] = t[i - 1] + (1 << (2 * i - 2));
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, s);
    int h = 0, l = strlen(s);
    for (int j = 0; j < l; j++)
      h = h * 4 + r[s[j]];
    if (com[0] == 105)
      used[h + t[l]] = true;
    else
      puts(used[h + t[l]] ? "yes" : "no");
  }
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define SIZE 1000039
using namespace std;

string hashTable[SIZE];

int hashValue(string str) {
  int res = 0;
  int v = 1;
  for (int i = 0; i < str.length(); i++) {
    res += v * (str[i] - '0');
    v *= 7;
  }
  return res;
}

int main(void) {
  for (int i = 0; i < SIZE; i++)
    hashTable[i] = "";

  int n;
  cin >> n;
  while (n--) {
    string query, str;
    cin >> query >> str;
    if (query == "insert") {
      int hash = hashValue(str);
      int place = hash % SIZE;
      int v = 1;
      while (hashTable[place] != "") {
        place = (hash % SIZE + v * (1 + hash % (SIZE - 1))) % SIZE;
        v++;
      }
      hashTable[place] = str;
    }
    if (query == "find") {
      int hash = hashValue(str);
      int place = hash % SIZE;
      int v = 1;
      bool yes = true;
      while (hashTable[place] != str) {
        if (hashTable[place] == "") {
          yes = false;
          break;
        }
        place = (hash % SIZE + v * (1 + hash % (SIZE - 1))) % SIZE;
        v++;
      }
      if (yes)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
#define SIZE 1000039
using namespace std;

string hashTable[SIZE];

int hashValue(string str) {
  int res = 0;
  int v = 1;
  for (int i = 0; i < str.length(); i++) {
    res += v * (str[i] - '0');
    v *= 7;
  }
  return res;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (int i = 0; i < SIZE; i++)
    hashTable[i] = "";

  int n;
  cin >> n;
  while (n--) {
    string query, str;
    cin >> query >> str;
    if (query == "insert") {
      int hash = hashValue(str);
      int place = hash % SIZE;
      int v = 1;
      while (hashTable[place] != "") {
        place = (hash % SIZE + v * (1 + hash % (SIZE - 1))) % SIZE;
        v++;
      }
      hashTable[place] = str;
    }
    if (query == "find") {
      int hash = hashValue(str);
      int place = hash % SIZE;
      int v = 1;
      bool yes = true;
      while (hashTable[place] != str) {
        if (hashTable[place] == "") {
          yes = false;
          break;
        }
        place = (hash % SIZE + v * (1 + hash % (SIZE - 1))) % SIZE;
        v++;
      }
      if (yes)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string.h>

using namespace std;

const int M = 1000003;
const int L = 12;

char dict[M][L];

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

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

int h(long key, int i) { return (h1(key) + i * h2(key)) % M; }

long strToKey(char str[]) {
  long result = 0;

  int length = strlen(str);
  for (int i = 0; i < length; i++) {
    // Because there is only 4 kind of letters
    result *= 5;
    switch (str[i]) {
    case 'A':
      result += 1;
      break;
    case 'C':
      result += 2;
      break;
    case 'G':
      result += 3;
      break;
    case 'T':
      result += 4;
      break;
    }
  }

  return result;
}

int insert(char str[]) {
  long key = strToKey(str);

  int i = 0;
  int index = h(key, i);
  while (dict[index][0] != '\0') {
    i += 1;
    index = h(key, i);
  }
  strcpy(dict[index], str);
  return 0;
}

int find(char str[]) {
  long key = strToKey(str);

  int i = 0;
  int index = h(key, i);
  while (dict[index][0] != '\0') {
    if (strcmp(dict[index], str) == 0) {
      return index;
    }
  }
  return -1;
}

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

  int n;
  cin >> n;
  char cmd[6];
  char str[L];
  for (int i = 0; i < n; i++) {
    cin >> cmd >> str;
    switch (cmd[0]) {
    case 'i':
      insert(str);
      break;
    case 'f':
      if (find(str) >= 0) {
        cout << "yes\n";
      } else {
        cout << "no\n";
      }
      break;
    }
  }
}


Fixed submission:
#include <iostream>
#include <string.h>

using namespace std;

const int M = 1000003;
const int L = 12;

char dict[M][L];

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

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

int h(long key, int i) { return (h1(key) + i * h2(key)) % M; }

long strToKey(char str[]) {
  long result = 0;

  int length = strlen(str);
  for (int i = 0; i < length; i++) {
    // Because there is only 4 kind of letters
    result *= 5;
    switch (str[i]) {
    case 'A':
      result += 1;
      break;
    case 'C':
      result += 2;
      break;
    case 'G':
      result += 3;
      break;
    case 'T':
      result += 4;
      break;
    }
  }

  return result;
}

int insert(char str[]) {
  long key = strToKey(str);

  int i = 0;
  int index = h(key, i);
  while (dict[index][0] != '\0') {
    i += 1;
    index = h(key, i);
  }
  strcpy(dict[index], str);
  return 0;
}

int find(char str[]) {
  long key = strToKey(str);

  int i = 0;
  int index = h(key, i);
  while (dict[index][0] != '\0') {
    if (strcmp(dict[index], str) == 0) {
      return index;
    } else {
      i += 1;
      index = h(key, i);
    }
  }
  return -1;
}

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

  int n;
  cin >> n;
  char cmd[6];
  char str[L];
  for (int i = 0; i < n; i++) {
    cin >> cmd >> str;
    switch (cmd[0]) {
    case 'i':
      insert(str);
      break;
    case 'f':
      if (find(str) >= 0) {
        cout << "yes\n";
      } else {
        cout << "no\n";
      }
      break;
    }
  }
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// #include "toollib.h"
// #include "puzzle.h"
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#pragma warning(disable : 4018)
#pragma warning(disable : 4244)
#pragma warning(disable : 4996)

using namespace std;
typedef long long int lint;

//***** Main Program *****

vector<bool> dictionary((int)pow(4, 10), false);

int main() {
  int n;
  cin >> n;
  Loop(i, n) {
    char cquery[10], cword[15];
    scanf("%s %s", cquery, cword);
    lint j = 0, k = 1, key = 0;
    while (cword[j] != '\0') {
      if (cword[j] == 'A')
        key += 1 * k;
      else if (cword[j] == 'T')
        key += 2 * k;
      else if (cword[j] == 'G')
        key += 3 * k;
      else
        key += 4 * k;
      k *= 4;
      j++;
    }
    if (cquery[0] == 'i') {
      dictionary[key - 1] = true;
    } else {
      bool judge = dictionary[key - 1];
      if (judge)
        printf("yes\n");
      else
        printf("no\n");
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
// #include "toollib.h"
// #include "puzzle.h"
#define INT_MAX 2147483647
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#pragma warning(disable : 4018)
#pragma warning(disable : 4244)
#pragma warning(disable : 4996)

using namespace std;
typedef long long int lint;

//***** Main Program *****

vector<bool> dictionary((int)pow(4, 12), false);

int main() {
  int n;
  cin >> n;
  Loop(i, n) {
    char cquery[10], cword[15];
    scanf("%s %s", cquery, cword);
    lint j = 0, k = 1, key = 0;
    while (cword[j] != '\0') {
      if (cword[j] == 'A')
        key += 1 * k;
      else if (cword[j] == 'T')
        key += 2 * k;
      else if (cword[j] == 'G')
        key += 3 * k;
      else
        key += 4 * k;
      k *= 4;
      j++;
    }
    if (cquery[0] == 'i') {
      dictionary[key - 1] = true;
    } else {
      bool judge = dictionary[key - 1];
      if (judge)
        printf("yes\n");
      else
        printf("no\n");
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int C = 1046527;

char baca[C][13];

int hs(int key, int i) { return (key % C + i * (1 + key % (C - 1))) % C; }

int cToI(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;
}
int getKey(string tmp) {
  int sum = 0, p = 1, l = tmp.length();
  for (int i = 0; i < l; i++) {
    sum += p * cToI(tmp[i]);
  }
  return sum;
}

void ins(string str) {
  int key = getKey(str);
  for (int i = 0; i < C; i++) {
    int h = hs(key, i);
    if (strlen(baca[h]) == 0) {
      strcpy(baca[h], str.c_str());
      break;
    }
  }
}

int find(string str) {
  int key = getKey(str);
  for (int i = 0;; i++) {
    int h = hs(key, i);
    if (strlen(baca[h]) == 0) {
      return 0;
    } else if (strcmp(baca[h], str.c_str()) == 0) {
      return 1;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  int cnt;
  cin >> cnt;
  string tmp;
  string key;
  for (int i = 0; i < cnt; i++) {
    cin >> tmp >> key;

    if (tmp[0] == 'i') {
      ins(key);
    } else {
      find(key) == 1 ? cout << "yes" << endl : cout << "no" << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int C = 1046527;

char baca[C][13];

int hs(int key, int i) { return (key % C + i * (1 + key % (C - 1))) % C; }

int cToI(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;
}
int getKey(string tmp) {
  int sum = 0, p = 1, l = tmp.length();
  for (int i = 0; i < l; i++) {
    sum += p * cToI(tmp[i]);
    p *= 5;
  }
  return sum;
}

void ins(string str) {
  int key = getKey(str);
  for (int i = 0; i < C; i++) {
    int h = hs(key, i);
    if (strlen(baca[h]) == 0) {
      strcpy(baca[h], str.c_str());
      break;
    }
  }
}

int find(string str) {
  int key = getKey(str);
  for (int i = 0;; i++) {
    int h = hs(key, i);
    if (strlen(baca[h]) == 0) {
      return 0;
    } else if (strcmp(baca[h], str.c_str()) == 0) {
      return 1;
    }
  }
}

int main() {
  ios::sync_with_stdio(false);

  int cnt;
  cin >> cnt;
  string tmp;
  string key;
  for (int i = 0; i < cnt; i++) {
    cin >> tmp >> key;

    if (tmp[0] == 'i') {
      ins(key);
    } else {
      find(key) == 1 ? cout << "yes" << endl : cout << "no" << endl;
    }
  }

  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

#define M 122222
#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;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

#define M 1006000
#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 (status: 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;
}

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 (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define M 1000000
std::vector<std::vector<std::string>> dic(M);
int hash(char ch) {
  if (ch == 'A')
    return 1;
  if (ch == 'C')
    return 2;
  if (ch == 'G')
    return 3;
  if (ch == 'T')
    return 4;
  return 0;
}

long long key(std::string str) {
  long long v = 0;
  int b = 1, n = str.size();
  for (int i = 0; i < n; i++) {
    v += b * (hash(str[i]));
    b *= 5;
  }
  return v;
}

int find(std::string str) {
  std::vector<std::string> s;
  s = dic[key(str)];
  int n = s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == str)
      return 1;
  }
  return 0;
}

void insert(std::string str) { dic[key(str)].push_back(str); }

int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::string com, str;
    std::cin >> com;
    std::cin >> str;
    if (com[0] == 'i')
      insert(str);
    else if (find(str))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define M 1000000
std::vector<std::vector<std::string>> dic(M);
int hash(char ch) {
  if (ch == 'A')
    return 1;
  if (ch == 'C')
    return 2;
  if (ch == 'G')
    return 3;
  if (ch == 'T')
    return 4;
  return 0;
}

long long key(std::string str) {
  long long v = 0;
  int b = 1, n = str.size();
  for (int i = 0; i < n; i++) {
    v += b * (hash(str[i]));
    b *= 5;
  }
  return v % M;
}

int find(std::string str) {
  std::vector<std::string> s;
  s = dic[key(str)];
  int n = s.size();
  for (int i = 0; i < n; i++) {
    if (s[i] == str)
      return 1;
  }
  return 0;
}

void insert(std::string str) { dic[key(str)].push_back(str); }

int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::string com, str;
    std::cin >> com;
    std::cin >> str;
    if (com[0] == 'i')
      insert(str);
    else if (find(str))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: 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互素
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;
}


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互素
// 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;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

int main() {
  std::set<std::string> dic;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::string command, str;
    std::cin >> command >> str;
    if (command == "insert") {
      dic.insert(str);
    } else if (command == "find") {
      std::cout << (dic.find(str) != dic.end() ? "yes" : "no") << std::endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::set<std::string> dic;
  int n;
  std::cin >> n;
  for (int i = 0; i < n; ++i) {
    std::string command, str;
    std::cin >> command >> str;
    if (command == "insert") {
      dic.insert(str);
    } else if (command == "find") {
      std::cout << (dic.find(str) != dic.end() ? "yes" : "no") << std::endl;
    }
  }
  return 0;
}