HDU 2648 shopping

Source: Internet
Author: User

 

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2648

 

There are n stores whose prices are rising every day. Ask us if there is a store named memory on ith day. The prices of these stores are several higher than that of others, output its ranking. k is above it, and it is the k + 1 place.

 

Solution: Hash search

 

# Include <iostream> <br/> # include <vector> <br/> using namespace STD; </P> <p> struct node <br/> {<br/> char STR [35]; <br/> int price; <br/> node () {price = 0 ;}< br/>}; </P> <p> const int prime = 100007; <br/> int AA [prime]; <br/> vector <node> list [prime]; </P> <p> unsigned int bkdrhash (char * Str) <br/>{< br/> unsigned int seed = 31, key = 0; <br/> while (* Str) <br/> key = key * seed + * STR ++; <br/> return key & (0x7fffffff ); <Br/>}</P> <p> int main () <br/> {<br/> int N, key, Val, len1, M, I, count, res, J, Len, price; <br/> node A; <br/> char s [35]; </P> <p> while (scanf ("% d", & M )! = EOF) <br/>{< br/>/* multiple groups of data, to clear the vector container */<br/> for (I = 0; I <prime; I ++) <br/> list [I]. clear (); </P> <p> for (I = 0; I <m; I ++) <br/>{< br/> scanf ("% s", S); <br/> strcpy (. STR, S); <br/> key = bkdrhash (s) % prime; <br/> list [Key]. push_back (a); <br/>}< br/> scanf ("% d", & N); <br/> while (n --) <br/> {<br/> COUNT = 0;/* The price of several shops is higher than that of memory */<br/> Len = 0; /* record all prices */<br/> price = 0; <br/> for (I = 0; I <m; I ++) <br/>{< br/> scanf ("% d % s", & Val, S); <br/> key = bkdrhash (s) % prime; <br/> len1 = list [Key]. size (); <br/> for (j = 0; j <len1; j ++) <br/>{< br/> If (! Strcmp (list [Key] [J]. STR, S) <br/>{< br/> list [Key] [J]. price + = val; <br/> res = list [Key] [J]. price;/* record value */<br/> break; <br/>}< br/> If (strcmp (S, "Memory ")! = 0) <br/> AA [Len ++] = res;/* input array */<br/> else <br/> price = res; /* record the memory store price */<br/>}< br/> for (I = 0; I <Len; I ++) <br/>{< br/> If (price <AA [I]) <br/> count ++; <br/>}< br/> printf ("% d/N", Count + 1); <br/>}< br/> return 0; <br/>}

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.