AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

自用重载字符串排序算法

作者: 作者的头像   Ascension ,  2023-05-26 20:17:34 ,  所有人可见 ,  阅读 36


0


现接收到一大批电子邮件,邮件地址格式为:用户名@主机域名,要求把这些电子邮件地址做主机域名的字典序升序排序,如果主机域名相同,则做用户名的字典序降序排序。

#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';
    }
}


0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息