`

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

 
阅读更多

题目来源: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;
    }
};


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics