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

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

using namespace std;

int main() {
  int n;
  string commond, word;
  set<string> dictionary;
  queue<string> result;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> commond;
    if (commond == "insert") {
      cin >> word;
      dictionary.insert(word);
    } else {
      cin >> word;
      if (dictionary.count(word)) {
        result.push("yes");
      } else {
        result.push("no");
      }
    }
  }
  while (!result.empty()) {
    cout << result.front() << endl;
    result.pop();
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;                     // number of lines for input
  typedef set<string> set_t; // define set of strings
  set_t A;                   // Initialize A
  string command;            // either insert or find
  string word;               // string to insert
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> command;
    if (command == "insert") {
      cin >> word;
      if (word.size() > 12)
        break;
      A.insert(word);
    } else if (command == "find") {
      cin >> word;
      if (A.count(word) == 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    } else
      break;
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS

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

struct Node {
  char str[12] = "\\0";
  Node *p_r = NULL;
  Node *p_l = NULL;
};

class Dictionary {
private:
  Node dummy;

public:
  void insert(const char str[]);
  bool find(const char str[]);
};

void Dictionary::insert(const char str[]) {
  Node *p_node = &dummy;
  Node **pp_node = NULL;
  while (p_node != NULL) {
    int rt = strcmp(str, p_node->str);
    if (rt == 0)
      return;
    if (rt > 0) {
      pp_node = &(p_node->p_r);
      p_node = p_node->p_r;
    } else {
      pp_node = &(p_node->p_l);
      p_node = p_node->p_l;
    }
  }

  Node *p_newnode = new Node;
  strcpy(p_newnode->str, str);
  *pp_node = p_newnode;
}

bool Dictionary::find(const char str[]) {
  Node *p_node = &dummy;
  while (p_node != NULL) {
    int rt = strcmp(str, p_node->str);
    if (rt == 0)
      return true;
    if (rt > 0) {
      p_node = p_node->p_r;
    } else {
      p_node = p_node->p_l;
    }
  }
  return false;
}

int main(void) {
  Dictionary dic;

  int n;
  cin >> n;

  char cm[7] = "\\0", str[13] = "\\0";
  for (int i = 0; i < n; i++) {
    cin >> cm >> str;
    if (strcmp(cm, "insert") == 0) {
      dic.insert(str);
    } else if (strcmp(cm, "find") == 0) {
      bool result = dic.find(str);
      if (result)
        cout << "yes";
      else
        cout << "no";
      cout << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

class Node {
private:
  char str[12] = "\\0";
  Node *p_next = NULL;

public:
  Node(const char s[]) { strcpy(str, s); }
  Node() {}
  Node *getNext() { return p_next; }
  bool equal(const char s[]) { return strcmp(str, s) == 0; }
  bool high(const char s[]) { return (strcmp(s, str) > 0); }
  void linkNext(Node *p_n) { p_next = p_n; }
};

class Dictionary {
private:
  Node dummy;

public:
  void insert(const char str[]);
  bool find(const char str[]);
};

void Dictionary::insert(const char str[]) {
  Node *p_node = &dummy;
  while (1) {
    if (p_node->equal(str))
      return;
    if (p_node->high(str))
      break;
    p_node = p_node->getNext();
  }

  Node *p_newnode = new Node(str);
  p_newnode->linkNext(p_node->getNext());
  p_node->linkNext(p_newnode);
}

bool Dictionary::find(const char str[]) {
  Node *p_node = &dummy;
  while (p_node = p_node->getNext()) {
    if (p_node->equal(str))
      return true;
  }
  return false;
}

int main(void) {
  Dictionary dic;

  int n;
  cin >> n;

  char cm[7] = "\\0", str[13] = "\\0";
  for (int i = 0; i < n; i++) {
    cin >> cm >> str;
    if (strcmp(cm, "insert") == 0) {
      dic.insert(str);
    } else if (strcmp(cm, "find") == 0) {
      bool result = dic.find(str);
      if (result)
        cout << "yes";
      else
        cout << "no";
      cout << endl;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>

// 5^12
// 0 -
// 1 A
// 2 G
// 3 C
// 4 T

// AAA = 1 * 4^0 + 1 * 4^1 + 1 * 4^3
// data[x] == 0 : none
// data[x] == 1 : found

int encode_ch(char ch) {
  switch (ch) {
  case 'A':
    return 1;
  case 'G':
    return 2;
  case 'C':
    return 3;
  case 'T':
    return 4;
  default:
    return 0;
  }
}

int encode(std::string str) {
  int x = 0;
  for (int i = 0; i < str.size(); i++) {
    x += encode_ch(str[str.size() - 1 - i]) * std::pow(4, i);
  }
  return x;
}

int main() {

  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::vector<bool> d;
  d.resize(244140625, false); // 5^12 = 244140625

  int n;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::string op;
    std::string w;
    std::cin >> op >> w;

    if (op == "insert") {
      d[encode(w)] = true;
    } else if (op == "find") {
      std::cout << (d[encode(w)] ? "yes" : "no") << std::endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>

int main() {

  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  int n;
  std::cin >> n;

  std::vector<std::string> d;

  d.push_back("zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz");

  for (int i = 0; i < n; i++) {
    std::string op;
    std::string w;
    std::cin >> op >> w;

    if (op == "insert") {
      for (std::vector<std::string>::iterator i = d.begin(); i != d.end();
           ++i) {
        if (w == *i)
          break;
        if (w < *i) {
          d.insert(i, w);
          break;
        }
      }
    } else if (op == "find") {
      if (std::binary_search(d.begin(), d.end(), w))
        std::cout << "yes" << std::endl;
      else
        std::cout << "no" << std::endl;
    }
  }
  /*
  for (int i = 0; i < d.size(); i++) {
          std::cout << d[i] << std::endl;
  }
  */
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

#define M 1000003

long long StrToID(std::string str) {
  long long id = 0;
  for (int i = 0; i < str.length(); ++i) {
    switch (str[i]) {
    case 'A':
      id = 1 + id * 10;
      break;
    case 'C':
      id = 2 + id * 10;
      break;
    case 'G':
      id = 3 + id * 10;
      break;
    case 'T':
      id = 4 + id * 10;
      break;
    default:
      break;
    }
  }
  return id;
}

bool includeNum(long long fig, std::vector<long long> S) {
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == fig)
      return true;
  }
  return false;
}

int main(int argc, char const *argv[]) {
  std::vector<std::vector<long long>> Dictionary(M);
  int N, key;
  long long id;
  std::string cmd, str;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> cmd >> str;
    id = StrToID(str);
    key = id % M;
    if (cmd == "insert") {
      if (!includeNum(id, Dictionary[key]))
        Dictionary[key].push_back(id);
    } else if (cmd == "find") {
      if (includeNum(id, Dictionary[key])) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    } else {
      // exception
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

#define M 10000000

long long StrToID(std::string str) {
  long long id = 0;
  for (int i = 0; i < str.length(); ++i) {
    switch (str[i]) {
    case 'A':
      id = 1 + id * 10;
      break;
    case 'C':
      id = 2 + id * 10;
      break;
    case 'G':
      id = 3 + id * 10;
      break;
    case 'T':
      id = 4 + id * 10;
      break;
    default:
      break;
    }
  }
  return id;
}

bool includeNum(long long fig, std::vector<long long> S) {
  for (int i = 0; i < S.size(); ++i) {
    if (S[i] == fig)
      return true;
  }
  return false;
}

int main(int argc, char const *argv[]) {
  std::vector<std::vector<long long>> Dictionary(M);
  int N, key;
  long long id;
  std::string cmd, str;
  std::cin >> N;

  for (int i = 0; i < N; ++i) {
    std::cin >> cmd >> str;
    id = StrToID(str);
    key = id % M;
    if (cmd == "insert") {
      if (!includeNum(id, Dictionary[key]))
        Dictionary[key].push_back(id);
    } else if (cmd == "find") {
      if (includeNum(id, Dictionary[key])) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    } else {
      // exception
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int i, n;
  cin >> n;
  string order, word;
  unordered_set<string> d;
  for (i = 0; i < n; i++) {
    cin >> order >> word;
    if (order[0] == 'i') {
      d.insert(word);
    } else {
      if (d.count(word) == 0) {
        printf("no\\n");
      } else {
        printf("yes\\n");
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int i, n;
  cin >> n;
  string order, word;
  vector<string> d;
  for (i = 0; i < n; i++) {
    cin >> order >> word;
    if (order[0] == 'i') {
      d.push_back(word);
    } else {
      auto itr = find(d.begin(), d.end(), word);
      if (itr == d.end()) {
        printf("no\\n");
      } else {
        printf("yes\\n");
      }
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <math.h>
#include <stdio.h>

int C[1777771];
int n;
char com[13];
char str[13];

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

int f() {
  int res = 0;
  int i;

  for (i = 0; str[i] != '\\0'; i++) {
    res = res * 4 + g(str[i]);
  }
  return res;
}

int main(void) {
  int i, n, ans, key;

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

    key = f();

    if (com[0] == 'f') {
      ans = C[key % 1777771];
      if (ans) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else {
      C[key % 1777771] = 1;
    }
  }
  return (0);
}


Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>
int C[1000000];
int n;
char com[13];
char str[13];

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

int f() {
  int res = 0;
  int i;

  for (i = 0; str[i] != '\\n'; i++) {
    res = res * 4 + g(str[i]);
  }
  return res;
}

int main(void) {
  int i, n, ans, key;

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

    key = f();

    if (com[0] == 'f') {
      ans = C[key];
      if (ans) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    } else {
      C[key] = 1;
    }
  }
  return (0);
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>

using namespace std;

const int M = 1046527;
string table[M];

int get_char(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;
}

long long get_key(string &text) {
  long long sum = 0, p = 1;
  for (auto &e : text) {
    sum += p * get_char(e);
    p *= 5; // 5 ** (text.size())
  }
  return sum;
}

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

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

bool insert(string &text) {
  int i = 0;
  long long k = get_key(text);
  while (true) {
    long long j = (h1(k) + i * h2(k)) % M;
    if (table[j] == text)
      return true;
    else if (table[j].empty()) {
      table[j] = text;
      return false;
    } else
      i += 1;
  }
  return false;
}

bool find(string &text) {
  int i = 0;
  long long k = get_key(text);
  while (true) {
    long long j = (h1(k) + i * h2(k)) % M;
    if (table[j] == text)
      return true;
    else if (table[j].empty())
      return false;
    else
      i += 1;
  }
  return false;
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    string order, text;
    cin >> order >> text;
    if (order[0] == 'i') {
      insert(text);
    } else {
      if (find(text))
        cout << "yes";
      else
        cout << "no";
      cout << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

const int M = 1046527;
string table[M];

int get_char(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;
}

long long get_key(string &text) {
  long long sum = 0, p = 1;
  for (auto &e : text) {
    sum += p * get_char(e);
    p *= 5; // 5 ** (text.size())
  }
  return sum;
}

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

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

bool insert(string &text) {
  int i = 0;
  long long k = get_key(text);
  while (true) {
    int j = (h1(k) + i * h2(k)) % M;
    if (table[j] == text)
      return true;
    else if (table[j].empty()) {
      table[j] = text;
      return false;
    } else
      i += 1;
  }
  return false;
}

bool find(string &text) {
  int i = 0;
  long long k = get_key(text);
  while (true) {
    int j = (h1(k) + i * h2(k)) % M;
    if (table[j] == text)
      return true;
    else if (!table[j].empty())
      return false;
    else
      i += 1;
  }
  return false;
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  while (n--) {
    string order, text;
    cin >> order >> text;
    if (order[0] == 'i') {
      insert(text);
    } else {
      if (find(text))
        cout << "yes";
      else
        cout << "no";
      cout << endl;
    }
  }

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

#include <cstdio>
const int SIZE = 33554432;
#define HASH(A)                                                                \\
  char *c = A;                                                                 \\
  Hash = 1;                                                                    \\
  while (*c) {                                                                 \\
    Hash <<= 2;                                                                \\
    Hash += Key[*c];                                                           \\
    c++;                                                                       \\
  }

int main() {
  bool dic[SIZE];
  int Hash;
  char Key[85];
  Key['A'] = 0;
  Key['C'] = 1;
  Key['G'] = 2;
  Key['T'] = 3;
  int n;
  std::scanf("%d\\n", &n);
  char str[21];
  for (int i = 0; i < n; i++) {
    std::fgets(str, 21, stdin);
    if (str[0] == 'i') {
      HASH(str + 7);
      dic[Hash] = 1;
    } else {
      HASH(str + 5);
      std::puts(dic[Hash] ? "yes" : "no");
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

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

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

char H[M][L];

// 文字から数値に変換
int getChar(char ch) {
  if (ch == 'A')
    return 1;
  else if (ch == 'C')
    return 2;
  else if (ch == 'G')
    return 3;
  else if (ch == 'T')
    return 4;
  else
    return 0;
}

// 文字列から数値へ変換して key を生成する
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); // より高速な入力 scnaf を使用

    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>
#include <vector>
#define M 1046527

std::vector<std::string> H(M);
long long getKey(std::string s) {
  long long sum = 0;
  for (int i = 0; i < s.size(); i++) {
    long long t = 0;
    switch (s[i]) {
    case 'A':
      t = 1;
      break;
    case 'C':
      t = 2;
      break;
    case 'G':
      t = 3;
      break;
    case 'T':
      t = 4;
      break;
    default:
      t = 0;
      break;
    }
    sum += t;
    sum *= 5;
  }
  return sum;
}

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

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

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

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

int main() {
  int n;
  std::cin >> n;
  std::vector<std::string> cmd(n);
  std::vector<std::string> s(n);
  for (int i = 0; i < n; i++) {
    std::cin >> cmd[i] >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (cmd[i][0] == 'i') {
      insert(s[i]);
    } else if (cmd[i][0] == 'f') {
      if (find(s[i])) {
        std::cout << "yes" << std::endl;
      } else {
        std::cout << "no" << std::endl;
      }
    }
  }
}

Error: TLE

Fixed submission:

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

int n;

int main() {
  std::cin >> n;
  std::vector<std::string> a;
  std::string b;
  std::string c;
  for (int i = 0; i < n; i++) {
    std::cin >> b;
    if (b[0] == 'i') {
      std::cin >> c;
      a.push_back(c);
    } else {
      std::cin >> c;
      int f = 1;
      for (std::vector<std::string>::iterator it = a.begin(); it != a.end();
           ++it) {
        if (c == *it) {
          std::cout << "yes" << std::endl;
          f = 0;
          break;
        }
      }
      if (f)
        std::cout << "no" << std::endl;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const long N = 16777216;
char str[N][13];
long n;

int getInt(char ch) {
  switch (ch) {
  case 'A':
    return 1;
  case 'T':
    return 2;
  case 'G':
    return 3;
  case 'C':
    return 4;
  }
  return 0;
}
long long getKey(char *tmp) {
  long long sum = 0, p = 1;
  for (int i = 0; i < strlen(tmp); ++i) {
    sum += p * getInt(tmp[i]);
    p *= 5;
  }
  return sum;
}

int main() {
  scanf("%ld", &n);
  while (n--) {
    char ch[13];
    scanf("%s", ch);
    if (strcmp(ch, "insert") == 0) {
      scanf("%s", ch);
      long long tmp = getKey(ch);
      strcpy(str[tmp], ch);
    } else if (strcmp(ch, "find") == 0) {
      scanf("%s", ch);
      long long tmp = getKey(ch);
      if (strcmp(str[tmp], ch) == 0)
        cout << "yes\\n";
      else
        cout << "no\\n";
    }
  }
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

const long N = 1046527;
char str[N][13];
long n;

int getInt(char ch) {
  switch (ch) {
  case 'A':
    return 1;
  case 'T':
    return 2;
  case 'G':
    return 3;
  case 'C':
    return 4;
  }
  return 0;
}
long long getKey(char *tmp) {
  long long sum = 0, p = 1;
  for (int i = 0; i < strlen(tmp); ++i) {
    sum += p * getInt(tmp[i]);
    p *= 5;
  }
  return sum;
}

int main() {
  scanf("%ld", &n);
  while (n--) {
    char ch[13];
    scanf("%s", ch);
    if (strcmp(ch, "insert") == 0) {
      scanf("%s", ch);
      long long tmp = getKey(ch);
      strcpy(str[tmp], ch);
    } else if (strcmp(ch, "find") == 0) {
      scanf("%s", ch);
      long long tmp = getKey(ch);
      if (strcmp(str[tmp], ch) == 0)
        cout << "yes\\n";
      else
        cout << "no\\n";
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dic[2236920];

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

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

    int key = 0, key2;
    for (int i = 0; i < str.size(); i++) {
      switch (str[i]) {
      case 'A':
        key2 = 1;
        break;
      case 'C':
        key2 = 2;
        break;
      case 'G':
        key2 = 3;
        break;
      case 'T':
        key2 = 4;
        break;
      }

      key += key2 * pow(4, str.size() - i - 1);
    }

    if (command[0] == 'i') {
      dic[key - 1] = 1;
    } else if (dic[key - 1]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

void BinarySearch(vector<string> S, int n, string key) {
  int l = 0, r = n;
  while (l < r) {
    int m = (l + r) / 2;
    if (S[m] < key) {
      l = m + 1;
    } else if (S[m] > key) {
      r = m;
    } else {
      cout << "yes" << endl;
      return;
    }
  }

  cout << "no" << endl;
}

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

  int n;
  cin >> n;

  vector<string> dic;
  for (int i = 0; i < n; i++) {
    string order, str;
    cin >> order >> str;

    if (order == "insert") {
      dic.push_back(str);
    }

    if (order == "find") {
      sort(dic.begin(), dic.end());
      BinarySearch(dic, dic.size(), str);
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(false);
  int n, i;
  cin >> n;

  string command, str;
  set<string> S;
  for (i = 0; i < n; i++) {
    cin >> command >> str;
    if (command == "insert") {
      S.insert(str);
    } else {
      if (S.find(str) == S.end()) {
        cout << "no" << endl;
      } else {
        cout << "yes" << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

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

  string command, str;
  set<string> S;
  for (i = 0; i < n; i++) {
    cin >> command >> str;
    if (command == "insert") {
      S.insert(str);
    } else {
      if (S.find(str) == S.end()) {
        cout << "no" << endl;
      } else {
        cout << "yes" << endl;
      }
    }
  }
  return 0;
}