zoukankan      html  css  js  c++  java
  • UVa 10295

    题目:有非常多工人。相应一个能力描写叙述表,每种能力有一个权值,求每一个工人的能力值。

    分析:字符串。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
    ",sum);
    		}
    	}
    	return 0;
    }
    

  • 相关阅读:
    delphi 实体类 JSON 数组 TJsonSerializer Deserialize
    IIS 禁止访问:在 Web 服务器上已拒绝目录列表
    ASP.NET 一般处理程序
    .net 架构
    delphi XE8 NetHTTPRequest NetHTTPClient
    ASP.NET web 应用程序项目
    HttpClient
    eclipse Android 开发基础 Activity 窗体 界面
    关闭 iTunes 自动同步
    could not be installed at this time
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7219840.html
Copyright © 2011-2022 走看看