01串排序

Description:

将01串首先按长度排序,长度相同时,按1的个数多少进行排序,1的个数相同时再按ASCII码值排序。

 Input:

输入数据中含有一些01串,01串的长度不大于256个字符。

OutPut:

重新排列01串的顺序。使得串按基本描述的方式排序。

Sample Input:

10011111
00001101
1010101
1
0
1100

Sample Output:

0
1
1100
1010101
00001101
10011111
心得:学会巧妙应用三目操作符,简化代码。

            STL中习题不要用手写的循环。比如如果仅是输出操作可以用copy算法输出,一行代码就可以搞定。

代码如下:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
struct myCmp
{
    bool operator()(const string& str1,const string& str2)
    {
        //if (str1.length() != str2.length())
        //{
        //    return str1.length() < str2.length();
        //}
        //else
        //{
        //    int num1 = count(str1.begin(), str1.end(), '1');
        //    int num2 = count(str2.begin(), str2.end(), '1');
        //    /*if (num1 != num2)
        //    {
        //        return num1 < num2;
        //    }
        //    else
        //    {
        //        return str1 < str2;
        //    }*/
        //    return (num1 != num2 ? num1 < num2 : str1 < str2);
        //}
        if (str1.length() != str2.length())
        {
            return str1.length() < str2.length();
        }
        int num1 = count(str1.begin(), str1.end(), '1');
        int num2 = count(str2.begin(), str2.end(), '1');
        return (num1 != num2 ? num1 < num2 : str1 < str2);

    }
};
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        multiset<string, myCmp> coll;
        string str;
        while (cin>>str)
        {
            coll.insert(str);
        }
       /* multiset<string, myCmp>::iterator it;
        for (it = coll.begin(); it != coll.end(); it++)
        {
            cout << *it << endl;
        }*/
        ostream_iterator<string> os(cout, "\n");
        copy(coll.begin(), coll.end(), os);
        return 0;
}

本题还有一种扩展形式,即只按 01 串中 1 的个数的多少进行升序排序。

代码中如果1的个数相同,按出现的先后顺序排序,用

return num1<num2;

如果1的个数相同,要按ASCII码进行排序,则用

return (num1!=num2?num1<num2:str1<str2);

代码如下:

#include<iostream>
#include<fstream>
#include<set>
#include<string>
#include<queue>
#include<set>
#include<iterator>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
using namespace std;
struct myCmp
{
    bool operator()(const string& str1,const string& str2)
    {
        int num1 = count(str1.begin(), str1.end(), '1');
        int num2 = count(str2.begin(), str2.end(), '1');
        //如果1的数量不等,按1的个数排序
        //如果1的数量相等,则按出现的先后顺序排序
        //只能用“>”或“<”,不能用“=”
        // return num1<num2;
        //如果1的数量相等,要按ASCII码大小排序
        return (num1 != num2 ? num1 < num2 : str1 < str2);
    }
};
int main()
{
    #ifdef ONLINE_JUDGE
    #else
        freopen("D:\\in.txt", "r", stdin);
        freopen("D:\\out.txt", "w", stdout);
    #endif
        vector<string> coll;
        string str;
        while (cin>>str)
        {
            coll.push_back(str);
        }
        sort(coll.begin(), coll.end(), myCmp());
        copy(coll.begin(), coll.end(), ostream_iterator<string>(cout, "\n"));
        return 0;
}

### C语言实现01排序算法 对于01排序问题,可以采用计数排序的思想来高效解决。由于输入仅由`0`和`1`组成,因此可以通过统计字符中`0`的数量(或`1`的数量),然后重新构建一个按顺序排列的新字符。 以下是基于C语言的具体实现: #### 思路描述 通过遍历整个字符,计算出`0`的数量。随后按照数量依次填充到新字符中,先填入所有`0`,再填入剩余位置上的`1`[^1]。 #### 实现代码 ```c #include <stdio.h> #include <string.h> void sortBinaryString(char *str) { int length = strlen(str); int countZero = 0; // 统计'0'的数量 for (int i = 0; i < length; i++) { if (str[i] == '0') { countZero++; } } // 构建新的有序字符 for (int i = 0; i < countZero; i++) { str[i] = '0'; } for (int i = countZero; i < length; i++) { str[i] = '1'; } } int main() { char binaryStr[] = "10101010"; printf("原始字符: %s\n", binaryStr); sortBinaryString(binaryStr); // 调用排序函数 printf("排序后的字符: %s\n", binaryStr); return 0; } ``` #### 运行结果示例 假设输入字符为 `"10101010"`,运行程序后输出如下: ``` 原始字符: 10101010 排序后的字符: 00001111 ``` --- ### 复杂度分析 - **时间复杂度**: 遍历字符两次完成统计与赋值操作,总的时间复杂度为 \(O(n)\)[^2]。 - **空间复杂度**: 使用原地修改的方式,无需额外的空间分配,故空间复杂度为 \(O(1)\)[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值