• POJ C++程序设计 编程题#7:字符串排序


    编程题#7:字符串排序

    来源: 北京大学在线程序评测系统POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

    总时间限制: 1000ms 内存限制: 1024kB

    描述

    请按照要求对输入的字符串进行排序。

    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    class A{
    private:
            string name;
    public:
            A(string n) :name(n){}
            friend bool operator < (const class A& a1, const class A &a2);
            friend bool operator == (const class A &a1, const class A &a2){
                    if (a1.name.size() == a2.name.size())
                            return true;
                    else
                            return false;
            }
            friend ostream & operator << (ostream &o, const A &a){
                    o << a.name;
                    return o;
            }
            string get_name() const{
                    return name;
            }
            int get_size() const{
                    return name.size();
            }
    };
    // 在此处补充你的代码
    int main(int argc, char* argv[])
    {
            list<A> lst;
            int ncase, n, i = 1;
            string s;
            cin >> ncase;
            while (ncase--){
                    cout << "Case: "<<i++ << endl;
                    cin >> n;
                    for (int i = 0; i < n; i++){
                             cin >> s;
                             lst.push_back(A(s));
                    }
                    lst.sort();
                    Show(lst.begin(), lst.end(), Print());
    
                    cout << endl;
                    lst.sort(MyLarge<A>());
                    Show(lst.begin(), lst.end(), Print());
                    cout << endl;
                    lst.clear();
            }
            return 0;
    }

    输入

    第一行是正整数T,表示测试数据的组数

    每组测试数据输入共两行,

    第一行是正整数N,表示字符串个数

    第二行是N个字符串, 字符串间用空格分离

     

    输出

    对于每组测试数据,先输出一行:

    Case: n

    如对第一组数据就输出Case: 1

    第二行按照字符串长度从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串长度相同的情况)

    第三行按照字符串首字符ASCII码序从小到大排序之后输出N个字符串,字符串之间以空格间隔(不会出现字符串首字母相同的情况)

     

    样例输入

    2
    4
    a bnss ds tsdfasg
    5
    aaa bbbb ccccd sa q

     

    样例输出

    Case: 1
    a ds bnss tsdfasg
    a bnss ds tsdfasg
    Case: 2
    q sa aaa bbbb ccccd
    aaa bbbb ccccd q sa

     1 #include <iostream>
     2 #include <string>
     3 #include <list>
     4 using namespace std;
     5 
     6 class A{
     7 private:
     8     string name;
     9 public:
    10     A(string n) :name(n){}
    11     friend bool operator < (const class A& a1, const class A &a2);
    12     friend bool operator == (const class A &a1, const class A &a2){
    13         if (a1.name.size() == a2.name.size())
    14             return true;
    15         else
    16             return false;
    17     }
    18     friend ostream & operator << (ostream &o, const A &a){
    19         o << a.name;
    20         return o;
    21     }
    22     string get_name() const{
    23         return name;
    24     }
    25     int get_size() const{
    26         return name.size();
    27     }
    28 };
    29 // 在此处补充你的代码
    30 bool operator< (const A& a1, const A &a2) {
    31     return a1.name.size() < a2.name.size();
    32 };
    33 
    34 template <class Iterator, class Function>
    35 void Show(Iterator begin, Iterator end, Function print) {
    36     for (Iterator iterator1 = begin; iterator1 != end; iterator1++) {
    37         print(*iterator1);
    38     }
    39 };
    40 
    41 class Print {
    42 public:
    43     void operator()(const A &a) {
    44         cout << a.get_name()<< " ";
    45     }
    46 };
    47 
    48 template <class A>
    49 struct MyLarge {
    50     inline bool operator()(const A &a1, const A &a2) {
    51         return a1.get_name() < a2.get_name();
    52     }
    53 };
    54 
    55 int main(int argc, char* argv[])
    56 {
    57     list<A> lst;
    58     int ncase, n, i = 1;
    59     string s;
    60     cin >> ncase;
    61     while (ncase--){
    62         cout << "Case: "<<i++ << endl;
    63         cin >> n;
    64         for (int i = 0; i < n; i++){
    65             cin >> s;
    66             lst.push_back(A(s));
    67         }
    68         lst.sort();
    69         Show(lst.begin(), lst.end(), Print());
    70 
    71         cout << endl;
    72         lst.sort(MyLarge<A>());
    73         Show(lst.begin(), lst.end(), Print());
    74         cout << endl;
    75         lst.clear();
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    第07组 Beta冲刺(3/5)
    第07组 Beta冲刺(2/5)
    第07组 Beta冲刺(1/5)
    第07组 Alpha事后诸葛亮
    第07组 Alpha冲刺(6/6)
    第07组 Alpha冲刺(5/6)
    【置顶】CSP/NOIP 2020 (高二游记)
    【置顶】CSP/S 2019退役祭
    【置顶】一些关于停课的感想(随时更新,连着前一篇博客)11/11~11/15
    【置顶】我的2018~2019信息学之路
  • 原文地址:https://www.cnblogs.com/dagon/p/4833352.html
Copyright © 2020-2023  润新知