HDU 2648 Shopping
2015-01-06 01:06
316 查看
Shopping
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2126 Accepted Submission(s): 712
Problem 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
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2648
~~~自己的第一道字符串hash,接触到了BKDRHash函数,网上百度了许多似乎没有对它进行太多的解释,因此我也就拿来用用,里面的seed=31没怎么理解,似乎是为了让hash值更分散从而减少冲突,key是记录hash值,最后的key&0x7fffffff好像只是为了让key得到一个整型的值,但是后面还是要对它进行取模,所以为什么中间不能对它取模呢,还是有些不理解的地方,希望以后会有机会解释这些。现在就先这样用着吧。
题目大意:有很多商店,有样物品在每个商店的价钱都不一样,而且每天这个物品的价钱都会增加,要求一个叫“memory”商店里的这个物品价格在所有商店里排在第几位(价格越低排在越前)。
题目思路:因为商店数n是10的4次方,暴力比较的话会超时,所以用字符串hash先记录每个商店的位置,用vector定义动态二维数组记录,这样遇到冲突的hash值也可以处理。
然后就是输入和更新了,这题目会用到许多变量,所以初始化和使用时还是要仔细点的。具体实现可以直接看代码。
AC代码:
#include<iostream> #include<cstdio> #include<vector> #include<cstring> using namespace std; #define N 10005 struct node{ char name[35]; int price; node(){ price=0; } }; int p ; vector<node>List ; unsigned int BKDRHash(char *str){ unsigned int seed = 31,key = 0; while(*str){ key=key*seed+*str++; } return key&0x7fffffff; } int main(){ int n,m,k,add,memory_price,tot,len; char s[35]; node t; while(scanf("%d",&n)!=EOF){ for(int i=0;i<N;i++){ List[i].clear(); } for(int i=0;i<n;i++){ scanf("%s",t.name); k=BKDRHash(t.name)%N; List[k].push_back(t); } scanf("%d",&m); while(m--){ tot=len=0; for(int i=0;i<n;i++){ scanf("%d%s",&add,s); k=BKDRHash(s)%N; for(int j=0;j<List[k].size();j++){ if(strcmp(List[k][j].name,s)==0){ List[k][j].price+=add; if(strcmp(s,"memory")==0) memory_price=List[k][j].price; else p[len++]=List[k][j].price; break; } } } for(int i=0;i<len;i++){ if(memory_price<p[i]) tot++; } printf("%d\n",tot+1); } } return 0; }
相关文章推荐
- HDU 2648 Shopping
- HDU 2648 Shopping
- hdu 2648 Shopping
- HDU 2648 Shopping
- HDU 2648 Shopping(map)
- hdu 2648 shopping
- HDU 2648 Shopping (map与迭代器)
- HDU 2648 Shopping (map与迭代器)
- HDU 2648——shopping
- HDU 2648 Shopping (map与迭代器)
- HDU 2648 Shopping (map与迭代器)
- HDU 2648 Shopping (map与迭代器)
- hdu 2648(Shopping) STL map的使用
- HDU 2648 Shopping (map与迭代器)
- HDU 2648 Shopping
- HDU 2648 Shopping (map与迭代器)
- HDU 2648 Shopping
- HDU 2648 Shopping
- hdu 2648(map简单应用)
- hdu 3768 Shopping(DFS+最短路)