Anagrams: Difference between revisions

Content added Content deleted
(minor correction to Clojure version)
Line 108: Line 108:
#include <time.h>
#include <time.h>


char *sortedWord(char *word, char *wbuf)
char *sortedWord(const char *word, char *wbuf)
{
{
char *p1, *p2, *endwrd;
char *p1, *p2, *endwrd;
Line 152: Line 152:




int Str_Hash( char *key, int ix_max )
int Str_Hash( const char *key, int ix_max )
{
{
char *cp = key;
const char *cp;
short mash;
short mash;
int hash = 33501551;
int hash = 33501551;
while (*cp) {
for (cp = key; *cp; cp++) {
mash = cxmap[*cp % CXMAP_SIZE];
mash = cxmap[*cp % CXMAP_SIZE];
hash = ((hash >>4) ^ 0x5C5CF5C) ^ (hash<<1 + mash<<5);
hash = (hash >>4) ^ 0x5C5CF5C ^ ((hash<<1) + (mash<<5));
hash &= 0x3FFFFFFF;
hash &= 0x3FFFFFFF;
cp++;
}
}
return hash % ix_max;
return hash % ix_max;
Line 168: Line 167:
typedef struct sDictWord *DictWord;
typedef struct sDictWord *DictWord;
struct sDictWord {
struct sDictWord {
char *word;
const char *word;
DictWord next;
DictWord next;
};
};
Line 174: Line 173:
typedef struct sHashEntry *HashEntry;
typedef struct sHashEntry *HashEntry;
struct sHashEntry {
struct sHashEntry {
char *key;
const char *key;
HashEntry next;
HashEntry next;
DictWord words;
DictWord words;
Line 221: Line 220:
we->next = he->words;
we->next = he->words;
he->words = we;
he->words = we;
he->wordCount += 1;
he->wordCount++;
if ( maxPC < he->wordCount) {
if ( maxPC < he->wordCount) {
maxPC = he->wordCount;
maxPC = he->wordCount;
Line 232: Line 231:
}
}
numWords += 1;
numWords++;
}
}
printf("%d words in dictionary max ana=%d\n", numWords, maxPC);
printf("%d words in dictionary max ana=%d\n", numWords, maxPC);
Line 239: Line 238:




int main( int argc, char *argv[] )
int main( )
{
{
HashEntry he;
HashEntry he;