Cod sursa(job #3277802)

Utilizator TeogaloiuMatei Ionescu Teogaloiu Data 17 februarie 2025 13:49:49
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream cin("hashuri.in");
ofstream cout ("hashuri.out");
const int mod=665993;
const int base=11;
long long powers[15];
void powermaker()
{
    int check=1;
    powers[0] = 1;
    for(int i=1;i<=14;i++)
    {
        powers[i]=check;
        check*=base;
        check%=mod;
    }
}
vector <int> v[mod+5];
int hasher(int x)
{
    long long newx=0;
    int p=1;
    while(x)
    {
        newx+=1LL*powers[p]*(x%10);
        newx%=mod;
        p++;
        x/=10;
    }
    return newx;
}
bool finder(int val,int hashed)
{
    bool flag=false;
    for(int i=0;i<v[hashed].size();i++)
        if(v[hashed][i]==val)
            flag=true;
    return flag;
}
int main()
{
    int n;
    cin>>n;
    powermaker();
    for(int k=1;k<=n;k++)
    {
        int c;
        cin>>c;
        int val,hashed;
        cin>>val;
        hashed=hasher(val);
        if(c==1) {
            // cout << hashed << '\n';
            if(finder(val,hashed)==false)
                v[hashed].push_back(val);
        }
        if(c==2)
        {
            for(int i=0;i<v[hashed].size();i++)
                if(v[hashed][i]==val){
                {
                    int last_idx = v[hashed].size() - 1;
                    swap(v[hashed][i],v[hashed][last_idx]);
                    v[hashed].pop_back();
                    break;
                }
            }
        }
        if(c==3)
        {
            if(finder(val,hashed))
                cout<<"1\n";
            else
                cout<<"0\n";
        }
           
    }
    return 0;
}