A Za, A Za, Fighting...

坚信:勤能补拙

PKU 2503 Babelfish

问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=2503

思路:
字符串哈希
这里使用的哈希函数是ELFHash
 1 int ELFHash(char *str)
 2 {
 3     unsigned long t, hash = 0;
 4     while(*str) {
 5         hash = (hash<<4+ (*str++);
 6         if((t = hash&0xF0000000L))
 7             hash ^= t>>24;
 8         hash &= ~t;
 9     }
10     return (hash & 0x7FFFFFFF)%PRIME;
11 }

因为之前写哈希表解决冲突都是用的链接法,这里想尝试一下开放地址法,采用最简单的线性探查,结果居然TLE
无奈还是改成链接法,然后就AC了
不过,时间却有719MS之多,而网上几乎相同的代码只需要230MS左右,不知道是何原因

代码:
TLE的开放地址法
 1 void
 2 insert(int hash_val, int index)
 3 {
 4     if(!hash[hash_val].used) {
 5         hash[hash_val].used = 1;
 6         hash[hash_val].index = index;
 7     } else
 8         insert((hash_val+1)%PRIME, index); /* linear probing */
 9 }
10 
11 int
12 search(char *f_word)
13 {
14     int hash_val = ELFHash(f_word);
15     int i = 0;
16     while(1) {
17         if(!hash[hash_val].used || i==PRIME)
18             break;
19         if(strcmp(f_word, flg[hash[hash_val].index])==0)
20             return hash[hash_val].index;
21         hash_val = (hash_val+1)%PRIME;
22         ++i;
23     }
24     return -1;
25 }
26 
27 void
28 input_hash()
29 {
30     int hash_val, index = 0;
31     char tmp[MAX_LEN*2+1];
32     memset(hash, 0sizeof(hash));
33     while(gets(tmp) && tmp[0]) {
34         sscanf(tmp, "%s %s", eng[index], flg[index]);
35         hash_val = ELFHash(flg[index]);
36         insert(hash_val, index);
37         ++index;
38     }
39 }

AC的链接法
 1 void
 2 insert(int hash_val, int index)
 3 {
 4     struct Node *node = (struct Node *)malloc(sizeof(struct Node));
 5     if(node == NULL) {
 6         fprintf(stderr, "malloc error in: insert\n");
 7         exit(1);
 8     }
 9     node->index = index;
10     node->next = hash[hash_val];
11     hash[hash_val] = node;
12 }
13 
14 int
15 search(char *f_word)
16 {
17     int hash_val = ELFHash(f_word);
18     struct Node *node = hash[hash_val];
19     while(node != NULL) {
20         if(strcmp(f_word, flg[node->index]) == 0)
21             return node->index;
22         node = node->next;
23     }
24     return -1;
25 }
26 
27 void
28 input_hash()
29 {
30     int hash_val, index = 0;
31     char tmp[MAX_LEN*2+1];
32     memset(hash, 0sizeof(hash));
33     while(gets(tmp) && tmp[0]) {
34         sscanf(tmp, "%s %s", eng[index], flg[index]);
35         hash_val = ELFHash(flg[index]);
36         insert(hash_val, index);
37         ++index;
38     }
39 }

posted on 2010-07-28 11:11 simplyzhao 阅读(208) 评论(0)  编辑 收藏 引用 所属分类: G_其他


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2010年7月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

统计

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜