Problem p02269 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define INF 1LL << 30
#define MAXN 1000003
#define pb push_back
#define optimizar_io                                                           \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define fst first
#define snd second
#define B 127
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
const int ALF = 5;
struct nodo {
  int termina;
  int hijos[ALF];
  nodo() {
    termina = -1;
    for (int i = 0; i < ALF; i++) {
      hijos[i] = -1;
    }
  }
};
nodo Trie[MAXN];

int ultimo = 1;
int valor(char c) {
  if (c == 'A')
    return 1;
  if (c == 'C')
    return 2;
  if (c == 'G')
    return 3;
  if (c == 'T')
    return 4;
}

void Agregar(string &s, int idx) {
  int pos = 0;

  for (int i = 0; i < s.size(); i++) {
    if (Trie[pos].hijos[valor(s[i])] == -1) {
      ultimo++;
      Trie[pos].hijos[valor(s[i])] = ultimo;
    }
    pos = Trie[pos].hijos[valor(s[i])];
  }
  if (Trie[pos].termina == -1) {
    Trie[pos].termina = idx;
  }
}

bool Busca(string &s) {
  int pos = 0;
  for (int i = 0; i < s.size(); i++) {
    pos = Trie[pos].hijos[valor(s[i])];
    if (pos == -1)
      return false;
  }
  if (Trie[pos].termina == -1)
    return false;
  return true;
}

int main() {
  int n;

  cin >> n;
  cin.ignore();
  int diff = 1;
  for (int t = 1; t <= n; t++) {
    string q, p;
    cin >> q >> p;
    if (q == "insert") {

      Agregar(p, diff);
      diff++;
    } else {
      if (Busca(p)) {
        cout << "yes";
      } else {
        cout << "no";
      }
      cout << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAXN 2000000
#define pb push_back
#define fst first
#define snd second
#define endl '\\n'
typedef long long int ll;
const ll P = 1000000007;
#define optimizar_io                                                           \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const ll B = 29LL;
string s;
string tipo;
ll n, aux;

ll H[100002];
ll ExpB[100002];

ll valor(char c) { return c - 'A' + 1; }

/*void Hashes(){
        H[0] = valor(s[0])%P;
        ExpB[0] = 1;
        for(int i=1; i< s.size();i++){
                H[i] = (B*H[i-1] )%P;
                H[i] = (H[i] + valor(s[i]))%P;

                ExpB[i] = (ExpB[i-1]*B)%P;
        }

}*/

ll unico() {
  ll ret = 0;
  for (int i = 0; i < s.size(); i++) {
    ret = (ret * B) % P;
    ret = (ret + valor(s[i])) % P;
  }
  return ret;
}

/*bool validacionunico(string s){
        bool flag = true;
        for(int i=0; i<s.size();i++){
                if(s[i] == 'A' || s[i] == 'C' || s[i] == 'G' || s[i] == 'T'  ){
                        // JEJE
                }else{
                                flag = false;
                }
                        //flag = true;

        }
        return flag;
}*/

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    // cout << "i: "<<i<<'\\n';
    cin >> tipo;
    if (tipo == "insert") {
      cin >> s;
      aux = unico();
      // cout << "auxinsert: "<<aux<<'\\n';
      H[aux]++;

    } else {
      cin >> s;
      aux = unico();
      // cout << "auxfind: "<<aux<<'\\n';
      if (H[aux] != 0)
        cout << "yes" << '\\n';
      else
        cout << "no" << '\\n';
    }
  }
  return 0;
}