UVa 10295 - Hay Points

题目:有很多工人,对应一个能力描述表,每种能力有一个权值,求每个工人的能力值。

分析:字符串,hash表,字典树。利用散列表或者字典树存储对应的单词和权值,查询即可。

说明:注意初始化,计算完将数据清除。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

//hash_define
typedef struct hnode
{
	char   words[20];
	int    value;
	hnode* next;
}hash;
hash  hash_node[2001];
hash* hash_table[2005];
int   hash_size;

int hash_initial()
{
	hash_size = 0;
	memset(hash_table, 0, sizeof(hash_table));
	memset(hash_node, 0, sizeof(hash_node));
} 

int hash_value(char *str)
{
	int value = 0;
	for (int i = 0 ; str[i] ; ++ i) {
		value = value*26%1000;
		value += str[i];
	}
	return value;
}

int hash_insert(char *str, int val)
{
	int value = hash_value(str);
	hash_node[hash_size].value = val;
	strcpy(hash_node[hash_size].words, str);
	hash_node[hash_size].next = hash_table[value];
	hash_table[value] = &hash_node[hash_size ++];
}

int hash_find(char *str)
{
	int value = hash_value(str);
	for (hash* p = hash_table[value] ; p ; p = p->next)
		if (!strcmp(p->words, str))
			return p->value;
	return 0;
}
//hash_end

int main()
{
	int  m,n,v;
	char buf[2001];
	while (~scanf("%d%d",&m,&n)) {
		hash_initial();
		for (int i = 0 ; i < m ; ++ i) {
			scanf("%s%d",buf,&v);
			hash_insert(buf, v);
		}
		
		for (int i = 0 ; i < n ; ++ i) {
			int sum = 0;
			while (~scanf("%s",buf)) {
				if (!strcmp(buf, "."))
					break;
				sum += hash_find(buf);
			}
			printf("%d\n",sum);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值