HDU2648 Shopping(字符串哈希)
Description
Every 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.
Input
One 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.
Output
Contains 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
3
memory
kfc
wind
2
49 memory
49 kfc
48 wind
80 kfc
85 wind
83 memory
Sample Output
1
2
题意
字符串哈希模板题。
#include<bits/stdc++.h>
#define lowbit(x) ((x)&-(x));
using namespace std;
typedef long long ll;
const int N=1e4+5,NN=2e3+10,INF=0x3f3f3f3f;
const ll MOD=1e9+7;
struct Node{
char name[35];
int price;
};
vector<Node>List[N];
int n,m,key,add,memory_price,rankx,len;
int p[N];
char s[35];
unsigned int BKDRHash(char *str){//Hash函数
unsigned int seed=31,key=0;
while(*str) key=key*seed+(*str++);
return key&0x7fffffff;
}
void init(){
for(int i=0;i<N;i++) List[i].clear();
}
int main(){
while(~scanf("%d",&n)){
init();
for(int i=0;i<n;i++){
Node t;
scanf("%s",t.name);
key=BKDRHash(t.name)%N;//计算Hash值
List[key].push_back(t);//Hash值可能冲突,把冲突的哈希值都存起来
}
scanf("%d",&m);
while(m--){
rankx=len=0;
for(int i=0;i<n;i++){
scanf("%d %s",&add,s);
key=BKDRHash(s)%N;//计算Hash值
for(int j=0;j<List[key].size();j++){//处理冲突问题
if(strcmp(List[key][j].name,s)==0){
List[key][j].price+=add;
if(strcmp(s,"memory")==0) memory_price=List[key][j].price;
else p[len++]=List[key][j].price;
break;
}
}
}
for(int i=0;i<len;i++) if(memory_price<p[i]) rankx++;
printf("%d\n",rankx+1);
}
}
return 0;
}