Toph Eet Izz Phet! Solution

#include<bits/stdc++.h>
#define N 100010
#define ll long long
#define ull unsigned long long
#define pi pair<int, int>
#define pii pair<ll, int>
#define maximum 100000001
#define minimum -100000001
#define fastio ios_base :: sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define point cout<<showpoint<<fixed<<setprecision(8);
using namespace std;

const ll p = 10003193;

ll Hash(string s)
{
    ll v = 0, mul = 1;
    int sz = s.size();

    for(int i=0; i<sz; i++)
    {
        v = (v + (mul * s[i]));
        mul = (mul * p);
    }

    return v;
}


int main()
{
    fastio

    int n, m;
    cin >> n;

    string s;
    unordered_map<ll, int>mp;
    for(int i=0; i<n; i++)
    {
        cin >> s;
        ll a = Hash(s);
        mp[a] = 1;
    }

    cin >> m;
    for(int i=1; i<=m; i++)
    {
        string str;
        cin >> str;

        ll a = Hash(str);
        if(mp[a] == 1)
            cout << "yes" << "\n";
        else
            cout << "no" << "\n";
    }

    return 0;
}

Comments