Ultraviolet A 10295-hay points

Source: Internet
Author: User

Question: There are many workers who correspond to a capability description table. Each capability has a weight and each worker has a capacity value.

Analysis: String, hash table, and dictionary tree. You can use the hash or dictionary tree to store the corresponding words and weights and query them.

Note: After initialization, clear the data.

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;//hash_definetypedef 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_endint 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;}

Ultraviolet A 10295-hay points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.