天天看点

用map来统计字符串出现的次数

题目链接

#include<cstdio>
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
 int n,count;;
 string str,temp;
 while(scanf("%d",&n)&&n)
 {
  map<string,int>m;
  map<string,int>::iterator it;
  for(it=m.begin();it!=m.end();it++)
  {
   it->second=0;
  }
  count=0;
  for(int i=1;i<=n;i++)
  {
   cin >> str;
   m[str]++;
   if(m[str]>count) 
   {
    count=m[str];
    temp=str;
   }
  }
  cout << temp << endl;
 }
 return 0;
}