题目要求:给你两个txt文档,一个是发送垃圾邮件的黑名单邮箱地址,另一个是待检测的邮箱地址,现要求给出一种方案,尽快将黑名单当中的地址从待测邮箱地址中分离出来。
大家都知道,对于这一类问题,散列表所给出的解决方案虽然需要消耗大量的物理空间,但在时间上却有很大的优势,不同的哈希函数有不同的效果,在特定的和环境下都能发挥出不一样的效果。在这里作者给出了十余种解决方式,并进行了测试,具体如下。。。。不多说,上C代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define N 10000005
#define MOD 10000007
typedef struct node
{
char address[20];
struct node *next;
}node, *ptr;
node str[N] = { 0 };
/*unsigned int Hash(char* str) // 154.4s
{
unsigned int hash = 0;
unsigned int x = 0;
unsigned int i = 0;
for(i = 0; i < sizeof(str); str++, i++)
{
hash = (hash << 4) + (*str);
if((x = hash & 0xF0000000L) != 0)
{
hash ^= (x >> 24);
}
hash &= ~x;
}
return hash;
} */
/*unsigned int Hash(char* str) // 148.9s
{
const unsigned int BitsInUnsignedInt = (unsigned int)(sizeof(unsigned int) * 8);
const unsigned int ThreeQuarters = (unsigned int)((BitsInUnsignedInt * 3) / 4);
const unsigned int OneEighth = (unsigned int)(BitsInUnsignedInt / 8);
const unsigned int HighBits = (unsigned int)(0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth)