您的位置:首页 > 运维架构

HDU 2648 Shopping

2010-07-27 22:41 381 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2648

题目大意:有N个店,他们的商品价格每天都在上涨,问你 ith天有个叫memory的店,它的价格在所有商店中,有几个高过它,输出它的排名,有k个高过它,它就是第k+1名。

解题思路:哈希搜索

#include <iostream>
#include <vector>
using namespace std;

struct node
{
char str[35];
int price;
node(){price = 0;}
};

const int prime = 100007;
int aa[prime];
vector<node>LIST[prime];

unsigned int BKDRHash(char *str)
{
unsigned int seed = 31,key = 0;
while(*str)
key = key*seed+ *str++;
return key&(0x7fffffff);
}

int main()
{
int n,key,val,len1,m,i,count,res,j,len,price;
node a;
char s[35];

while (scanf("%d",&m)!=EOF)
{
/*多组数据,要清空vector容器*/
for (i=0;i<prime;i++)
LIST[i].clear();

for(i=0;i<m;i++)
{
scanf("%s",s);
strcpy(a.str,s);
key = BKDRHash(s)%prime;
LIST[key].push_back(a);
}
scanf("%d",&n);
while (n--)
{
count = 0;/*统计有几个shop价格大于memory*/
len = 0;/*记录所有价格*/
price = 0;
for(i=0;i<m;i++)
{
scanf("%d%s",&val,s);
key = BKDRHash(s)%prime;
len1 = LIST[key].size();
for (j=0;j<len1;j++)
{
if(!strcmp(LIST[key][j].str,s))
{
LIST[key][j].price+=val;
res = LIST[key][j].price;/*记录数值*/
break;
}
}
if(strcmp(s,"memory")!=0)
aa[len++] = res;/*入数组*/
else
price = res;/*记录memory店的价格*/
}
for (i=0;i<len;i++)
{
if(price<aa[i])
count++;
}
printf("%d/n",count+1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: