Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 183 Accepted Submission(s): 46
Problem DescriptionEvery girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called "memory". Now she wants to know the rank of this shop's price after the change of everyday.
InputOne line contians a number n ( n<=10000),stands for the number of shops.
Then n lines ,each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.)stands for the name of the shop.
Then a line contians a number m (1<=m<=50),stands for the days .
Then m parts , every parts contians n lines , each line contians a number s and a string p ,stands for this day ,the shop p 's price has increased s.
OutputContains m lines ,In the ith line print a number of the shop "memory" 's rank after the ith day. We define the rank as :If there are t shops' price is higher than the "memory" , than its rank is t+1.
Sample Input
3memorykfcwind249 memory49 kfc48 wind80 kfc85 wind83 memory
Sample Output
12 一下是用map容器過的,下面還有一種是雜湊過的#include <iostream>#include <map>#include <string>using namespace std;#define N 10005map<string,int> elem;int node[N];int main(){int n,m,i,val,j,rank;string str;while(cin>>n){int n1=n;for(i=1;i<=n1;i++){cin>>str;if(str=="memory"){str[0]='0';elem[str]=0;i--;n1--;}elseelem[str]=i;}memset(node,0,sizeof(node));scanf("%d",&m);while(m--){for(i=1;i<=n;i++){cin>>val>>str;if(str=="memory")str[0]='0';node[elem[str]]+=val;}int cnt=0;for(i=1;i<n;i++){if(node[i]>node[0])cnt++;}printf("%d/n",cnt+1);}}return 0;}最後同學提醒用雜湊表,去學習了下雜湊才過的~有一點需要注意:剛剛開始用scanf("%d",&n);一直WA。。。原來不是一組資料~要while(scanf("%d",&n)!=EOF)因為這個錯的朋友要注意了!#include <iostream>using namespace std;#define szHASH 50000 /*HASH表總長度*/#define HASH_ROOT 50007 /*用於計算雜湊地址的隨機數*/struct THash{int key; /*鑰匙碼*/char shop_name[35];int depth;};int rank,hhh;int elem[szHASH];/*HASH地址尋找*/int GetHashAddress(int key,int root){return key%root;}/*衝突地址計算,如果發現地址衝突,則用當前地址和鑰匙碼、雜湊根重建一個新地址*//*int GetConflictAddress(int key, int address, int root){int addr=address+key%5+1;return addr%root;}*//*尋找key*/unsigned int ELFHash(char* str) { unsigned int hash = 0; unsigned int x = 0; while(*str) { hash = (hash << 4) + (*str++); if((x = hash & 0xF0000000L) != 0) { hash ^= (x >> 24); hash &= ~x; } } return hash & 0x7FFFFFFF; }/*根據商店數、長度和雜湊根構造雜湊表*/struct THash * CreateNames(int size, int root, int number){int i =0, key = 0, addr = 0, depth = 0; char name[35];struct THash * h = 0, *hash = 0;/*雜湊根和長度不能太小*///if(size < root || root < 2) return 0;/*根據雜湊表長度構造一個空的雜湊表*/hash = (struct THash *)malloc(sizeof(struct THash) * size);/*將整個表清空*/memset(hash, 0, sizeof(struct THash) * size);for(i = 0; i < number; i++) {/*首先產生一個隨機的店名,並根據店名計算雜湊鑰匙碼,再根據鑰匙碼計算地址*/scanf("%s",name);key=ELFHash(name);addr=GetHashAddress(key,root);h = hash + addr;if (h->depth == 0) { /*如果當前雜湊地址沒有被佔用,則存入資料*/h->key = key;strcpy(h->shop_name,name);h->depth ++;continue;}/*如果雜湊地址已經被佔用了,就是說有衝突,則尋找一個新地址,直到沒有被佔用*/depth = 0;while(h->depth) {addr = (addr+1)%HASH_ROOT;h = hash + addr;depth ++;}/*按照新地址存放資料,同時記錄檢索深度*/h->key = key;strcpy(h->shop_name , name);h->depth = depth + 1;}return hash;}/*在雜湊表中以特定雜湊根尋找一個商店的記錄*/struct THash * Lookup(struct THash * hash, char * name, int root){int key = 0, addr = 0; struct THash * h = 0;/*不接受空表和空名稱*/if(!name || !hash) return 0;key = ELFHash(name);addr = GetHashAddress(key, root);h = hash + addr;/*如果結果不正確表示按照衝突規則繼續尋找*/while(strcmp(h->shop_name , name)) {addr = (addr+1)%HASH_ROOT;h = hash + addr;if(h->key == 0) return 0; }return hash + addr;}int main(){struct THash *hash=0,*h=0;int n,m,i,val;char name[35];while(scanf("%d",&n)!=EOF){memset(elem,0,sizeof(elem));hash=CreateNames(szHASH, HASH_ROOT,n);h=Lookup(hash,"memory",HASH_ROOT);hhh=h-hash;scanf("%d",&m);while(m--){rank=0;for(i=0;i<n;i++){scanf("%d%s",&val,name);h=Lookup(hash,name,HASH_ROOT);elem[h-hash]+=val;}for(i=0;i<szHASH;i++){if(elem[i]>elem[hhh])rank++;}printf("%d/n",rank+1);}}return 0;}