您的位置:首页 > 其它

SRM 222 Div II Level Two: GroceryBagger,STL map 用法

2013-07-17 11:43 429 查看
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3450

这个题目其实很简单,只要得到每种物品的数量就可以了,用一般的方法也可以做,不过如果使用STL中map

容器的话,那真是轻松加惬意啊!主要是map重载了[]运算符,所以操作起来特别方便。

代码如下:

#include <string>
#include <vector>
#include <map>

using namespace std;

class GroceryBagger {
public:
int minimumBags(int strength, vector <string> itemType) {
int res = 0;
map <string, int> msi;
for (int i = 0; i < itemType.size(); i++) {
msi[ itemType[i] ] = 0;
}

for (int i = 0; i < itemType.size(); i++) {
++msi[ itemType[i] ];
}

for ( map<string, int>::iterator it = msi.begin();
it != msi.end(); it++) {
res += (it->second + strength - 1) / strength;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: