排列对称串

Description:很多字串,有些是对称的,有些是不对称的,请将那些对称的字事按从小到大的顺序输出,字事先以长度论大小,如果长度相同,再以ASCI码值为大小标准

Input.输入数据中含有一些字串(1≤串长≤256)。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class String {
public:
    String(const string& s) : str_(s) {}  // 判断字符串是否对称
    bool pd() const {
        int left = 0, right = str_.size() - 1;
        while (left < right) {
            if (str_[left] != str_[right]) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }
    bool operator<(const String& other) const {
        if (str_.size() != other.str_.size()) {
            return str_.size() < other.str_.size();  }
        return str_ < other.str_;}
    const string& Get() const {
        return str_;
    }
private:
    string str_;
};
int main() {
    vector<String> fn;
    string s;
    while (cin >> s) { fn.push_back(String(s)); }
    vector<String> ff;
    copy_if(fn.begin(), fn.end(),
        back_inserter(ff),
        [](const String& ss) { return ss.pd(); });
   sort(ff.begin(), ff.end());
    for (const auto& ss : ff) {
       cout << ss.Get() << endl;
    }
    return 0;
}

 

这种情况下,无法实现回车两次直接输出结果,借助Ctrl+z两次输出结果,跳出循环

while (cin >> s) { fn.push_back(String(s)); }

改为

while (getline(cin, s) && !s.empty()) {
    fn.push_back(String(s));
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class String {
public:
    String(const string& s) : str_(s) {}  // 判断字符串是否对称
    bool pd() const {
        int left = 0, right = str_.size() - 1;
        while (left < right) {
            if (str_[left] != str_[right]) {
                return false;
            }
            ++left;
            --right;
        }
        return true;
    }
    bool operator<(const String& other) const {
        if (str_.size() != other.str_.size()) {
            return str_.size() < other.str_.size();  }
        return str_ < other.str_;}
    const string& Get() const {
        return str_;
    }
private:
    string str_;
};
int main() {
    vector<String> fn;
    string s;
    while (getline(cin, s) && !s.empty()) {
    fn.push_back(String(s));
}
    vector<String> ff;
    copy_if(fn.begin(), fn.end(),
        back_inserter(ff),
        [](const String& ss) { return ss.pd(); });
   sort(ff.begin(), ff.end());
    for (const auto& ss : ff) {
       cout << ss.Get() << endl;
    }
    return 0;
}

这个时候可以直接借助两次回车将结果输出

 思路如下:

  1. 定义了一个名为String的类,构造函数接受一个string类型的参数s,初始化私有成员变量str_。类中包含了三个成员函数:

    • pd():检查字符串是否对称(即回文串)。通过两个指针leftright分别从字符串两端开始向中间移动,如果在移动过程中遇到不相等的字符,则返回false,否则遍历完成后返回true,表示字符串是对称的。
    • operator<:重载小于运算符,用于在排序时比较两个String对象。首先比较字符串长度,长度短的更小;长度相同时,按照字典序比较字符串内容。
    • Get():获取封装的字符串。
  2. main()函数流程:

    • 初始化一个vector容器fn用于存储用户输入的字符串。
    • 通过循环从标准输入(通常是键盘)读取字符串s,并将每个字符串实例化为String对象后推入fn容器中。
    • 使用copy_if算法将满足对称条件(即pd()返回true)的String对象复制到新的vector容器ff中。
    • ff容器中的String对象进行排序。
    • 遍历排序后的ff容器,输出每个String对象所封装的字符串内容。
  • 19
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

筱姌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值