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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstddef>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <valarray>
#include <vector>

using ll = long long;
using ull = unsigned long long;
using ld = long double;

// vector
template <class T> using vec = std::vector<T>;

template <class T> using vvec = vec<vec<T>>;

constexpr std::size_t operator""_sz(ull n) { return std::size_t(n); }

int main() {
  using namespace std;
  // cerr.rdbuf(nullptr);

  set<string> dict;
  int n;
  cin >> n;

  for (int i = 0; i < n; ++i) {
    string o, str;
    cin >> o >> str;
    if (o == "find") {
      if (dict.count(str) == 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else {
      dict.emplace(str);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <list>
#include <string>

using namespace std;

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

  list<string> li;

  for (int i = 0; i < n; i++) {
    string command;
    string str;
    cin >> command >> str;

    if (command[0] == 'i')
      li.push_back(str);
    else {
      bool isExist = false;
      for (auto s : li)
        if (s == str)
          isExist = true;
      cout << (isExist ? "yes" : "no") << endl;
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <cstdlib>
#include <cstring>

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

char H[M][L];

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

int getChar(char ch) {
  switch (ch) {
  case 'A':
    return 1;
  case 'C':
    return 2;
  case 'G':
    return 3;
  case 'T':
    return 4;
  }
  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 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;
};

bool 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 true;
    else if (strlen(H[h]) == 0)
      return false;
  }
  return false;
}

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

  char order[10], str[L];
  for (long long i = 0; i < M; i++)
    H[i][0] = '\\0';
  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, str);
    if (strcmp(order, "insert") == 0) {
      int x = insert(str);
    } else if (strcmp(order, "find") == 0) {
      bool x = find(str);
      if (x) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>

#define MAX_N 1000000
long T[MAX_N] = {};

long str2long(char a[]) {
  char str[50] = "";
  for (int i = 0; i < (sizeof &a / sizeof a[0]) - 1; i++) {
    char c[10];
    sprintf(c, "%d", a[i] - '0');
    strcat(str, c);
  }
  char *ends;
  return strtol(str, &ends, 0);
}

int h(char key[], int i) {
  long x = str2long(key);
  int hash = (int)(x % MAX_N) + i;
  if (MAX_N <= hash) {
    hash %= MAX_N;
  }
  return hash;
}

int insert(char key[]) {
  int i = 0;
  int position;
  for (;;) {
    position = h(key, i);
    if (T[position] == 0) {
      T[position] = str2long(key);
      break;
    }
    i++;
  }
  return position;
};

bool find(char key[]) {
  int i = 0;
  int position;
  for (;;) {
    if (MAX_N <= i)
      return false;
    position = h(key, i);
    if (T[position] == str2long(key)) {
      break;
    }
    i++;
  }
  return true;
}

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

  char order[10];
  char key[12];
  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, key);
    if (strcmp(order, "insert") == 0) {
      int x = insert(key);
    } else if (strcmp(order, "find") == 0) {
      bool x = find(key);
      if (x) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }

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

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

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

char Hash[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, h, i;
  key = getkey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(Hash[h], str) == 0)
      return 1;
    else if (strlen(Hash[h]) == 0)
      return 0;
  }
  return 0;
}

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

int main() {
  int n;
  char ORDER[10], MOJI[L];

  for (int i = 0; i < M; i++)
    Hash[i][0] = '\\0';
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", ORDER, MOJI);
    if (ORDER[0] == 'i')
      insert(MOJI);
    else {
      if (find(MOJI))
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

char Hash[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;
  for (long long 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 insert(char str[]) {
  long long key, h, i;
  key = getkey(str);
  for (i = 0;; i++) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(Hash[h], str) == 0)
      return 1;
    else if (strlen(Hash[h]) == 0) {
      strcpy(Hash[h], str);
      return 0;
    }
  }
  return 0;
}

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

int main() {
  int n;
  char ORDER[10], MOJI[L];

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

ll pow_int(int x, int y) {
  ll res = 1;
  for (int i = 1; i <= y; i++)
    res *= x;
  return res;
}

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

  ll n;
  cin >> n;
  map<string, int> map;
  REP(i, n) {
    string meirei, str;
    cin >> meirei >> str;
    if (meirei[0] == 'i')
      map[str] = 1;
    else if (map[str] == 1)
      cout << "yes\\n";
    else
      cout << "no\\n";
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

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

  ll n;
  cin >> n;
  map<char, list<string>> dic;
  REP(i, n) {
    string meirei, str;
    cin >> meirei >> str;
    char c = str[0];
    if (meirei == "insert") {
      list<string>::iterator itr =
          lower_bound(dic[c].begin(), dic[c].end(), str);
      if (itr == dic[c].end())
        dic[c].push_back(str);
      else if (*itr != str)
        dic[c].insert(itr, str);
    } else {
      if (binary_search(dic[c].begin(), dic[c].end(), str))
        cout << "yes\\n";
      else
        cout << "no\\n";
    }
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>

typedef unsigned uint;

int main() {
  std::ios_base::sync_with_stdio(false);

  std::unordered_set<std::string> dic;

  uint n;
  std::cin >> n;

  std::string buf;
  for (uint i = 0; i < n; i++) {
    std::cin >> buf;
    if (buf == "insert") {
      std::cin >> buf;
      dic.insert(buf);
    }
    if (buf == "find") {
      std::cin >> buf;
      std::cout << ((dic.find(buf) == dic.end()) ? "no" : "yes") << '\\n';
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

typedef unsigned uint;

int main() {
  std::ios_base::sync_with_stdio(false);

  std::list<std::string> dic;

  uint n;
  std::cin >> n;

  std::string buf;
  for (uint i = 0; i < n; i++) {
    std::cin >> buf;
    if (buf == "insert") {
      std::cin >> buf;
      dic.push_back(buf);
    }
    if (buf == "find") {
      std::cin >> buf;
      std::cout << ((std::find(dic.begin(), dic.end(), buf) == dic.end())
                        ? "no"
                        : "yes")
                << '\\n';
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define M 1000000
#define NIL -1;
#define L 14
char H[M][L];

int h1(int key) { return key % M; }
int h2(int key) { return (1 + (key % (M - 1))); }
int h3(int key, int i) { return (h1(key) + i * h2(key)) % 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(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 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 Find(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)
      return 0;
  }
  return 0;
}

int main() {
  int n, i;
  char cmd[9], str[L];
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> cmd >> str;
    if (cmd[0] == 'i') {
      Insert(str);
    } else if (cmd[0] == 'f') {
      if (Find(str)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define M 10465247
#define NIL -1;
#define L 14
char H[M][L];

int h1(int key) { return key % M; }
int h2(int key) { return (1 + (key % (M - 1))); }
int h3(int key, int i) { return (h1(key) + i * h2(key)) % 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(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 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 Find(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)
      return 0;
  }
  return 0;
}

int main() {
  int n, i;
  char cmd[9], str[L];
  for (i = 0; i < M; i++) {
    H[i][0] = '\\0';
  }
  cin >> n;
  for (i = 0; i < n; ++i) {
    cin >> cmd >> str;
    if (cmd[0] == 'i') {
      Insert(str);
    } else if (cmd[0] == 'f') {
      if (Find(str)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

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

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main(void) {
  map<string, bool> dict;
  int n;

  cin >> n;
  for (int i = 0; i < n; i++) {
    char com[13], str[10];
    cin >> com;
    cin >> str;
    if (com[0] == 'i') {
      dict[string(str)] = true;
    } else {
      if (dict[string(str)])
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

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

#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  set<string> s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string a, b;
    cin >> a >> b;
    if (a[0] == 'i')
      s.insert(b);
    else {
      if (s.find(b) != s.end())
        cout << "yes\\n";
      else
        cout << "no\\n";
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool find(vector<string> l, string s) {
  for (size_t i = 0; i < l.size(); i++) {
    if (l[i] == s)
      return true;
    if (l[i] > s)
      return false;
  }
  return false;
}
int main() {
  vector<string> l;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s, str;
    cin >> s >> str;
    if (s == "insert") {
      l.push_back(str);
      sort(l.begin(), l.end());
    } else if (s == "find")
      cout << (find(l, str) ? "yes\\n" : "no\\n");
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define M 1000003

int T[M] = {0};

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 key) {
  int j, i = 0;
  while (1) {
    j = h(key, i);
    if (T[j] == 0) {
      T[j] = key;
      return;
    } else {
      i++;
    }
  }
}

bool search(int key) {
  int j, i = 0;
  while (1) {
    j = h(key, i);
    if (T[j] == key)
      return true;
    else if (T[j] == 0 || i >= M)
      return false;
    else
      i++;
  }
}

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;
}

int getKey(string str) {
  int sum = 0, p = 1;
  REP(i, str.size()) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int main() {
  int n;
  cin >> n;
  string c, s;
  REP(i, n) {
    cin >> c >> s;
    int key;
    key = getKey(s);
    if (c[0] == 'i')
      insert(key);
    else if (search(key))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)

int n;
int T[1000000] = {0};

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

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

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

void insert(int key) {
  int j, i = 0;
  while (1) {
    j = h(key, i);
    if (T[j] == 0) {
      T[j] = key;
      return;
    } else {
      i++;
    }
  }
}

bool search(int key) {
  int j, i = 0;
  while (1) {
    j = h(key, i);
    if (T[j] == key)
      return true;
    else if (T[j] == 0 || i >= n)
      return false;
    else
      i++;
  }
}

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;
}

int getKey(string str) {
  int sum = 0, p = 1;
  REP(i, str.size()) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

int main() {
  cin >> n;
  string c, s;
  REP(i, n) {
    cin >> c >> s;
    int key;
    key = getKey(s);
    if (c[0] == 'i')
      insert(key);
    else if (search(key))
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

#define MAX 10000000
#define INF 922337203685477580
typedef long long ll;

using namespace std;

char table[MAX][14];

int char2ord(char c) {
  if (c == 'A') {
    return 2;
  } else if (c == 'T') {
    return 3;
  } else if (c == 'C') {
    return 5;
  } else if (c == 'G') {
    return 7;
  } else {
    assert(0);
  }
}
ll genHash(string s) {
  ll hash = 11;
  for (string::iterator it = s.begin(); it != s.end(); ++it) {
    hash *= *it * 13;
    hash = hash % MAX;
    // cout << hash << endl;
  }
  // cout << "hash for <" << s << ">: " << hash << endl;
  return hash;
}
void insert(string s) {
  ll hash = genHash(s);
  while (strlen(table[hash]) != 0) { // if not empty
    hash += 1;
  }
  strcpy(table[hash], s.c_str());
}
int find(string s) {
  ll hash = genHash(s);

  while (strlen(table[hash]) != 0) {
    if (strcmp(table[hash], s.c_str()) == 0) {
      return 1; // found
    }
    hash++;
  }
  return 0;
}

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

  for (int i = 0; i < n; i++) {
    char cmd[12];
    // cin >> cmd;
    scanf("%s", cmd);

    if (cmd[0] == 'i') { // insert
      string dna;
      cin >> dna;
      insert(dna);
    } else if (cmd[0] == 'f') { // find
      string dna;
      cin >> dna;
      if (find(dna)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>

#define MAX 10000000
#define INF 922337203685477580
typedef long long ll;

using namespace std;

char table[MAX][14];

int char2ord(char c) {
  if (c == 'A') {
    return 2;
  } else if (c == 'T') {
    return 3;
  } else if (c == 'C') {
    return 5;
  } else if (c == 'G') {
    return 7;
  } else {
    assert(0);
  }
}
ll genHash(string s) {
  ll hash = 11;
  for (string::iterator it = s.begin(); it != s.end(); ++it) {
    hash *= *it * 13;
    hash = hash % MAX;
    // cout << hash << endl;
  }
  // cout << "hash for <" << s << ">: " << hash << endl;
  return hash;
}
void insert(string s) {
  ll hash = genHash(s);
  while (strlen(table[hash]) != 0) { // if not empty
    hash += 1;
  }
  strcpy(table[hash], s.c_str());
}
int find(string s) {
  ll hash = genHash(s);

  while (strlen(table[hash]) != 0) {
    if (strcmp(table[hash], s.c_str()) == 0) {
      return 1; // found
    }
    hash++;
  }
  return 0;
}

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

  for (int i = 0; i < n; i++) {
    string cmd;
    cin >> cmd;

    if (cmd == "insert") {
      string dna;
      cin >> dna;
      insert(dna);
    } else if (cmd == "find") {
      string dna;
      cin >> dna;
      if (find(dna)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>

using namespace std;

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

  int n;
  cin >> n;

  unordered_set<string> d;
  while (n--) {
    string cmd, s;
    cin >> cmd >> s;

    if (cmd == "insert")
      d.insert(s);
    else
      cout << (d.find(s) != d.end() ? "yes" : "no") << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

  int n;
  cin >> n;

  vector<string> A, C, G, T;
  string s;
  while (n--) {
    cin >> s;
    if (s == "insert") {
      cin >> s;
      switch (s[0]) {
      case 'A':
        A.push_back(s);
        break;
      case 'C':
        C.push_back(s);
        break;
      case 'G':
        G.push_back(s);
        break;
      case 'T':
        T.push_back(s);
        break;
      }
    } else if (s == "find") {
      cin >> s;
      vector<string>::iterator it, itEnd;
      switch (s[0]) {
      case 'A':
        it = find(A.begin(), A.end(), s);
        itEnd = A.end();
        break;
      case 'C':
        it = find(C.begin(), C.end(), s);
        itEnd = C.end();
        break;
      case 'G':
        it = find(G.begin(), G.end(), s);
        itEnd = G.end();
        break;
      case 'T':
        it = find(T.begin(), T.end(), s);
        itEnd = T.end();
        break;
      }
      if (it != itEnd)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
bool h[500000000];
int main() {
  int n, i, t = 0, q[13];
  for (i = 1; t < 13; i *= 5)
    q[t++] = i;
  cin >> n;
  while (n--) {
    char a[10], b[14];
    scanf("%s %s", a, b);
    for (t = i = 0; i < strlen(b); i++) {
      if (b[i] == 'A')
        t += q[i] * 1;
      if (b[i] == 'C')
        t += q[i] * 2;
      if (b[i] == 'G')
        t += q[i] * 3;
      if (b[i] == 'T')
        t += q[i] * 4;
    }
    if (a[0] == 'i')
      h[t]++;
    else {
      if (h[t])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool h[500000000];
int main() {
  int n, i, t = 0, q[13];
  for (i = 1; t < 13; i *= 5)
    q[t] = i;
  cin >> n;
  while (n--) {
    char a[10], b[14];
    scanf("%s %s", a, b);
    for (t = i = 0; i < strlen(b); i++) {
      if (b[i] == 'A')
        b[i] = q[i] * 1;
      if (b[i] == 'C')
        b[i] = q[i] * 1;
      if (b[i] == 'G')
        b[i] = q[i] * 1;
      if (b[i] == 'T')
        b[i] = q[i] * 1;
    }
    if (a[0] == 'i')
      h[t]++;
    else {
      if (h[t])
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}