You could use an additive hash function, as Salem suggested.
Printable View
You could use an additive hash function, as Salem suggested.
Here's something else to try, given strings "abcde" and "deabc".
Locate the position of the first element "a" in the 2nd string.
Now walk along both strings in parallel, comparing each character.
When 2nd string ends, "switchback" to its beginning and continue comparing chars.
Terminate the program when the 1st string ends ie "match found" or a mismatch occurs.
what if you have more than one "a" in the string?
"ababa"
"babaa"
The problem is that you want a crappy hash. You want collisions. Designing to purposefully have collisions is generally bad.
I can see a way to do it, but your hash is going to be LEN^2+1 in size, and you aren't going to be able use == to compare hashes. Here's what you do:Now, write yourself a shift function, and a sort function, and you're done. (Edit: You might want to free list when you're done with it.)Code:char *hash( char *s )
{
if( s )
{
char *h = NULL;
size_t len = strlen( s );
if( (h = malloc( len * len + 1 )) )
{
size_t x;
char **list = malloc( len * sizeof *list );
for( x = 0; x < len; x++ )
{
list[ x ] = shift( s, x );
}
sort( list )
for( x = 0; x < len; x++ )
{
strcat( h, list[ x ] );
}
}
return h;
}
return NULL;
}
Quzah.
Hmmm! in that case methinks the code needs to step thro' all occurences of "a" until either a mismatch or the 1st strings ends.