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

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

int main() {
  int n;
  cin >> n;
  set<string> S;

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


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<string> S;

  string command, str;
  for (int i = 0; i < n; ++i) {
    cin >> command >> str;
    if (command == "insert") {
      S.push_back(str);
    } else if (command == "find") {
      auto itr = find(S.begin(), S.end(), str);
      if (itr == S.end()) {
        cout << "no" << endl;
      } else {
        cout << "yes" << endl;
      }
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

#define L 1000003

string A[L];

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

long long key(string s) {
  long long sum = 0;
  int k = 1;
  for (int i = 0; i < strlen(&s[0]); i++) {
    sum += num(s[i]) * k;
    k *= 5;
  }
  return sum;
}

int hash1(int x) { return x % L; }

int hash2(int x) { return 1 + x % (L - 1); }

void insert(string s) {
  long long k = key(s);
  int h1, h2;
  h1 = hash1(k);
  h2 = hash2(k);
  for (int i = 0;; i++) {
    int h = (h1 + i * h2) % L;
    if (A[h][0] == '\\0') {
      A[h] = s;
      break;
    } else if (A[h] == s)
      break;
  }
}

void find(string s) {
  long long k = key(s);
  int h1, h2;
  h1 = hash1(k);
  h2 = hash2(k);
  for (int i = 0;; i++) {
    int h = (h1 + i * h2) % L;
    if (A[h][0] == '\\0') {
      cout << "no" << endl;
      return;
    }
    if (A[h] == s) {
      cout << "yes" << endl;
      return;
    }
  }
}

int main() {
  for (int i = 0; i < L; i++) {
    A[i] = '\\0';
  }
  string com, s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> com >> s;
    if (com[0] == 'i') {
      insert(s);
    } else {
      find(s);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

#define L 1000003

string A[L];

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

long long key(string s) {
  long long sum = 0;
  int k = 1;
  for (int i = 0; i < strlen(&s[0]); i++) {
    sum += num(s[i]) * k;
    k *= 5;
  }
  return sum;
}

int hash1(int x) { return x % L; }

int hash2(int x) { return 1 + x % (L - 1); }

void insert(string s) {
  long long k = key(s);
  int h1, h2;
  h1 = hash1(k);
  h2 = hash2(k);
  int i = 0;
  while (A[(h1 + i * h2) % L][0] != '\\0') {
    if (A[(h1 + i * h2) % L] == s)
      break;
    i++;
  }
  A[h1 + i * h2] = s;
}

void find(string s) {
  long long k = key(s);
  int h1, h2;
  h1 = hash1(k);
  h2 = hash2(k);
  int i = 0;
  while (A[(h1 + i * h2) % L] != s) {
    if (A[(h1 + i * h2) % L][0] == '\\0') {
      cout << "no" << endl;
      return;
    }
    i++;
  }
  cout << "yes" << endl;
}

int main() {
  for (int i = 0; i < L; i++) {
    A[i] = '\\0';
  }
  string com, s;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> com >> s;
    if (com[0] == 'i') {
      insert(s);
    } else {
      find(s);
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long M = 1046527;
const long NIL = -1;
const long L = 14;

string H[M];

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

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

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

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

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

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

Error: TLE

Fixed submission:

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

long binarySearch(string A[], long n, string key) {
  long left = 0, right = n;
  while (left < right) {
    long 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() {
  long n, p = 0;
  cin >> n;
  string dict[1000000];
  for (long i = 0; i < n; i++) {
    long flag = 0;
    string opecode, str;
    cin >> opecode >> str;
    if (opecode[0] == 'i') { // insert
      dict[p] = str;
      p++;
      sort(dict, dict + p);
    } else { // find
      flag = binarySearch(dict, p, str);
      if (flag != -1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

struct Node {
  string str;
  Node *next;
  Node(const string &str, Node *next) : str(str), next(next) {}
};

const int DICT_SIZE = 1000001;
Node *dict[DICT_SIZE];

inline int tohash(const string &str) {
  int result = 0;
  for (int i = 0; i < str.size(); ++i) {
    result = result * 31 + (str[i]);
    result %= DICT_SIZE;
  }
  return result;
}

int main(void) {

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

  int n;
  cin >> n;

  char command[7];
  string str;
  for (int i = 0; i < n; ++i) {
    cin >> command >> str;
    int hashval = tohash(str);
    //        cout << "[hashval] str = " << str
    //             << ", hashval = " << hashval << "\\n";
    if (command[0] == 'i') {
      if (dict[hashval]) {
        Node *node = dict[hashval];
        while (node->next) {
          node = node->next;
        }
        node->next = new Node(str, NULL);
      } else {
        dict[hashval] = new Node(str, NULL);
      }
    } else {
      bool exist = false;
      for (Node *node = dict[hashval]; node != NULL; node = node->next) {
        if (node->str == str) {
          exist = true;
          break;
        }
      }
      cout << (exist ? "yes" : "no") << '\\n';
    }
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

const int DIC_SIZE = 1000001;
bool dict[DIC_SIZE];

inline int tohash(const string &str) {
  int result = 0;
  for (int i = 0; i < str.size(); ++i) {
    result = result * 31 + (str[i]);
  }
  return result;
}

int main(void) {

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

  int n;
  cin >> n;

  char command[7];
  string str;
  for (int i = 0; i < n; ++i) {
    cin >> command >> str;
    int hashval = tohash(str) % DIC_SIZE;
    //        cout << "[hashval] str = " << str
    //             << ", hashval = " << hashval << "\\n";
    if (command[0] == 'i') {
      dict[tohash(str) % DIC_SIZE] = true;
    } else {
      cout << (dict[tohash(str) % DIC_SIZE] ? "yes" : "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() {
  long long n;
  cin >> n;
  unordered_set<string> dict;
  for (int i = 0; i < n; i++) {
    string method, element;
    cin >> method >> element;
    if (method == "insert") {
      dict.insert(element);
    } else {
      if (dict.count(element) >= 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  long long n;
  cin >> n;
  set<string> dict;
  for (int i = 0; i < n; i++) {
    string method, element;
    cin >> method >> element;
    if (method == "insert") {
      dict.insert(element);
    } else {
      if (dict.count(element) >= 1)
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef std::set<int> TDictionary;

int CharToInt(char c) {
  switch (c) {
  case 'A':
    return 0;
  case 'G':
    return 1;
  case 'C':
    return 2;
  case 'T':
    return 3;
  }
  return 0;
}

int ToInt(std::string String) {
  int Result = 0;
  int Order = 1;
  for (unsigned int i = 0; i < String.size(); i++) {
    Result += CharToInt(String[i]) * Order;
    Order *= 4;
  }
  return Result + Order;
}

std::string GetResultString(bool Result) { return Result ? "yes" : "no"; }

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  TDictionary Dictionary;
  int Count = 0;
  std::cin >> Count;

  for (int i = 0; i < Count; i++) {
    std::string Command;
    std::cin >> Command;
    std::string Input;
    std::cin >> Input;
    if (Command == "insert") {
      Dictionary.insert(ToInt(Input));
    } else if (Command == "find") {
      std::cout << GetResultString(Dictionary.find(ToInt(Input)) !=
                                   Dictionary.end())
                << std::endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef std::vector<int> TDictionary;

int CharToInt(char c) {
  switch (c) {
  case 'A':
    return 0;
  case 'G':
    return 1;
  case 'C':
    return 2;
  case 'T':
    return 3;
  }
  return 0;
}

int ToInt(std::string String) {
  int Result = 0;
  int Order = 1;
  for (unsigned int i = 0; i < String.size(); i++) {
    Result += CharToInt(String[i]) * Order;
    Order *= 4;
  }
  return Result + Order;
}

std::string GetResultString(bool Result) { return Result ? "yes" : "no"; }

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  TDictionary Dictionary;
  int Count = 0;
  std::cin >> Count;

  for (int i = 0; i < Count; i++) {
    std::string Command;
    std::cin >> Command;
    std::string Input;
    std::cin >> Input;
    bool InDictionary =
        std::binary_search(Dictionary.begin(), Dictionary.end(), ToInt(Input));
    if (Command == "insert") {
      if (!InDictionary)
        Dictionary.push_back(ToInt(Input));
      std::sort(Dictionary.begin(), Dictionary.end());
    } else if (Command == "find") {
      std::cout << GetResultString(InDictionary) << std::endl;
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>

using namespace std;

int main() {

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

  set<string> v;
  for (int i = 0; i < n; ++i) {
    char s[10];
    scanf("%s", s);
    char c[3];
    scanf("%s", c);
    string q(c);

    if (!strcmp(s, "insert")) {
      v.insert(q);
    } else {
      set<string>::iterator it = v.find(q);
      if (it != v.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

using namespace std;

int main() {

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

  vector<string> v;
  for (int i = 0; i < n; ++i) {
    char s[10];
    scanf("%s", s);
    char q[3];
    scanf("%s", q);

    if (!strcmp(s, "insert")) {
      v.push_back(q);
    } else {
      auto itr = find(v.begin(), v.end(), q);
      if (itr != v.end()) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }

  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Runtime Error

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

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;
  }
  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);
  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: 0

Fixed submission:

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

int Hash[244140];
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 *= 5;
  }
  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;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  std::ios_base::sync_with_stdio(false);
  set<string> st;
  int n;
  cin >> n;
  while (n--) {
    string BUFF, c;
    cin >> BUFF >> c;
    if (BUFF == "insert")
      st.insert(c);
    else
      cout << (st.find(c) != st.end() ? "yes" : "no") << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  // std::ios_base::sync_with_stdio(false);
  set<string> st;
  int n;
  cin >> n;
  while (n--) {
    string BUFF, c;
    cin >> BUFF >> c;
    if (BUFF == "insert")
      st.insert(c);
    else
      cout << (st.find(c) != st.end() ? "yes" : "no") << endl;
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int a;
  cin >> a;
  set<string> b;
  for (int c = 0; c < a; c++) {
    string d, e;
    cin >> d >> e;
    if (d == "insert")
      b.insert(e);
    else {
      if (b.find(e) == b.end())
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

int main() {
  map<string, int> a;
  int b;
  cin >> b;
  for (int c = 0; c < b; c++) {
    string d;
    string e;
    cin >> d >> e;
    if (d == "insert")
      a[e] = 1;
    else
      (a[e] == 1) ? (cout << "yes" << endl) : (cout << "no" << endl);
  }
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 26 * 12 * 14 * 10;
vector<string> memo[MAX_N];

int Key(string x) {
  int sum = 0;
  for (int i = 0, size = x.size(); i < size; ++i)
    sum = (sum + (i + size) * (int)(x[i] - 'A')) % MAX_N;
  return sum % MAX_N;
}

void Insert(string x) {
  int idx = Key(x);
  memo[idx].emplace_back(x);
}

bool Find(string x) {
  int idx = Key(x);
  for (auto y : memo[idx])
    if (y == x)
      return true;
  return false;
}

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

  int n;
  string s, x;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s >> x;
    if (s == "insert")
      Insert(x);
    else if (s == "find")
      cout << (Find(x) ? "yes\\n" : "no\\n");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAX_N = 26 * 12 * 13;
vector<string> memo[MAX_N];

int Key(string x) {
  int sum = 0;
  for (int i = 0, size = x.size(); i < size; ++i)
    sum = (sum + (i + 1) * (int)(x[i] - 'A')) % MAX_N;
  return sum % MAX_N;
}

void Insert(string x) {
  int idx = Key(x);
  memo[idx].emplace_back(x);
}

bool Find(string x) {
  int idx = Key(x);
  for (auto y : memo[idx])
    if (y == x)
      return true;
  return false;
}

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

  int n;
  string s, x;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> s >> x;
    if (s == "insert")
      Insert(x);
    else if (s == "find")
      cout << (Find(x) ? "yes\\n" : "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() {
  int n;
  cin >> n;
  string operation, target;
  typedef unordered_set<string> set_t;
  set_t dict;
  for (int i = 0; i < n; i++) {
    cin >> operation >> target;
    if (operation == "insert") {
      dict.insert(target);
    } else {
      if (dict.count(target) == 0)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n;
  cin >> n;
  string operation, target;
  typedef set<string> set_t;
  set_t dict;
  for (int i = 0; i < n; i++) {
    cin >> operation >> target;
    if (operation == "insert") {
      dict.insert(target);
    } else {
      if (dict.count(target) == 0)
        cout << "no" << endl;
      else
        cout << "yes" << endl;
    }
  }
}
Problem p02269 (C++) — buggy submission, verdict: 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;
int main() {
  int n;
  cin >> n;
  set<string> ss;
  string s1, s2;
  rep(i, 0, n) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      ss.emplace(s2);
    } else {
      P(ss.find(s2) != ss.end() ? "yes" : "no");
    }
  }
  return 0;
}

Error: TLE

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;
int main() {
  int n;
  cin >> n;
  vs v(n);
  string s1, s2;
  rep(i, 0, n) {
    cin >> s1 >> s2;
    if (s1 == "insert") {
      v.eb(s2);
    } else {
      P(find(all(v), s2) != v.end() ? "yes" : "no");
    }
  }
  return 0;
}
Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_C&lang=jp*/

#include <cmath>
#include <iostream>

#define MAX 1000000

#define hash(key, p) ((key) % (p))
#define hash_a(key) hash(key, MAX)
#define hash_b(key) (hash(key, MAX - 1) + 1)
#define myhash(key, n) hash_a(hash_a(key) + (n)*hash_b(key))

using namespace std;

string s[MAX];

int getKey(string value) {
  int key = 0;
  for (int i = 0; i < value.length(); i++) {
    switch (value[i]) {
    case 'A':
      key += pow(5, i) * 1;
      break;
    case 'C':
      key += pow(5, i) * 2;
      break;
    case 'G':
      key += pow(5, i) * 3;
      break;
    case 'T':
      key += pow(5, i) * 4;
      break;
    }
  }
  return key;
}

bool insert(string value) {
  int key = getKey(value);
  for (int j = 0; j < MAX; j++) {
    int h = myhash(key, j);
    if (s[h] == "") {
      s[h] = value;
      return true;
    } else if (s[h] == value) {
      return false;
    }
  }
}

bool search(string value) {
  int key = getKey(value);
  for (int j = 0; true; j++) {
    int h = myhash(key, j);
    if (j > MAX + 1 || s[h] == "") {
      return false;
    } else if (s[h] == value) {
      return true;
    }
  }
}

int main() {
  int n;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ins;
    string value;
    cin >> ins >> value;
    if (ins == "insert") {
      insert(value);
    } else if (search(value)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

/*http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_4_C&lang=jp*/

#include <iostream>

#define MAX 1000000

#define hash(key, p) ((key) % (p))
#define hash_a(key) hash(key, MAX)
#define hash_b(key) (hash(key, MAX - 1) + 1)
#define myhash(key, n) hash_a(hash_a(key) + (n)*hash_b(key))

using namespace std;

string s[MAX];

int getKey(string value) {
  int key = 0;
  for (int i = 0; i < value.length(); i++) {
    switch (value[i]) {
    case 'A':
      key += 1;
      break;
    case 'C':
      key += 2;
      break;
    case 'G':
      key += 3;
      break;
    case 'T':
      key += 4;
      break;
    }
  }
  return key;
}

bool insert(string value) {
  int key = getKey(value);
  for (int j = 0; j < MAX; j++) {
    int h = myhash(key, j);
    if (s[h] == "") {
      s[h] = value;
      return true;
    } else if (s[h] == value) {
      return false;
    }
  }
}

bool search(string value) {
  int key = getKey(value);
  for (int j = 0; true; j++) {
    int h = myhash(key, j);
    if (j > MAX + 1 || s[h] == "") {
      return false;
    } else if (s[h] == value) {
      return true;
    }
  }
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ins;
    string value;
    cin >> ins;
    cin >> value;
    if (ins == "insert") {
      insert(value);
    } else if (search(value)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}