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

#define MYDEBUG
//
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define dbp(x) cout << #x << ": " << x << endl
#define dbp2(x, y) cout << #x << "," << #y << ": " << x << "," << y << endl
#define dbp3(x, y, z)                                                          \\
  cout << #x << "," << #y << "," << #z << ": " << x << "," << y << "," << z    \\
       << endl
#define dbp4(w, x, y, z)                                                       \\
  cout << #w << "," << #x << "," << #y << "," << #z << ": " << w << "," << x   \\
       << "," << y << "," << z << endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x, y)
#define dbp3(x, y, z)
#define dbp4(w, x, y, z)
#define ifcin(x)
#endif
#define ll long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)
#define REP(i, from, to) for (int i = from; i <= to; ++i)
using namespace std;
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << "," << p.second << "}";
  return out;
}

static const int M = 1046597;
static const int L = 12;
char T[M][L];
char com[6], word[L];
int getChar(char ch) {
  if (ch == 'A') {
    return 1;
  } else if (ch == 'C') {
    return 2;
  } else if (ch == 'G') {
    return 3;
  } else {
    return 4;
  }
}

ll getKey(char str[]) {
  ll key = 0, i = 0, p = 1;
  for (i = 0; i < (ll)strlen(str); ++i) {
    key += p * getChar(str[i]);
    key *= 5; // 左シフト(5進数)
  }
  return key;
}

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

// mとh2は互いに素に
int h2(int key) {
  // 素数Mより小さい数はみなMと互いに素
  // h2の周期はM未満であればなんでも可
  // 1を加えるのはkey%(M-1) == 0のときにずれなくなるため
  // この関数は1 ~ M-1の間の値をとる
  return 1 + (key % (M - 1));
}

int insert(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(T[h], str) == 0) {
      return 1; // すでに格納済
    } else if (strlen(T[h]) == 0) {
      strcpy(T[h], str);
      return 0;
    }
  }
  return 0;
}

bool find(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % M;
    if (strcmp(T[h], str) == 0) {
      return true;
    } else if (strlen(T[h]) == 0) {
      return false;
    }
  }
  return false;
}

void solve() {
  rep(i, 0, M) {
    T[i][0] = '\\0'; // すべて空文字列で初期化
  }
  int n;
  scanf("%d", &n);
  rep(i, 0, n) {
    scanf("%s %s", com, word);
    if (com[0] == 'i') {
      insert(word);
    } else {
      if (find(word)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
}
int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#define MYDEBUG
//
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define dbp(x) cout << #x << ": " << x << endl
#define dbp2(x, y) cout << #x << "," << #y << ": " << x << "," << y << endl
#define dbp3(x, y, z)                                                          \\
  cout << #x << "," << #y << "," << #z << ": " << x << "," << y << "," << z    \\
       << endl
#define dbp4(w, x, y, z)                                                       \\
  cout << #w << "," << #x << "," << #y << "," << #z << ": " << w << "," << x   \\
       << "," << y << "," << z << endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x, y)
#define dbp3(x, y, z)
#define dbp4(w, x, y, z)
#define ifcin(x)
#endif
#define ll long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for (int i = from; i < to; ++i)
#define REP(i, from, to) for (int i = from; i <= to; ++i)
using namespace std;
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  size_t last = v.size() - 1;
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i];
    if (i != last) {
      out << ",";
    }
  }
  out << "]";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (size_t i = 0; i < v.size(); ++i) {
    out << v[i] << endl;
  }
  return out;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << "," << p.second << "}";
  return out;
}

static const int m = 1046597;
static const int l = 12;
char T[m][l];
char com[6], word[12];
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(char str[]) {
  int key = 0;
  rep(i, 0, (int)strlen(str)) {
    key += getChar(str[i]);
    key *= 5; // 左シフト(5進数)
  }
  return key;
}

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

// mとh2は互いに素に
int h2(int key) { return (1 + key) % (m - 1); }

int insert(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % m;
    if (strcmp(T[h], str) == 0) {
      return 1; // すでに格納済
    } else if (strlen(T[h]) == 0) {
      strcpy(T[h], str);
      return 0;
    }
  }
  return 0;
}

bool find(char str[]) {
  ll key, i, h;
  key = getKey(str);
  for (i = 0;; ++i) {
    h = (h1(key) + i * h2(key)) % m;
    if (strcmp(T[h], str) == 0) {
      return true;
    } else if (strlen(T[h]) == 0) {
      return false;
    }
  }
  return false;
}

void solve() {
  rep(i, 0, m) {
    T[i][0] = '\\0'; // すべて空文字列で初期化
  }
  int n;
  scanf("%d", &n);
  rep(i, 0, n) {
    scanf("%s %s", com, word);
    if (com[0] == 'i') {
      insert(word);
    } else {
      if (find(word)) {
        printf("yes\\n");
      } else {
        printf("no\\n");
      }
    }
  }
}
int main() {
  solve();
  return 0;
}