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

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.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 argc, char const *argv[]) {
  char str[L], com[9];
  int i, n, h;
  std::cin >> n;
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
      // printf("ins\\n");
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define M 10465277
#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 argc, char const *argv[]) {
  char str[L], com[9];
  int i, n, h;
  std::cin >> n;
  for (i = 0; i < M; i++)
    H[i][0] = '\\0';
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, str);
    if (com[0] == 'i') {
      insert(str);
      // printf("ins\\n");
    } 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 <unordered_set>
using namespace std;

int main() {
  typedef unordered_set<string> set_t;
  set_t A;
  int n;
  cin >> n;
  string inst, word;
  for (int i = 0; i < n; i++) {
    cin >> inst >> word;
    if (inst == "insert") {
      A.insert(word);
    } else {
      int m = A.count(word);
      if (m == 0)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  typedef set<string> set_t;
  set_t A;
  int n;
  cin >> n;
  string inst, word;
  for (int i = 0; i < n; i++) {
    cin >> inst >> word;
    if (inst == "insert") {
      A.insert(word);
    } else {
      int m = A.count(word);
      if (m == 0)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#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;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
map<string, bool> D;
char c[30], s[30];
int N;
int main() {
  scanf("%d\\n", &N);
  for (; N--;) {
    scanf("%s%s", c, s);
    if (c[0] == 'i')
      D[string(s)] = true;
    else
      puts(D[string(s)] ? "yes" : "no");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
map<string, bool> D;
string c, s;
int N;
int main() {
  scanf("%d\\n", &N);
  for (; N--;) {
    cin >> c >> s;
    if (c == "insert")
      D[s] = true;
    else
      puts(D[s] ? "yes" : "no");
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int Hash[244140625];
char s[13];

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

int getKey() {
  int sum = 0, p = 1;

  for (int i = 0; i < strlen(s); i++) {
    sum += p * (getChar(s[i]));
    p *= 5;
  }
  return sum;
}

int main() {
  int n;
  char com[7];

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%s %s", com, s);
    if (com[0] == 'i') {
      Hash[getKey()] = 1;
    } else {
      if (Hash[getKey()] == 1)
        puts("yes");
      else
        puts("no");
    }
  }
}

Error: TLE

Fixed submission:

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

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

char H[M][L];

//?????? to ??°???
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);
    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\\n");
      } else
        printf("no\\n");
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
#define MOD 1046527
#define MAX 1046527
using namespace std;

bool A[MAX + 10] = {};
char s[MAX + 10][100] = {};

unsigned long long int hash2(char str[]) {
  int t;
  unsigned long long int a = 0, p = 1;
  for (int i = 0; i < (int)strlen(str); i++) {
    if (str[i] == 'A')
      t = 1;
    else if (str[i] == 'T')
      t = 2;
    else if (str[i] == 'C')
      t = 3;
    else if (str[i] == 'G')
      t = 4;
    else
      t = 0;
    a += t * p;
    p *= 5;
  }
  for (int i = 0;; i++) {
    unsigned long long int c = (a % MOD + i * (1 + a % (MOD - 1))) % MOD;
    if (!strcmp(s[c], str) || strlen(s[c]) == 0) {
      a = c;
      break;
    }
  }
  // printf("%llu ",a);fflush(stdout);
  return a;
}

int main() {
  int n;
  char str1[100];
  char str2[100];
  unsigned long long int h = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%s %s", str1, str2);
    h = hash2(str2);
    if (str1[0] == 'i') {
      A[h] = true;
      strcpy(s[h], str2);
    } else if (str1[0] == 'f') {
      if (A[h] && !strcmp(s[h], str2))
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int hash1(char *str) {
  int a = 0;
  for (int i = 0; i <= 13 && str[i] != '\\0'; i++) {
    a += ((str[i] - 'A' + 1) * pow(10, i));
  } // printf("%s:%d\\n",str,a);
  return a % 999991;
}
int main() {
  int n;
  char str1[100];
  char str2[100];
  bool A[1000000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%s %s", str1, str2);
    if (str1[0] == 'i') { // printf("insert:");
      A[hash1(str2)] = true;
    } else if (str1[0] == 'f') { // printf("find:");
      if (A[hash1(str2)])
        printf("yes\\n");
      else
        printf("no\\n");
    }
  }

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

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

int Hash[16777216];
char s[13];

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

int getKey() {
  int sum = 0, p = 1;
  for (int i = 0; i < strlen(s); i++) {
    sum += p * (getChar(s[i]));
    p *= 4;
  }
  return sum;
}

int main() {
  int n;
  char com[7];
  scanf("%d", &n);
  while (n--) {
    scanf("%s %s", com, s);
    if (com[0] == 'i') {
      Hash[getKey()] = 1;
      // printf("%d\\n",getKey());
    } else {
      if (Hash[getKey()]) {
        puts("yes");
      } else {
        puts("no");
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define M 1046527
#define L 15

char H[M][L];

int convertToNum(char *str) {
  int result = 0;
  int i = 0;
  while (str[i++]) {
    int j = 0;
    switch (str[i]) {
    case 'A':
      j = 1;
      break;
    case 'C':
      j = 2;
      break;
    case 'G':
      j = 3;
      break;
    case 'T':
      j = 4;
      break;
    }
    result += j * 5 ^ (i);
  }
  return result;
}

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

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

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

bool find(char *str) {
  int i = 0;
  int num = convertToNum(str);
  while (int key = getHash(num, i++)) {
    if (strcmp(H[key], str) == 0)
      return true;
    if (H[key][0] == '\\0')
      break;
  }
  return false;
}

void insert(char *str) {
  int i = 0;
  int num = convertToNum(str);
  while (int key = getHash(num, i++)) {
    if (strcmp(H[key], str) == 0)
      break;
    if (H[key][0] == '\\0') {
      strcpy(H[key], str);
      break;
    }
  }
  return;
}

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

  for (int i = 0; i < n; ++i) {
    char command[7];
    scanf("%s", command);
    char c[L];
    scanf("%s", c);
    if (command[0] == 'i')
      insert(c);
    if (command[0] == 'f') {
      if (find(c)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
using namespace std;

bool hash[1024 * 1024 * 16];

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

unsigned int makeHash(char str[]) {
  unsigned int ret = 0;
  for (int i = 0; i < strlen(str); i++) {
    ret <<= 2;
    ret += trans(str[i]);
  }
  return ret;
}

int main() {
  int n;
  memset(hash, 0, sizeof(hash));
  cin >> n;
  while (n--) {
    char command[16], word[16];
    cin >> command >> word;
    if (command[0] == 'i') {
      hash[makeHash(word)] = true;
    } else {
      cout << (hash[makeHash(word)] ? "yes" : "no") << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;

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

char dic[1000000][16];

bool binarySearch(int l, int r, char *v) {
  if (l > r)
    return false;
  int c = (l + r) / 2;
  int cmp = strcmp(v, dic[c]);
  if (cmp == 0) {
    return true;
  } else if (cmp > 0) {
    return binarySearch(c + 1, r, v);
  } else {
    return binarySearch(l, c - 1, v);
  }
}

int main() {
  char command[8];
  int n, index = 0;
  cin >> n;
  while (n--) {
    cin >> command;
    char word[16];
    cin >> word;
    if (command[0] == 'i') {
      int j = 0;
      for (; j < index; j++) {
        if (strcmp(word, dic[j]) < 0)
          break;
      }
      for (int i = index - 1; i >= j; i--) {
        strcpy(dic[i + 1], dic[i]);
      }
      strcpy(dic[j], word);
      index++;
    } else {
      cout << (binarySearch(0, index - 1, word) ? "yes" : "no") << endl;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define fst first
#define snd second

int main() {
  std::ios_base::sync_with_stdio(false);
  set<string> dict;
  int N;
  cin >> N;

  REP(i, N) {
    string c, str;
    cin >> c >> str;
    if (c == "insert") {
      dict.insert(str);
    } else if (c == "find") {
      cout << (dict.find(str) != dict.end() ? "yes" : "no") << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define fst first
#define snd second

int main() {
  set<string> dict;
  int N;
  scanf("%d", &N);

  REP(i, N) {
    string c, str;
    cin >> c >> str;
    if (c == "insert") {
      dict.insert(str);
    } else if (c == "find") {
      printf(dict.find(str) != dict.end() ? "yes\\n" : "no\\n");
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: 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;
    } 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;
    }
  }
}


Error: TLE

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;
    }
  }
  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, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

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

  int n = 0;
  cin >> n;

  set<string> dic;

  for (int i = 0; i < n; i++) {
    string buf1, buf2;
    cin >> buf1 >> buf2;
    if (buf1 == "insert") {
      dic.insert(buf2);
    } else {
      if (dic.find(buf2) != dic.end()) {
        cout << "yes" << '\\n';
      } else {
        cout << "no" << '\\n';
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

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

  set<string> dic;

  for (int i = 0; i < n; i++) {
    string buf1, buf2;
    cin >> buf1 >> buf2;
    if (buf1.substr(0, 1) == "i") {
      dic.insert(buf2);
    } else {
      if (dic.find(buf2) != dic.end()) {
        cout << "yes" << '\\n';
      } else {
        cout << "no" << '\\n';
      }
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
#include <string.h>
int n, r[128]; // ASCII文字コードに対応させる
bool used[22370000];
char com[7], s[13];

int main() {
  scanf("%d", &n);
  r[65] = 1;
  r[67] = 2;
  r[71] = 3;
  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]];
    h += 1 << (2 * l);
    if (com[0] == 105)
      used[h] = true;
    else
      puts(used[h] ? "yes" : "no");
  }
}


Error: TLE

Fixed submission:

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

class Dictionary {
  vector<string> str;

public:
  void insert(string s);
  string find(string s);
};

void Dictionary::insert(string s) { str.push_back(s); }

string Dictionary::find(string s) {
  for (auto &e : str) {
    if (e == s) {
      return "yes";
    }
  }
  return "no";
}

int main() {
  int n;
  char ch[7];
  vector<string> ans;
  Dictionary dic;

  cin >> n;
  for (int i = 0; i < n; i++) {
    string str;
    cin >> ch;
    cin >> str;
    if (ch[0] == 'i')
      dic.insert(str);
    else if (ch[0] == 'f') {
      string s = dic.find(str);
      ans.push_back(s);
    }
  }

  for (auto &e : ans) {
    cout << e << endl;
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <map>
using namespace std;
int main() {
  map<string, bool> dict;
  int n;
  string x, y;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (x == "insert")
      dict[y] = 1;
    else {
      if (dict[y] == 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<string> dictionary;
  string x, y;
  int n;
  cin >> n;
  dictionary.resize(n);
  bool check;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (x == "insert")
      dictionary.push_back(y);
    if (x == "find") {
      for (int j = 0; j < dictionary.size(); j++) {
        check = false;
        if (dictionary[j] == y) {
          cout << "yes" << endl;
          check = true;
          break;
        }
      }
      if (check == false) {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}