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

HDU 2648 Shopping

2016-05-07 22:35 369 查看

题目分析

这里的题目意思我就不啰嗦了,利用map进行处理,并用hash的思想更快更简单地解决问题,详情看代码。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
using namespace std;
map <string, int> change; //将字符串转化为整型值利于处理
int has[10005];           //hash表用来存储每个字符串对应的商店的价格

int main()
{
int n,m;
string s;
while(scanf("%d", &n) != EOF)
{
memset(has, 0, sizeof(has));
int flag,increase;
for(int i = 1; i <= n; i++)
{
cin >> s;
change[s] = i;               //直接这样写或者change.insert(pair<string,int>(s,i)),不懂的看一下map的用法
if(s == "memory")
flag = i;
}
scanf("%d", &m);
while(m--)
{
for(int i = 1; i <= n; i++)
{
cin >> increase >> s;   //将对应商场的价格增加increase;
has[change[s]] += increase;
}
int sum = 0;
for(int i = 1; i <= n; i++) //找到对应的位置
if(has[i] > has[flag]) sum++;
printf("%d\n", sum+1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: