Problem p02269 (C++) — buggy submission, verdict: Time Limit Exceeded

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
#define y0 qvya13579
#define y1 qvyb24680
#define j0 qvja13579
#define j1 qvjb24680
#define next qvne13579xt
#define prev qvpr13579ev
#define INF 1000000007
#define PI acos(-1.0)
#define endl "\\n"
#define IOS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define M_P make_pair
#define PU_B push_back
#define PU_F push_front
#define PO_B pop_back
#define PO_F pop_front
#define U_B upper_bound
#define L_B lower_bound
#define B_S binary_search
#define PR_Q priority_queue
#define FIR first
#define SEC second
#if __cplusplus < 201103L
#define stoi(argument_string) atoi((argument_string).c_str())
#endif
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i)
#define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SIZ(x) ((int)(x).size())
#define COUT(x) cout << (x) << endl
#define CIN(x) cin >> (x)
#define CIN2(x, y) cin >> (x) >> (y)
#define CIN3(x, y, z) cin >> (x) >> (y) >> (z)
#define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w)
#define SCAND(x) scanf("%d", &(x))
#define SCAND2(x, y) scanf("%d%d", &(x), &(y))
#define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w))
#define SCANLLD(x) scanf("%lld", &(x))
#define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y))
#define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z))
#define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w))
#define PRINTD(x) printf("%d\\n", (x))
#define PRINTLLD(x) printf("%lld\\n", (x))
typedef long long int lli;
using namespace std;

bool compare_by_2nd(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int ctoi(char c) {
  if (c >= '0' and c <= '9') {
    return (int)(c - '0');
  }

  return -1;
}

int alphabet_pos(char c) {
  if (c >= 'a' and c <= 'z') {
    return (int)(c - 'a');
  }

  return -1;
}

int alphabet_pos_capital(char c) {
  if (c >= 'A' and c <= 'Z') {
    return (int)(c - 'A');
  }

  return -1;
}

vector<string> split(string str, char ch) {
  int first = 0;
  int last = str.find_first_of(ch);

  if (last == string::npos) {
    last = SIZ(str);
  }

  vector<string> result;

  while (first < SIZ(str)) {
    string Ssubstr(str, first, last - first);
    result.push_back(Ssubstr);
    first = last + 1;
    last = str.find_first_of(ch, first);

    if (last == string::npos) {
      last = SIZ(str);
    }
  }

  return result;
}

int gcd(int a, int b) // assuming a,b >= 1
{
  if (a < b) {
    return gcd(b, a);
  }

  if (a % b == 0) {
    return b;
  }

  return gcd(b, a % b);
}

int lcm(int a, int b) // assuming a,b >= 1
{
  return a * b / gcd(a, b);
}

/*------------------ the end of the template -----------------------*/

#define M 1046527

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

lli getkey(string str) {
  lli sm = 0;
  lli p = 1;

  REP(i, SIZ(str)) {
    sm += (alphabet_pos_capital(str[i]) + 1) * p;
    p *= 3;
  }

  return sm;
}

int indgen(string str, lli i) {
  lli temp = getkey(str);

  return ((temp % M) + i * ((temp % (M - 1)) + 1)) % M;
}

int insert(string str, vector<string> &v) {
  lli i = 0;
  int h;

  for (i = 0; i < M; ++i) {
    h = indgen(str, i);
    if (v[h] == str) {
      return 1;
    } else if (SIZ(v[h]) == 0) {
      v[h] = str;
      return 0;
    }
  }
}

int find(string str, vector<string> &v) {
  lli i = 0;
  int h;

  for (i = 0; i < M; ++i) {
    h = indgen(str, i);

    if (v[h] == str) {
      return 1;
    } else if (SIZ(v[h]) == 0) {
      return 0;
    }
  }
}

int main() {
  IOS; /* making cin faster */
  vector<string> v(M);
  int n;

  CIN(n);

  string com, st;

  REP(i, n) {
    CIN2(com, st);

    if (com == "insert") {
      insert(st, v);

    } else {
      if (find(st, v)) {
        printf("yes\\n");

      } else {
        printf("no\\n");
      }
    }
  }
}


Error: TLE

Fixed submission:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
#define y0 qvya13579
#define y1 qvyb24680
#define j0 qvja13579
#define j1 qvjb24680
#define next qvne13579xt
#define prev qvpr13579ev
#define INF 1000000007
#define PI acos(-1.0)
#define endl "\\n"
#define IOS                                                                    \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define M_P make_pair
#define PU_B push_back
#define PU_F push_front
#define PO_B pop_back
#define PO_F pop_front
#define U_B upper_bound
#define L_B lower_bound
#define B_S binary_search
#define PR_Q priority_queue
#define FIR first
#define SEC second
#if __cplusplus < 201103L
#define stoi(argument_string) atoi((argument_string).c_str())
#endif
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP_R(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define FOR(i, m, n) for (int i = ((int)(m)); i < (int)(n); ++i)
#define FOR_R(i, m, n) for (int i = ((int)(m)-1); i >= (int)(n); --i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define SIZ(x) ((int)(x).size())
#define COUT(x) cout << (x) << endl
#define CIN(x) cin >> (x)
#define CIN2(x, y) cin >> (x) >> (y)
#define CIN3(x, y, z) cin >> (x) >> (y) >> (z)
#define CIN4(x, y, z, w) cin >> (x) >> (y) >> (z) >> (w)
#define SCAND(x) scanf("%d", &(x))
#define SCAND2(x, y) scanf("%d%d", &(x), &(y))
#define SCAND3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z))
#define SCAND4(x, y, z, w) scanf("%d%d%d%d", &(x), &(y), &(z), &(w))
#define SCANLLD(x) scanf("%lld", &(x))
#define SCANLLD2(x, y) scanf("%lld%lld", &(x), &(y))
#define SCANLLD3(x, y, z) scanf("%lld%lld%lld", &(x), &(y), &(z))
#define SCANLLD4(x, y, z, w) scanf("%lld%lld%lld%lld", &(x), &(y), &(z), &(w))
#define PRINTD(x) printf("%d\\n", (x))
#define PRINTLLD(x) printf("%lld\\n", (x))
typedef long long int lli;
using namespace std;

bool compare_by_2nd(pair<int, int> a, pair<int, int> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int ctoi(char c) {
  if (c >= '0' and c <= '9') {
    return (int)(c - '0');
  }

  return -1;
}

int alphabet_pos(char c) {
  if (c >= 'a' and c <= 'z') {
    return (int)(c - 'a');
  }

  return -1;
}

int alphabet_pos_capital(char c) {
  if (c >= 'A' and c <= 'Z') {
    return (int)(c - 'A');
  }

  return -1;
}

vector<string> split(string str, char ch) {
  int first = 0;
  int last = str.find_first_of(ch);

  if (last == string::npos) {
    last = SIZ(str);
  }

  vector<string> result;

  while (first < SIZ(str)) {
    string Ssubstr(str, first, last - first);
    result.push_back(Ssubstr);
    first = last + 1;
    last = str.find_first_of(ch, first);

    if (last == string::npos) {
      last = SIZ(str);
    }
  }

  return result;
}

int gcd(int a, int b) // assuming a,b >= 1
{
  if (a < b) {
    return gcd(b, a);
  }

  if (a % b == 0) {
    return b;
  }

  return gcd(b, a % b);
}

int lcm(int a, int b) // assuming a,b >= 1
{
  return a * b / gcd(a, b);
}

/*------------------ the end of the template -----------------------*/

#define M 1046527

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

lli getkey(string str) {
  lli sm = 0;
  lli p = 1;

  REP(i, SIZ(str)) {
    sm += (alphabet_pos_capital(str[i]) + 1) * p;
    p *= 3;
  }

  return sm;
}

int indgen(string str, lli i) {
  lli temp = getkey(str);

  return ((temp % 1919) + i * ((temp % 541) + 1)) % M;
}

int insert(string str, vector<string> &v) {
  lli i = 0;
  int h;

  for (i = 0; i < M; ++i) {
    h = indgen(str, i);
    if (v[h] == str) {
      return 1;
    } else if (SIZ(v[h]) == 0) {
      v[h] = str;
      return 0;
    }
  }
}

int find(string str, vector<string> &v) {
  lli i = 0;
  int h;

  for (i = 0; i < M; ++i) {
    h = indgen(str, i);

    if (v[h] == str) {
      return 1;
    } else if (SIZ(v[h]) == 0) {
      return 0;
    }
  }
}

int main() {
  IOS; /* making cin faster */
  vector<string> v(M);
  int n;

  CIN(n);

  string com, st;

  REP(i, n) {
    CIN2(com, st);

    if (com == "insert") {
      insert(st, v);

    } else {
      if (find(st, v)) {
        printf("yes\\n");

      } else {
        printf("no\\n");
      }
    }
  }
}