xmu 1438.程序员Astar的生活

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

map<string,int> mp;
vector<string> ans;

int main()
{
   string str,minStr;
   int i=0,n,maxNum;
   cin>>n;
   n=n*3;
   while(n--)
   {
	   cin>>str;
	   mp[str]++;
   }

   map<string,int>::iterator iter;
   int count=3,k=0;
   while (count--)
   {
	   iter=mp.begin();
	   minStr=" ";
	   maxNum=0;
	   for(;iter!=mp.end();iter++)
	   {
		   if (maxNum<=iter->second)
		   {
			   if (maxNum==iter->second)
			   {
                  str=iter->first;
				  if (minStr>str)
				  {
					  minStr=str;
					  maxNum=iter->second;
				  }
			   }
			   else
			   {
				   minStr=iter->first;
				   maxNum=iter->second;
			   }
		   }
	   }
	   ans.push_back(minStr);
	   mp.erase(minStr);
   }
   sort(ans.begin(),ans.end());
   for (vector<string>::iterator it=ans.begin();it!=ans.end();++it)
   {
	   cout<<*it<<" ";
   }
   cout<<endl;
   return 0;
}

你可能感兴趣的:(xmu 1438.程序员Astar的生活)