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

#include <bits/stdc++.h>
using namespace std;
string H[10465270];
int getchar(char key) {
  if (key == 'A') {
    return 1;
  } else if (key == 'C') {
    return 2;
  } else if (key == 'G') {
    return 3;
  } else if (key == 'T') {
    return 4;
  }
}

long long getkey(string key) {
  long long p = 1, sum = 0;
  for (int i = 0; i < key.size(); i++) {
    sum += getchar(key.at(i)) * p;
    p *= 4;
  }
  return sum;
}
void insert(string dic) {
  int key;
  key = getkey(dic);
  H[key] = dic;
}
int find(string dic) {
  if (H[getkey(dic)] == dic) {
    return 1;
  } else {
    return 0;
  }
}
int main() {
  int n;
  string str, dic;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str >> dic;
    if (str.at(0) == 'i') {
      insert(dic);
    } else {
      if (find(dic)) {
        cout << "yes" << endl;
      } else {
        cout << "no" << endl;
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
using namespace std;

int main(void) {
  string A;
  string Key;
  int n;
  cin >> n;
  string S[n];
  string AS[n];
  string CS[n];
  string GS[n];
  string TS[n];
  int countA = 0;
  int countC = 0;
  int countG = 0;
  int countT = 0;
  for (int i = 0; i < n; i++) {
    cin >> A;
    int countAA = countA;
    int countCC = countC;
    int countGG = countG;
    int countTT = countT;

    if (A == "insert") {
      cin >> S[i];
      if (S[i].at(0) == 'A') {
        AS[countA] = S[i];
        countA++;
      } else if (S[i].at(0) == 'C') {
        CS[countC] = S[i];
        countC++;
      } else if (S[i].at(0) == 'G') {
        GS[countG] = S[i];
        countG++;
      } else {
        TS[countT] = S[i];
        countT++;
      }
    } else {
      cin >> Key;
      if (Key.at(0) == 'A') {
        for (int j = 0; j <= countA / 2; j++) {
          if (Key == AS[j]) {
            printf("yes\\n");
            break;
          }
          if (Key == AS[countAA]) {
            printf("yes\\n");
            break;
          }
          if (j == countA / 2) {
            printf("no\\n");
          }
          countAA--;
        }
      } else if (Key.at(0) == 'C') {
        for (int j = 0; j <= countC / 2; j++) {
          if (Key == CS[j]) {
            printf("yes\\n");
            break;
          }
          if (Key == CS[countCC]) {
            printf("yes\\n");
            break;
          }
          if (j == countC / 2) {
            printf("no\\n");
          }
          countCC--;
        }
      } else if (Key.at(0) == 'G') {
        for (int j = 0; j <= countG / 2; j++) {
          if (Key == GS[j]) {
            printf("yes\\n");
            break;
          }
          if (Key == GS[countGG]) {
            printf("yes\\n");
            break;
          }
          if (j == countG / 2) {
            printf("no\\n");
          }
          countGG--;
        }
      } else {
        for (int j = 0; j <= countT / 2; j++) {
          if (Key == TS[j]) {
            printf("yes\\n");
            break;
          }
          if (Key == TS[countTT]) {
            printf("yes\\n");
            break;
          }
          if (j == countT / 2) {
            printf("no\\n");
          }
          countTT--;
        }
      }
    }
  }
  return 0;
}