alexjercan/bugnet huggingface dataset:
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

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

#define M 1000000
#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 (key % M - 1) + 1; }
// Hash function
int hash(int key, int i) { return (h1(key) + i * h2(key)) % M; }

int find(char str[]) {
  long long i = 0;
  long long key = getKey(str);
  while (true) {
    long long j = hash(key, i);
    if (strcmp(H[j], str) == 0)
      return 1;
    else if (strlen(H[j]) == 0)
      return 0;
    i++;
  }
}

int insert(char str[]) {
  long long i = 0;
  long long key = getKey(str);
  while (true) {
    long long j = hash(key, i);
    if (H[j][0] == '\\0') {
      strcpy(H[j], str);
      return 1;
    } else {
      i++;
    }
  }
}

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: 0

Fixed submission:

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

#define M 1000000
#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 (key % M - 1) + 1; }
// Hash function
int hash(int key, int i) { return (h1(key) + i * h2(key)); }

int find(char str[]) {
  long long i = 0;
  long long key = getKey(str);
  while (true) {
    long long j = hash(key, i);
    if (strcmp(H[j], str) == 0)
      return 1;
    else if (strlen(H[j]) == 0)
      return 0;
    i++;
  }
}

int insert(char str[]) {
  long long i = 0;
  long long key = getKey(str);
  while (true) {
    long long j = hash(key, i);
    if (H[j][0] == '\\0') {
      strcpy(H[j], str);
      return 1;
    } else {
      i++;
    }
  }
}

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 <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  unordered_set<string> histo;
  while (N--) {
    string s, t;
    cin >> s >> t;
    if (s == "insert") {
      histo.insert(t);
    } else {
      cout << (histo.count(t) ? "yes" : "no") << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  set<string> histo;
  while (N--) {
    string s, t;
    cin >> s >> t;
    if (s == "insert") {
      histo.insert(t);
    } else {
      cout << (histo.count(t) ? "yes" : "no") << endl;
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define M 1000000
#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 (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 10000000
#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 (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 <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int main() {
  int n;
  map<string, int> dic;
  cin >> n;
  char inp[8];
  string name;
  while (n--) {
    scanf("%s", inp);
    cin >> name;
    if (inp[0] == 'i') {
      dic[name] = 1;
    } else if (inp[0] == 'f') {
      if (dic.find(name) != dic.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int main() {
  int n;
  map<string, int> dic;
  cin >> n;
  while (n--) {
    char inp[8];
    string name;
    scanf("%s", inp);
    if (inp[0] == 'i') {
      cin >> name;
      dic[name] = 1;
    } else if (inp[0] == 'f') {
      cin >> name;
      if (dic.find(name) != dic.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#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 *= 6;
  }
  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;
    }
  }
}
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: 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 *= 10;
  }
  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;
    }
  }
}
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 <iostream>
#include <string>
using namespace std;

#define M 1046527
#define NIL -1
#define L 14

string H[M];

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(string str) {
  long long sum = 0, p = 1;
  for (int i = 0; i < str.size(); 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(string str) {
  long long key, h;
  key = getKey(str);
  for (int i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (H[h] == str)
      return 1;
    else if (H[h].size() == 0)
      return 0;
  }
  return 0;
}

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

int main(void) {
  int n, h;
  string str, com;
  cin >> n;

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

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool linearSearch(vector<string> v, string key) {
  int i = 0;
  while (v[i] != key)
    i++;
  if (i == v.size() - 1)
    return false;
  else
    return true;
}

int main(void) {
  vector<string> v;
  int n;
  string com, str;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> com >> str;
    if (com == "insert") {
      v.push_back(str);
    } else if (com == "find") {
      v.push_back(str);
      if (linearSearch(v, str))
        cout << "yes" << endl;
      else
        cout << "no" << endl;
      v.erase(v.begin() + v.size() - 1);
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

#define MAX 16777216

using namespace std;

bool dic[16777216];

int getStrNum(string str) {
  int sum = 0, p = 1;
  for (int i = 0; i < str.size(); ++i) {
    if (str[i] == 'A')
      sum += 1 * p;
    else if (str[i] == 'C')
      sum += 2 * p;
    else if (str[i] == 'G')
      sum += 3 * p;
    else if (str[i] == 'T')
      sum += 4 * p;
    p *= 4;
  }
  return sum;
}

int main() {
  int n, i;
  cin >> n;

  string siji, _str;
  for (i = 0; i < n; ++i) {
    cin >> siji >> _str;
    if (siji[0] == 'i') {
      dic[getStrNum(_str)] = true;
    } else {
      if (dic[getStrNum(_str)])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, i, j;
  string siji, strIn, ansIn;
  vector<string> str;
  vector<bool> ans;

  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> siji;
    cin >> strIn;
    if (siji[0] == 'i') {
      str.push_back(strIn);
    } else {
      if (str.size() == 0)
        ans.push_back(false);
      for (j = 0; j < str.size(); ++j) {
        if (str[j] == strIn) {
          ans.push_back(true);
          break;
        }
        if (j == str.size() - 1)
          ans.push_back(false);
      }
    }
  }

  for (i = 0; i < ans.size(); ++i) {
    if (ans[i])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  map<string, bool> S;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      string t;
      cin >> t;
      S[t] = true;
      continue;
    }
    if (s == "find") {
      string t;
      cin >> t;
      // auto result = find(S.begin(), S.end(), t);

      // if (result != S.end())
      if (S[t] == true) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<string> S;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    if (s == "insert") {
      string t;
      cin >> t;
      S.push_back(t);
      continue;
    }
    if (s == "find") {
      string t;
      cin >> t;
      auto result = find(S.begin(), S.end(), t);
      if (result != S.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAX_S = 1 << 16;
int C[128];

bool isExist[9][MAX_S];

int convert(string s) {
  int j = 0;
  for (int i = 0; i < s.size(); ++i) {
    j |= C[s[i]] << (i * 2);
  }
  return j;
}

int main() {
  int n;
  char s[100];
  C['A'] = 0;
  C['C'] = 1;
  C['G'] = 2;
  C['T'] = 3;
  fill(isExist[0], isExist[9], false);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (isExist[strlen(s)][convert(string(s))])
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf("%s", s);
      isExist[strlen(s)][convert(string(s))] = true;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

const int MAX_S = 1 << 16;
int C[128];

bool isExist[9][MAX_S];

int convert(string s) {
  int j = 0;
  for (int i = 0; i < s.size(); ++i) {
    j |= C[s[i]] << (i * 2);
  }
  return j;
}

int main() {
  int n;
  char s[100];
  C['A'] = 0;
  C['C'] = 1;
  C['G'] = 2;
  C['T'] = 3;
  fill(isExist[0], isExist[9], false);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (isExist[strlen(s)][convert(string(s))])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else {
      scanf("%s", s);
      isExist[strlen(s)][convert(string(s))] = true;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef long long lli;

const int M = 5000001;
const int P = M / 10;

lli C[128];
lli X[M];

lli convert(const char s[]) {
  lli j = 0;
  for (lli i = 0; s[i] != '\\0'; ++i) {
    j |= C[s[i]] << (i * 3LL);
  }
  return j;
}

void insert(const char s[]) {
  int x = convert(s);
  int p = x % M;
  while (X[p] != -1LL)
    p = (p + P) % M;
  X[p] = x;
}

bool find(const char s[]) {
  int x = convert(s);
  int p = x % M;
  while (X[p] != -1LL) {
    if (X[p] == x)
      return true;
    p = (p + P) % M;
  }
  return false;
}

int main() {
  int n;
  char s[100];
  C['A'] = 1LL;
  C['C'] = 2LL;
  C['G'] = 3LL;
  C['T'] = 4LL;
  fill(X, X + M, -1LL);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (find(s))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf("%s", s);
      insert(s);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef long long lli;

const int M = 5000001;
const int P = 32;

lli C[128];
lli X[M];

lli convert(const char s[]) {
  lli j = 0;
  for (lli i = 0; s[i] != '\\0'; ++i) {
    j |= C[s[i]] << (i * 3LL);
  }
  return j;
}

void insert(const char s[]) {
  int x = convert(s);
  int p = x % M;
  while (X[p] != -1LL)
    p = (p + P) % M;
  X[p] = x;
}

bool find(const char s[]) {
  int x = convert(s);
  int p = x % M;
  while (X[p] != -1LL) {
    if (X[p] == x)
      return true;
    p = (p + P) % M;
  }
  return false;
}

int main() {
  int n;
  char s[100];
  C['A'] = 1LL;
  C['C'] = 2LL;
  C['G'] = 3LL;
  C['T'] = 4LL;
  fill(X, X + M, -1LL);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (find(s))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf("%s", s);
      insert(s);
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef unsigned int uint;

const uint M = 6000011u;
const uint P = M / 10u + 1u;

uint Five[13];
uint C[128];
uint X[M];

uint convert(const char s[]) {
  uint j = 0;
  for (uint i = 0u; s[i] != '\\0'; ++i) {
    j += C[s[i]] * Five[i];
  }
  return j;
}

void insert(const char s[]) {
  uint x = convert(s);
  uint p = x % M;
  while (X[p] != UINT_MAX)
    p = (p + P) % M;
  X[p] = x;
}

bool find(const char s[]) {
  uint x = convert(s);
  uint p = x % M;
  while (X[p] != UINT_MAX) {
    if (X[p] == x)
      return true;
    p = (p + P) % M;
  }
  return false;
}

int main() {
  int n;
  char s[100];

  C['A'] = 1u;
  C['C'] = 2u;
  C['G'] = 3u;
  C['T'] = 4u;
  Five[0] = 1u;
  for (int i = 1; i < 13; ++i)
    Five[i] = Five[i - 1] * 5u;

  fill(X, X + M, UINT_MAX);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (find(s))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf("%s", s);
      insert(s);
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

typedef unsigned int uint;

const uint M = 6000011u;
const uint P = 2u;

uint Five[13];
uint C[128];
uint X[M];

uint convert(const char s[]) {
  uint j = 0;
  for (uint i = 0u; s[i] != '\\0'; ++i) {
    j += C[s[i]] * Five[i];
  }
  return j;
}

void insert(const char s[]) {
  uint x = convert(s);
  uint p = x % M;
  while (X[p] != UINT_MAX)
    p = (p + P) % M;
  X[p] = x;
}

bool find(const char s[]) {
  uint x = convert(s);
  uint p = x % M;
  while (X[p] != UINT_MAX) {
    if (X[p] == x)
      return true;
    p = (p + P) % M;
  }
  return false;
}

int main() {
  int n;
  char s[100];

  C['A'] = 1u;
  C['C'] = 2u;
  C['G'] = 3u;
  C['T'] = 4u;
  Five[0] = 1u;
  for (int i = 1; i < 13; ++i)
    Five[i] = Five[i - 1] * 5u;

  fill(X, X + M, UINT_MAX);
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    if (s[0] == 'f') {
      scanf("%s", s);
      if (find(s))
        printf("yes\\n");
      else
        printf("no\\n");
    } else {
      scanf("%s", s);
      insert(s);
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

map<string, int> m;

int main() {
  int n;
  cin >> n;
  char inp[8];

  rep(i, n) {
    string word;
    scanf("%s", inp);
    cin >> word;

    if (inp[0] == 'i') {
      m[word] = 1;
    } else {
      if (m.find(word) != m.end())
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 20)

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;

map<string, int> m;

int main() {
  int n;
  cin >> n;

  rep(i, n) {
    string s, word;
    cin >> s >> word;

    if (s == "insert")
      m[word] = 1;
    else if (s == "find") {
      if (m[word])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}