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/>}