sicily--1299. Academy Awards

  1. 水题
  2. map 用于储存字符串出现的次数
  3. 一开始直接遍历map 找出最大值输出,结果就WA 了,原来题目要求说 若有相同的次数要输出最早在 list 中出现的那个, 而 map 是会自动排序的,所以还需要再加入一个vector来存“顺序”, 最后通过遍历 vector 找出最大值来输出

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

int main()
{
	int awards;
	while(cin >> awards && awards != 0)
	{
		map<string, int> film;
		map<string, int>::iterator it;
		vector<string> v;//没办法,题目要求要顺序
		while(awards--)
		{
			string awardsName;
			cin >> awardsName;
			int filmNum;
			cin >> filmNum;
			while(filmNum--)
			{
				string temp;
				cin >> temp;
				v.push_back(temp);
				it = film.find(temp);
				if(it == film.end())//未出现过
				{
					film[temp] = 1;
				}
				else//出现过
				{
					film[temp]++;
				}
			}
		}

		string maxString;
		int maxInt = 0;
		for(int i = 0; i < v.size(); i++)
		{
			string temp = v[i];
			it = film.find(temp);
			if((*it).second > maxInt)//找出现次数最多的
			{
				maxString = (*it).first;
				maxInt = (*it).second;
			}
		}
		cout << maxString << endl;
	}
	return 0;
}


你可能感兴趣的:(vector,String,list,iterator)