My way of Hashing , Need a better way .

How do we use Hashing of characters, I have a very funny way . Have to write a Lot , This actually , is my code . Please Help me for this , everytime , needed to find this code in my pc and copy paste . :stuck_out_tongue:

vector<pair<char, int> > vp;
vp.push_back(make_pair('a', 0));
vp.push_back(make_pair('b', 0));
vp.push_back(make_pair('c', 0));
vp.push_back(make_pair('d', 0));
vp.push_back(make_pair('e', 0));
vp.push_back(make_pair('f', 0));
vp.push_back(make_pair('g', 0));
vp.push_back(make_pair('h', 0));
vp.push_back(make_pair('i', 0));
vp.push_back(make_pair('j', 0));
vp.push_back(make_pair('k', 0));
vp.push_back(make_pair('l', 0));
vp.push_back(make_pair('m', 0));
vp.push_back(make_pair('n', 0));
vp.push_back(make_pair('o', 0));
vp.push_back(make_pair('p', 0));
vp.push_back(make_pair('q', 0));
vp.push_back(make_pair('r', 0));
vp.push_back(make_pair('s', 0));
vp.push_back(make_pair('t', 0));
vp.push_back(make_pair('u', 0));
vp.push_back(make_pair('v', 0));
vp.push_back(make_pair('w', 0));
vp.push_back(make_pair('x', 0));
vp.push_back(make_pair('y', 0));
vp.push_back(make_pair('z', 0));

int total=0;
string s;
cin>>s;
a=s.length();
for( r=0;r<a;r++)
{
    for( g=0;g<26;g++)
    {
       if(s[r]==vp[g].first)
            vp[g].second+= 1;
    }
}

I didn’t get the motive of your code above. BUt however, as you said, you need hashing on strings. I generally do it like:

map<string,bool>exist;
string str;
for(int i=0;i<n;i++)
{
   cin>>str;
   exist[str]=1;
}
// now to check if any string lets say ab exists or not,
just check if exist["ab"]==1 or not
1 Like

I actually wanted to ask hashing of characters .

Assuming only lowercase letters, you can just use an array of 26.

int hash[26] = {0};

That would initialize everything to 0.

cin >> s;
for (i = 0; i < s.length(); i++)
{
    hash[s[i] - 'a']++;
}

Each character ch gets mapped to the index (ch - 'a') in the hash-table.

Going by your example code, this is what you want.

1 Like

You mean hashing of each particular character of each string?

Thanks , And if upper case i can do - ‘A’

You can use

vector second (26,0); //It initializes 26 blocks of memory to vector each with initial value = 0

1 Like

yes i mean that

You can do it this way, also, like in the above code basically ASCII values gets mapped. To directly map a character,

you can use this:

map<char,bool>exist;
for(i=0;i<s.length();i++)
{
exist[s[i]]++;
}

1 Like

See my comment on the above answer,…

int hash[26] = {0};

That would initialize everything to 0.
@tijoforyou,As far as I know THAT wouldn’t initialize everything to zero :frowning: :frowning:
Because of this,I screwed once upon a time (Though my solution was correct :frowning: )

That only initializes first element.
Be careful :slight_smile: :slight_smile:

                                                 Yours 
                                              ABHILASH RUDRA

It always does, show your code please, there might be something else missing, refer this http://stackoverflow.com/questions/629017/how-does-array100-0-set-the-entire-array-to-0

@rudra_sarraf It does initialize everything to zero.

In fact, int array[SIZE] = {x, y, z}; initializes array[0] to x, array[1] to y, array[2] to z, and the rest (array[3] to array[SIZE - 1]) to 0.

Here is a couple more variations, you could achieve it: http://stackoverflow.com/a/201116

@tijoforyou and @ damn_me,Yep.

my mistake :frowning: :frowning:

That would be true for element 0.

I meant int hash[26]={1} will not initialize everything to 1.

1 Like

there is another way to set entire array to some value…
use memset(arr,somevalue,sizeof(arr))

@rjohari23 That is not exactly true. memset sets data per byte. You can use memset to set values in a char array (assuming 1-byte chars, of course.) But, for an int array (usually 4-bytes), memset won’t work as you expected, most of the times. Sure, it can set 0s. (In hexadecimal terms, the integers that can be set using memset are 0x0000, 0x1111, 0x2222, … 0xFFFF, if you set the parameter somevalue as 0, 1, 2, …, F, respectively.)

Actually this takes O(log n) for each addition. It’s better to use vector or array for character matching as it is much faster and less complicated.