题目描述
输入n组单词,统计不同单词出现的个数,并按照字典顺序输出。(n<1000,每个单词长度小于1000)
样例输入
4
aab
aab
aa
bc
样例输出
aa 1
aab 2
bc 1
使用map容器可以比较容易做出
#include <iostream>
#include <map>
#include <iterator>
#include <stdlib.h>
using namespace std;
main()
{
typedef map<string,long >word;
int len ;
word pairs;
long k = 1;
cin>>len;
string *f = new string [len];
for(int i=0;i<len;i++)
{
cin>>f[i];
if(pairs.count(f[i]) == 0)
{
pairs.insert(make_pair(f[i],k));
}
else{
pairs[f[i]] ++;
}
}
word::iterator j;
for(j = pairs.begin(); j!= pairs.end(); j++)
cout<<j->first<<" "<<j->second <<endl;
pairs.clear();
return 0;
}