知识点:
关于重载<:
可以看到与priority_queue中的重载是相反的
一般:return < 时表示 升序;
但在priority_queue中return < 时表示 降序;
可跳转:P3366
ac代码:
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
struct node{
string s;
int u;
bool operator <(const node& n2) const{
if(u==n2.u){
if(s.compare(n2.s)<0) return true;
else return false;
}
return u>n2.u;
}
};
int n,m,ans;
map<string,int>p;
map<node,int>q;
int main(){
cin>>n>>m;
getchar(); //!!!!!! 如果要使用getline(cin,str) 要保证前面没有\n;
for(int i=1;i<=n;i++){
string str;
getline(cin,str);
if(p[str]) p[str]++;
else{
p[str]=1; ans++;
}
}
for(auto it=p.begin();it!=p.end();++it){
node nod;
nod.s=it->first;
nod.u=it->second;
q[nod]=1;
}
printf("%d\n",ans);
for(auto it=q.begin();it!=q.end();++it)
cout<<it->first.u<<" "<<it->first.s<<endl;
return 0;
}