现接收到一大批电子邮件,邮件地址格式为:用户名@主机域名,要求把这些电子邮件地址做主机域名的字典序升序排序,如果主机域名相同,则做用户名的字典序降序排序。
#include<bits/stdc++.h>
// 重新定义std::string的小于号运算符
bool operator<(const std::string& a, const std::string& b) {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
}
// 在solve类中实现小于号运算符
class solve {
public:
std::string a, b;
bool operator<(const solve& S) const {
if (b != S.b) {
return b < S.b;
}
return a > S.a;
}
};
signed main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n; std::cin >> n;
std::vector<solve> v(n);
for (int i = 0; i < n; i++) {
std::string email;
std::cin >> email;
int pos = email.find('@');
v[i].a = email.substr(0, pos);
v[i].b = email.substr(pos + 1);
}
std::sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
std::cout << v[i].a << '@' << v[i].b << '\n';
}
}