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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int s[10003], t[5003];
  int n, q;
  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;
  REP(i, q) cin >> t[i];
  int ct = 0;
  REP(i, q) {
    int l = 0;
    int r = n;
    while (abs(r - l) > 1) {
      int mid = (r + l) / 2;
      if (t[i] < s[mid])
        r = mid;
      else
        l = mid;
    }
    if (s[(r + l) / 2] == t[i])
      ct++;
  }
  cout << ct << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); i--)
#define ll long long
#define ull unsigned long long
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int s[100003], t[50003];
  int n, q;
  cin >> n;
  REP(i, n) cin >> s[i];
  cin >> q;
  REP(i, q) cin >> t[i];
  int ct = 0;
  REP(i, q) {
    int l = 0;
    int r = n;
    while (abs(r - l) > 1) {
      int mid = (r + l) / 2;
      if (t[i] < s[mid])
        r = mid;
      else
        l = mid;
    }
    if (s[(r + l) / 2] == t[i])
      ct++;
  }
  cout << ct << endl;
  return 0;
}
Problem p02268 (C++)

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

int binarySearch(int n, int s[], int t) {
  int i = 0;
  int j = n - 1;
  while (i <= j) {
    int mid = i + (j - i) / 2;
    int guess = s[mid];
    if (guess == t) {
      return 1;
    }
    if (guess < t) {
      i = mid + 1;
    } else {
      j = mid - 1;
    }
  }
  return 0;
}

int main() {
  int n, q, s[10001], t, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    ans += binarySearch(n, s, t);
  }
  cout << ans << endl;
  return 0;
}


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

int binarySearch(int n, int s[], int t) {
  int i = 0;
  int j = n - 1;
  while (i <= j) {
    int mid = i + (j - i) / 2;
    int guess = s[mid];
    if (guess == t) {
      return 1;
    }
    if (guess < t) {
      i = mid + 1;
    } else {
      j = mid - 1;
    }
  }
  return 0;
}

int main() {
  int n, q, s[100000], t, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> t;
    ans += binarySearch(n, s, t);
  }
  cout << ans << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> lake;

int main(void) {
  int n, q;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cin >> q;
  vector<int> t(q);
  for (int i = 0; i < q; i++)
    cin >> t[i];
  // sort(s.begin(), s.end());
  // sort(t.begin(), t.end());
  int count = 0;
  for (int i = 0; i < q; i++) {
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (s[mid] == t[i]) {
        count++;
        break;
      } else if (s[mid] < t[i]) {
        left = mid;
      } else {
        right = mid;
      }
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> lake;

int main(void) {
  int n, q;
  cin >> n;
  vector<int> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  cin >> q;
  vector<int> t(q);
  for (int i = 0; i < q; i++)
    cin >> t[i];
  // sort(s.begin(), s.end());
  // sort(t.begin(), t.end());
  int count = 0;
  for (int i = 0; i < q; i++) {
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (s[mid] == t[i]) {
        count++;
        break;
      } else if (s[mid] < t[i]) {
        left = mid + 1;
      } else {
        right = mid;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02268 (C++)

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

int main() {
  int n, q;

  cin >> n;
  int S[n];
  for (int k = 0; k < n; k++) {
    cin >> S[k];
  }
  cin >> q;
  int T[q];
  for (int l = 0; l < q; l++) {
    cin >> T[l];
  }

  int count = 0;
  for (int j = 0; j < q; j++) {
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (S[mid] == T[j]) {
        count += 1;
      } else if (T[j] < S[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }

  cout << count << endl;

  return 0;
}

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

int main() {
  int n, q;

  cin >> n;
  int S[n];
  for (int k = 0; k < n; k++) {
    cin >> S[k];
  }
  cin >> q;
  int T[q];
  for (int l = 0; l < q; l++) {
    cin >> T[l];
  }

  int count = 0;
  for (int j = 0; j < q; j++) {
    int left = 0;
    int right = n;
    while (left < right) {
      int mid = (left + right) / 2;
      if (S[mid] == T[j]) {
        count += 1;
        break;
      } else if (T[j] < S[mid]) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

using namespace std;

bool binary_search(vector<int> v, int kay, bool is_sorted) {
  if (is_sorted)
    sort(ALL(v));

  int left = 0;
  int right = SZ(v);
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (v[mid] == kay)
      return true;
    else if (kay < v[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

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

  int n;
  cin >> n;

  vector<int> v;
  int t;
  REP(_, n) {
    cin >> t;
    v.PB(t);
  }

  cin >> n;
  int count = 0;
  REP(_, n) {
    cin >> t;
    if (binary_search(v, t, true))
      count++;
  }

  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

using namespace std;

bool binary_search(vector<int> v, int kay, bool is_sorted) {
  if (!is_sorted)
    sort(ALL(v));

  int left = 0;
  int right = SZ(v);
  int mid;
  while (left < right) {
    mid = (left + right) / 2;
    if (v[mid] == kay)
      return true;
    else if (kay < v[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

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

  int n;
  cin >> n;

  vector<int> v;
  int t;
  REP(_, n) {
    cin >> t;
    v.PB(t);
  }

  cin >> n;
  int count = 0;
  REP(_, n) {
    cin >> t;
    if (binary_search(v, t, true))
      count++;
  }

  cout << count << endl;
}
Problem p02268 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, q, S[10000], T, res = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  sort(S, S + n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (binary_search(S, S + n, T))
      res++;
  }
  cout << res << endl;
  return 0;
}

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

int main() {
  int n, q, S[100000], T, res = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  sort(S, S + n);
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T;
    if (binary_search(S, S + n, T))
      res++;
  }
  cout << res << endl;
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
using namespace std;

int A[100000];

int search(int key, int n) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) { //??????????????£?????????notfound
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (key < A[mid]) { // motto left
      right = mid;
    } else {
      left = mid;
    }
  }
  return 0;
}

int main() {
  int n, q, key;
  int cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(key, n))
      cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;

int A[100000];

int search(int key, int n) {
  int left = 0;
  int right = n;
  int mid;
  while (left < right) { //??????????????£?????????notfound
    mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (key < A[mid]) { // motto left
      right = mid;
    } else {
      left = mid + 1; //?????¶???????????????????
    }
  }
  return 0;
}

int main() {
  int n, q, key;
  int cnt = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d", &key);
    if (search(key, n))
      cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
Problem p02268 (C++)

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

bool binarySearch(int a[], int n, int x) {
  int mid;
  int left = 0;
  int right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == x)
      return true;
    else if (x < a[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  int n, q, sum = 0;
  int S[10001], T[10001];

  /* input */

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> S[i];
  }

  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> T[i];
  }

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i])) {
      sum++;
    }
  }

  cout << sum << "\n";

  return 0;
}


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

bool binarySearch(int a[], int n, int x) {
  int mid;
  int left = 0;
  int right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (a[mid] == x)
      return true;
    else if (x < a[mid])
      right = mid;
    else
      left = mid + 1;
  }
  return false;
}

int main(void) {
  int n, q, sum = 0;
  int S[100001], T[50001];

  /* input */

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> S[i];
  }

  std::cin >> q;
  for (int i = 0; i < q; i++) {
    std::cin >> T[i];
  }

  for (int i = 0; i < q; i++) {
    if (binarySearch(S, n, T[i])) {
      sum++;
    }
  }

  cout << sum << "\n";

  return 0;
}
Problem p02268 (C++)

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

int main(void) {
  int ans = 0;
  int n, q;
  std::cin >> n;
  std::vector<int> arr(n);
  for (auto &a : arr)
    std::cin >> a;
  std::cin >> q;
  for (size_t i = 0; i < q; i++) {
    int in = 0;
    std::cin >> in;
    int left = 0, mid = n / 2, right = n;
    while (left != right) {
      if (arr[mid] == in) {
        ans++;
        break;
      }
      if (in < arr[mid])
        right = mid;
      else
        left = mid;

      mid = (right + left) / 2;
    }
  }

  std::cout << ans << std::endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <map>
#include <set>
#include <vector>

int main(void) {
  int ans = 0;
  int n, q;
  std::cin >> n;
  std::vector<int> arr(n);
  for (auto &a : arr)
    std::cin >> a;
  std::cin >> q;
  for (size_t i = 0; i < q; i++) {
    int in = 0;
    std::cin >> in;
    int left = 0, mid = n / 2, right = n;
    while (left != right) {
      if (arr[mid] == in) {
        ans++;
        break;
      }
      if (in < arr[mid])
        right = mid;
      else
        left = mid + 1;

      mid = (right + left) / 2;
    }
  }

  std::cout << ans << std::endl;
  return 0;
}
Problem p02268 (C++)

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

int A[1000000], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key > A[mid])
      left = mid + 1;
    else if (key < A[mid])
      right = mid;
  }
  return 0;
}

int main(void) {
  int i, q, k, sum = 0;

  scanf("%d", &n);
  for (i = 0; k < n; i++) {
    scanf("%d", &A[i]);
  }

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}

Fixed submission:
#include <stdio.h>

int A[1000000], n;

int binarySearch(int key) {
  int left = 0;
  int right = n;
  int mid;

  while (left < right) {
    mid = (left + right) / 2;
    if (key == A[mid])
      return 1;
    if (key > A[mid])
      left = mid + 1;
    else if (key < A[mid])
      right = mid;
  }
  return 0;
}

int main(void) {
  int i, q, k, sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &A[i]);
  }

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &k);
    if (binarySearch(k))
      sum++;
  }
  printf("%d\n", sum);
  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
inline int f(int n, vi s, int q, vi t) {
  int cnt = 0;
  rep(j, 0, q) {
    if (count(all(s), t[j]))
      ++cnt;
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  unordered_set<int> ss;
  {
    ll t;
    rep(i, 0, n) {
      cin >> t;
      ss.emplace(t);
    }
  }
  vi s(all(ss));
  int q;
  cin >> q;
  vi t(q);
  rep(i, 0, q) cin >> t[i];
  P(f(len(s), s, q, t));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
#define P(x) cout << (x) << endl
#define p(x) cout << (x)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define vv(type, c, m, n, i) vector<vector<type>> c(m, vector<type>(n, i));
#define rep(i, a, n) for (int i = (a), i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, n) for (int i = (a); i > n; --i)
#define len(x) ((int)(x).size())
#define mp make_pair
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
inline int f(int n, vi s, int q, vi t) {
  int cnt = 0;
  rep(j, 0, q) {
    if (find(all(s), t[j]) != s.end())
      ++cnt;
  }
  return cnt;
}
int main() {
  int n;
  cin >> n;
  unordered_set<int> ss;
  {
    ll t;
    rep(i, 0, n) {
      cin >> t;
      ss.emplace(t);
    }
  }
  vi s(all(ss));
  int q;
  cin >> q;
  vi t(q);
  rep(i, 0, q) cin >> t[i];
  P(f(len(s), s, q, t));
  return 0;
}
Problem p02268 (C++)

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

using namespace std;

int main() {
  int S[100000], T[100000], n, q, key, low = 0, upper = 0, mid, flag;
  int i = 0, cnt = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  for (i = 0; i < q; i++) {
    ///------------------------------
    key = T[i];
    low = 0;
    upper = 0;
    mid = 0;
    upper = n; // n-1

    mid = (low + upper) / 2;
    while (low <= upper) {
      // mid=(low+upper)/2;

      if (S[mid] == key) {

        flag = 1;
        cnt++;
        break;
      }

      if (S[mid] < key) {
        low = mid + 1;
      } else {
        upper = mid; // mid-1
      }
      mid = (low + upper) / 2; //
    }
    if (flag != 1) {
      // no
    }

    ///------------------------------
  }

  cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int S[100000], T[100000], n, q, key, low = 0, upper = 0, mid, flag;
  int i = 0, cnt = 0;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
  }

  for (i = 0; i < q; i++) {
    ///------------------------------
    key = T[i];
    low = 0;
    upper = 0;
    mid = 0;
    upper = n; // n-1

    mid = (low + upper) / 2;
    while (low < upper) {
      // mid=(low+upper)/2;

      if (S[mid] == key) {

        flag = 1;
        cnt++;
        break;
      }

      if (S[mid] < key) {
        low = mid + 1;
      } else {
        upper = mid; // mid-1
      }
      mid = (low + upper) / 2; //
    }
    if (flag != 1) {
      // no
    }

    ///------------------------------
  }

  cout << cnt << endl;

  return 0;
}
Problem p02268 (C++)

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

int binarySearch(int s[], int n, int key) {

  int head = 0;
  int tail = n - 1;

  int center;

  while (head != tail) {
    center = (head + tail) / 2;
    if (s[center] == key) {
      return center;
    } else if (s[center] < key) {
      head = center + 1;
    } else {
      tail = center - 1;
    }
  }

  if (s[head] == key) {
    return head;
  }

  return -1;
}

int main() {

  int n, q;
  int cnt = 0;

  std::cin >> n;

  int s[n];

  for (int i = 0; i < n; i++) {
    std::cin >> s[i];
  }

  std::cin >> q;

  int key;

  for (int i = 0; i < q; i++) {
    std::cin >> key;

    if (binarySearch(s, n, key) >= 0) {
      cnt++;
    }
  }

  std::cout << cnt << "\n";
}


Fixed submission:
#include <iostream>

int binarySearch(int s[], int n, int key) {

  int head = 0;
  int tail = n - 1;

  int center;

  while (head != tail) {
    center = (head + tail) / 2;
    if (s[center] == key) {
      return center;
    } else if (s[center] < key) {
      head = center + 1;
    } else {
      tail = center;
    }
  }

  if (s[head] == key) {
    return head;
  }

  return -1;
}

int main() {

  int n, q;
  int cnt = 0;

  std::cin >> n;

  int s[n];

  for (int i = 0; i < n; i++) {
    std::cin >> s[i];
  }

  std::cin >> q;

  int key;

  for (int i = 0; i < q; i++) {
    std::cin >> key;

    if (binarySearch(s, n, key) >= 0) {
      cnt++;
    }
  }

  std::cout << cnt << "\n";
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define MAXN 200000 + 10
using namespace std;
int s[MAXN];
int t[MAXN];
int ns, nt;
int tot, l, r, mid;
int main() {
  int i, now;
  cin >> ns;
  for (i = 0; i < ns; ++i)
    cin >> s[i];
  cin >> nt;
  for (i = 0; i < nt; ++i)
    cin >> t[i];

  for (i = 0; i < nt; ++i) {
    now = t[i];
    l = 0;
    r = ns;
    while (l < r) {
      mid = l + (r - l) / 2;
      if (s[mid] == now) {
        tot++;
        break;
      }
      if (s[mid] < now)
        l = mid;
      else
        r = mid;
    }
  }
  cout << tot << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#define MAXN 200000 + 10
using namespace std;
int s[MAXN];
int t[MAXN];
int ns, nt;
int tot, l, r, mid;
int main() {
  int i, now;
  cin >> ns;
  for (i = 0; i < ns; ++i)
    cin >> s[i];
  cin >> nt;
  for (i = 0; i < nt; ++i)
    cin >> t[i];

  for (i = 0; i < nt; ++i) {
    now = t[i];
    l = 0;
    r = ns;
    while (l < r) {
      mid = l + (r - l) / 2;
      if (s[mid] == now) {
        tot++;
        break;
      }
      if (s[mid] < now)
        l = mid + 1;
      else
        r = mid;
    }
  }
  cout << tot << endl;
  return 0;
}
Problem p02268 (C++)

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

#define MAXN 100000
#define MAXQ 50000

using namespace std;

int n, q;
int S[MAXN + 1], T[MAXQ + 1];
int cnt = 0;

bool binarySearch(int num) {
  int lo, mi, hi;
  bool res = false;
  lo = 0;
  mi = hi = n;
  while (1 < mi) {
    mi = (lo + hi) / 2;
    if (num < S[mi]) {
      hi = mi - 1;
    } else if (S[mi] < num) {
      lo = mi + 1;
    } else {
      res = true;
      break;
    }
  }

  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
    cnt += binarySearch(T[i]);
  }
  cout << cnt << endl;

  return 0;
}

Fixed submission:
#include <iostream>

#define MAXN 100000
#define MAXQ 50000

using namespace std;

int n, q;
int S[MAXN + 1], T[MAXQ + 1];
int cnt = 0;

bool binarySearch(int num) {
  int lo, mi, hi;
  bool res = false;
  lo = 0;
  mi = hi = n;
  while (lo <= hi) {
    mi = (lo + hi) / 2;
    if (num < S[mi]) {
      hi = mi - 1;
    } else if (S[mi] < num) {
      lo = mi + 1;
    } else {
      res = true;
      break;
    }
  }

  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> T[i];
    cnt += binarySearch(T[i]);
  }
  cout << cnt << endl;

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left <= right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);
    if (is_find(x))
      num++;
  }
  printf("%d\n", num);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 8;
int n, q, x, num;
int s[maxn];

bool is_find(int x) {
  int left, right, mid;
  left = 0;
  right = n;
  while (left < right) {
    mid = (left + right) / 2;
    if (s[mid] == x)
      return true;
    else if (s[mid] > x) {
      right = mid;
    } else if (s[mid] < x) {
      left = mid + 1;
    }
  }
  return false;
}
int main() {
  scanf("%d", &n);
  num = 0;
  for (int i = 0; i < n; ++i)
    scanf("%d", &s[i]);
  sort(s, s + n);
  scanf("%d", &q);
  for (int i = 0; i < q; ++i) {
    scanf("%d", &x);
    if (is_find(x))
      num++;
  }
  printf("%d\n", num);
  return 0;
}
Problem p02268 (C++)

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

using namespace std;

bool find(const vector<int> &S, int begin, int end, int x) {
  if (end - begin <= 0) {
    return false;
  }
  int mid = (begin + end) / 2;
  if (x < S[mid]) {
    return find(S, begin, mid, x);
  } else if (S[mid] < x) {
    return find(S, mid, end, x);
  } else {
    return true;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }

  int q;
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }

  int count = 0;
  for (int i = 0; i < q; ++i) {
    if (find(S, 0, S.size(), T[i])) {
      ++count;
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

bool find(const vector<int> &S, int begin, int end, int x) {
  if (end - begin == 1) {
    return S[begin] == x;
  }
  int mid = (begin + end) / 2;
  if (x < S[mid]) {
    return find(S, begin, mid, x);
  } else if (S[mid] < x) {
    return find(S, mid, end, x);
  } else {
    return true;
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> S(n);
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }

  int q;
  cin >> q;
  vector<int> T(q);
  for (int i = 0; i < q; ++i) {
    cin >> T[i];
  }

  int count = 0;
  for (int i = 0; i < q; ++i) {
    if (find(S, 0, S.size(), T[i])) {
      ++count;
    }
  }
  cout << count << endl;
}
Problem p02268 (C++)

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

int binarySearch(int A[], int n, int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (A[mid] < key)
      left = mid + 1;
    else
      right = mid;
  }
  return 0;
}

int main() {
  int i, n, A[10000 + 1], q, key, sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(A, n, key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}


Fixed submission:
#include <stdio.h>

int binarySearch(int A[], int n, int key) {
  int left = 0;
  int right = n;
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key)
      return 1;
    else if (A[mid] < key)
      left = mid + 1;
    else
      right = mid;
  }
  return 0;
}

int main() {
  int i, n, A[100000 + 1], q, key, sum = 0;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d", &key);
    if (binarySearch(A, n, key))
      sum++;
  }
  printf("%d\n", sum);

  return 0;
}
Problem p02268 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int NS;
int S[MAX_N];
int NT;
int T[MAX_N];

// not the most left
int binarySearch(int A[], int N, int key) {
  int left = 0;
  int right = N;
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key) {
      return mid;
    } else if (key < A[mid]) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return -1;
}

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  int ret = 0;
  rep(i, NT) {
    if (binarySearch(S, NS, T[i]) >= 0)
      ret++;
  }
  cout << ret << endl;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, n) FOR(i, 0, n)
#define DEBUG(x) cout << #x << ": " << x << endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include <map>
#include <queue>
#include <set>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
int NS;
int S[MAX_N];
int NT;
int T[MAX_N];

// not the most left
int binarySearch(int A[], int N, int key) {
  int left = 0;
  int right = N;
  while (left < right) {
    int mid = (left + right) / 2;
    if (A[mid] == key) {
      return mid;
    } else if (key < A[mid]) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return -1;
}

int main() {

  cin >> NS;
  rep(i, NS) { cin >> S[i]; }
  cin >> NT;
  rep(i, NT) { cin >> T[i]; }

  int ret = 0;
  rep(i, NT) {
    if (binarySearch(S, NS, T[i]) >= 0)
      ret++;
  }
  cout << ret << endl;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define M 104627
#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[1]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

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

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

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

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#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[1]) {
  long long sum = 0, p = 1, i;
  for (i = 0; i < strlen(str); i++) {
    sum += p * (getChar(str[i]));
    p *= 5;
  }
  return sum;
}

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

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

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

int main() {
  int i, n, h;
  char str[L], com[9];
  for (i = 0; i < M; i++)
    H[i][0] = '\0';
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s %s", com, str);

    if (com[0] == 'i') {
      insert(str);
    } else {
      if (find(str)) {
        printf("yes\n");
      } else {
        printf("no\n");
      }
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>
#include <string.h>
using namespace std;

#define LIM 519430400
#define LEN 13

char H[LIM][LEN];

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

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

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

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

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

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

  return 0;
}


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

#define LIM 519430400
#define LEN 13

char H[LIM][LEN];

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

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

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

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

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

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

  return 0;
}
Problem p02269 (C++)

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

#define TABLE_SIZE 16777216
#define ORDER_LENGTH 7
#define STR_LENGTH 13

using namespace std;

int translateChar(char ch) {
  int ret;
  switch (ch) {
  case 'A':
    ret = 1;
    break;
  case 'C':
    ret = 2;
    break;
  case 'G':
    ret = 3;
    break;
  case 'T':
    ret = 4;
    break;
  }
  return ret;
}

int calc(int num) {
  if (num == 0) {
    return 1;
  } else {
    return 4 * calc(num - 1);
  }
}

int translateStr(char buf[]) {
  int sum = 0;
  for (int i = 0; buf[i] != '\0'; i++) {
    sum += translateChar(buf[i]) * calc(i);
  }
  return sum - 1;
}

int main() {

  queue<string> Messages;
  int n;
  scanf("%d", &n);
  char order[ORDER_LENGTH], str[STR_LENGTH];

  char *check_table = new char[n];

  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, str);
    if (strcmp(order, "insert") == 0) {
      check_table[translateStr(str)] = 'Y';
    } else {
      if (check_table[translateStr(str)] == 'Y') {
        Messages.push("yes");
      } else {
        Messages.push("no");
      }
    }
  }

  while (!Messages.empty()) {
    cout << Messages.front() << endl;
    Messages.pop();
  }

  delete[] check_table;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

#define TABLE_SIZE 16777216
#define ORDER_LENGTH 7
#define STR_LENGTH 13

using namespace std;

int translateChar(char ch) {
  int ret;
  switch (ch) {
  case 'A':
    ret = 1;
    break;
  case 'C':
    ret = 2;
    break;
  case 'G':
    ret = 3;
    break;
  case 'T':
    ret = 4;
    break;
  }
  return ret;
}

int calc(int num) {
  if (num == 0) {
    return 1;
  } else {
    return 4 * calc(num - 1);
  }
}

int translateStr(char buf[]) {
  int sum = 0;
  for (int i = 0; buf[i] != '\0'; i++) {
    sum += translateChar(buf[i]) * calc(i);
  }
  return sum - 1;
}

int main() {

  queue<string> Messages;
  int n;
  scanf("%d", &n);
  char order[ORDER_LENGTH], str[STR_LENGTH];

  char *check_table = new char[TABLE_SIZE];

  for (int i = 0; i < n; i++) {
    scanf("%s %s", order, str);
    if (strcmp(order, "insert") == 0) {
      check_table[translateStr(str)] = 'Y';
    } else {
      if (check_table[translateStr(str)] == 'Y') {
        Messages.push("yes");
      } else {
        Messages.push("no");
      }
    }
  }

  while (!Messages.empty()) {
    cout << Messages.front() << endl;
    Messages.pop();
  }

  delete[] check_table;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):

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

typedef unsigned int U;
#define L 1 << 20
bool HA[L];
char T[256];

int main() {
  U n, s, t;
  char c;
  T['A'] = (char)1, T['C'] = (char)2, T['G'] = (char)3, T['T'] = (char)4;
  scanf("%d", &n);
  while (getchar() != '\n')
    ;
  for (register U i = 0; i++ < n;) {
    s = 0;
    t = 1;
    bool flag = (c = getchar()) == 'i';
    while ((c = getchar()) != ' ')
      ;
    while ((c = T[getchar()]) != 0) {
      s += c * t;
      t *= 4;
    }
    if (flag) {
      HA[s] = true;
    } else {
      puts(HA[s] ? "yes" : "no");
    }
  }
  return 0;
}

Fixed submission:

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

typedef unsigned int U;
#define L 1 << 20 + 1
bool HA[L];
char T[256];

int main() {
  U n, s, t;
  char c;
  T['A'] = (char)1, T['C'] = (char)2, T['G'] = (char)3, T['T'] = (char)4;
  scanf("%d", &n);
  while (getchar() != '\n')
    ;
  for (register U i = 0; i++ < n;) {
    s = 0;
    t = 1;
    bool flag = (c = getchar()) == 'i';
    while ((c = getchar()) != ' ')
      ;
    while ((c = T[getchar()]) != 0) {
      s += c * t;
      t *= 4;
    }
    if (flag) {
      HA[s] = true;
    } else {
      puts(HA[s] ? "yes" : "no");
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  auto encodeString = [](const string &str) {
    auto encodeChar = [](char c) {
      switch (c) {
      case 'A':
        return 1;
      case 'C':
        return 2;
      case 'G':
        return 3;
      case 'T':
        return 4;
      default:
        assert(false);
      }
    };
    auto pow = [](auto b, auto e) {
      int r{1};
      for (auto i = 0; i < e; ++i) {
        r *= b;
      }
      return r;
    };
    vector<char> v;
    transform(begin(str), end(str), back_inserter(v), encodeChar);
    int r{0};
    for (size_t i = 0; i < v.size(); ++i) {
      r += v[i] * pow(5, i);
    }
    return r;
  };

  bitset<244140624 + 1> b;

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string instruction;
    string s;
    cin >> instruction >> s;
    if (instruction == "insert") {
      b[encodeString(s)] = true;
    } else {
      if (b[encodeString(s)]) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  auto encodeString = [](const string &str) {
    auto encodeChar = [](char c) {
      switch (c) {
      case 'A':
        return 1;
      case 'C':
        return 2;
      case 'G':
        return 3;
      case 'T':
        return 4;
      default:
        assert(false);
      }
    };
    auto pow = [](auto b, auto e) {
      int r{1};
      for (auto i = 0; i < e; ++i) {
        r *= b;
      }
      return r;
    };
    vector<char> v;
    transform(begin(str), end(str), back_inserter(v), encodeChar);
    int r{0};
    for (size_t i = 0; i < v.size(); ++i) {
      r += v[i] * pow(5, i);
    }
    return r;
  };
  vector<bool> b(244140624 + 1);

  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string instruction;
    string s;
    cin >> instruction >> s;
    if (instruction == "insert") {
      b[encodeString(s)] = true;
    } else {
      if (b[encodeString(s)]) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
}
Problem p02269 (C++)

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

int main() {
  int n;
  cin >> n;
  set<string> st;
  while (n--) {
    string order;
    string str;

    cin >> order;
    cin >> str;

    if (order == "insert") {
      st.insert(str);
    } else {
      if (st.find(str) != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  set<string> st;
  while (n--) {
    string order;
    string str;

    cin >> order;
    cin >> str;

    if (order == "insert") {
      st.insert(str);
    } else {
      if (st.find(str) != st.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  return 0;
}
Problem p02269 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;

int main() {

  int n;
  cin >> n;
  set<string> s;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    if (s1[0] == 'i')
      s.insert(s2);
    else {
      if (s.find(s2) == s.end())
        printf("no\n");
      else
        printf("yes\n");
    }
  }
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  set<string> s;
  for (int i = 0; i < n; i++) {
    string s1, s2;
    cin >> s1 >> s2;
    if (s1[0] == 'i')
      s.insert(s2);
    else {
      if (s.find(s2) == s.end())
        printf("no\n");
      else
        printf("yes\n");
    }
  }
}
Problem p02269 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 8;
#define MOD 1046527
int n;
char H[maxn][15];

int getChar(char ch) { return ch - 'A' + 1; }
ll getValue(char t[]) {
  int len = strlen(t);
  ll sum = 0, p = 1, i;
  for (i = 0; i < len; ++i) {
    sum += p * getChar(t[i]);
    p *= 5;
  }
  return sum;
}
ll h1(ll k) { return k % MOD; }
ll h2(ll k) { return 1 + (k % (MOD - 1)); }
int solve_insert(char c[]) {
  ll key, i, h;
  key = getValue(c);
  i = 0;
  while (true) {
    h = (h1(key) + i * h2(key)) % MOD;
    if (strcmp(H[h], c) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], c);
      return 0;
    }
    i++;
  }
}
int solve_find(char c[]) {
  ll key, i, h;
  i = 0;
  key = getValue(c);
  while (true) {
    h = (h1(key) + i * h2(key)) % MOD;
    if (strcmp(H[h], c) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      return 0;
    }
    i++;
  }
}
int main() {
  scanf("%d", &n);
  char s[15], t[15];
  for (int i = 0; i < n; ++i) {
    scanf("%s%s", s, t);
    if (s[0] == 'i') {
      solve_insert(t);
    } else {
      if (solve_find(t))
        printf("yes\n");
      else
        printf("no\n");
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn = 1e7 + 8;
#define MOD 1046527
int n;
char H[maxn][15];

int getChar(char ch) { return ch - 'A' + 1; }
ll getValue(char t[]) {
  int len = strlen(t);
  ll sum = 0, p = 1, i;
  for (i = 0; i < len; ++i) {
    sum += p * getChar(t[i]);
    p *= 5;
  }
  return sum;
}
ll h1(ll k) { return k % MOD; }
ll h2(ll k) { return 1 + (k % (MOD - 1)); }
int solve_insert(char c[]) {
  ll key, i, h;
  key = getValue(c);
  i = 0;
  while (true) {
    h = (h1(key) + i * h2(key)) % MOD;
    if (strcmp(H[h], c) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      strcpy(H[h], c);
      return 0;
    }
    i++;
  }
}
int solve_find(char c[]) {
  ll key, i, h;
  i = 0;
  key = getValue(c);
  while (true) {
    h = (h1(key) + i * h2(key)) % MOD;
    if (strcmp(H[h], c) == 0)
      return 1;
    else if (strlen(H[h]) == 0) {
      return 0;
    }
    i++;
  }
}
int main() {
  scanf("%d", &n);
  char s[15], t[15];
  for (int i = 0; i < n; ++i) {
    scanf("%s%s", s, t);
    if (s[0] == 'i') {
      solve_insert(t);
    } else {
      if (solve_find(t))
        printf("yes\n");
      else
        printf("no\n");
    }
  }
  return 0;
}