Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, June 14, 2015

HASHIT-Hash it!

Hash it!

Given below c++ code for HASHIT spoj or Hash it! spoj.



#include <bits/stdc++.h>
using namespace std;
long long hash_value(string a)
{
    long long h=0;
    for(long long i=0; i<a.size(); i++)
    {
        h+=(i+1)*(long long)a[i];
    }
    h=h*19;
    return h%101;
}
vector<string> hash_table[101];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int q;
        scanf("%d",&q);
        START:
        while(q--)
        {
            string a;
            cin>>a;
            string x(a.begin()+4,a.end());
            if(a[0]=='A')
            {
                for(int i = 0 ; i < 101 ; i++)
                {
                    if(!hash_table[i].empty()&&hash_table[i][0] == x)
                        goto START;
                }
                int h=hash_value(x);
                for(int j=0; j<20; j++)
                {
                    int idx=(h+j*j+23*j)%101;
                    if(hash_table[idx].empty())
                    {
                        hash_table[idx].push_back(x);
                        break;
                    }
                }
            }
            else
            {
                int h=hash_value(x);
                for(int j=0; j<20; j++)
                {
                    int idx=(h+j*j+23*j)%101;
                    if(!hash_table[idx].empty()&&hash_table[idx][0]==x)
                    {
                        hash_table[idx].pop_back();
                        break;
                    }
                }
            }
        }
        int c=0;
        for(int i=0; i<101; i++)
            c+=hash_table[i].size();
        printf("%d\n",c);
        for(int i=0; i<101; i++)
        {
            if(!hash_table[i].empty())
                printf("%d:%s\n",i,hash_table[i][0].c_str());
        }
        for(int i=0; i<101; i++)
            hash_table[i].clear();
    }
    return 0;
}