思路 stl也太棒了!直接用set去重得到不同标签数目,定义结构体存储全部信息,,然后sort排序,当然sort排序的比较函数需要自己定义。
#include<iostream>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
struct p
{
string name;
int key, sum;
friend ostream& operator<<(ostream& os, const p a)
{
cout << a.name;
return os;
}
};
bool cmp(p& a, p& b)
{
if (a.key == b.key)
{
return a.sum < b.sum;
}
return a.key > b.key;
}
int main()
{
int n;
cin >> n;
vector<p> arr(n);
for (int i = 0; i < n; i++)
{
set<int> book;
cin >> arr[i].name >> arr[i].sum;
for (int j = 0; j < arr[i].sum; j++)
{
int temp;
cin >> temp;
book.insert(temp);
}
arr[i].key = book.size();
}
sort(arr.begin(), arr.end(), cmp);
if (n >= 3)
{
cout << arr[0]<< ' ' << arr[1]<< ' '<<arr[2];
}
else if (n == 2)
{
cout << arr[0] << ' ' << arr[1] << ' ' << '-';
}
else
{
cout << arr[0] << ' ' << '-' << ' ' << '-';
}
}
|