码迷,mamicode.com
首页 > 编程语言 > 详细

01串排序

时间:2017-07-06 17:00:42      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:queue   cst   count   opera   end   sci   int   algo   type   

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

输入样例

10011111

00001101

1010101

1

0

1100

输出样例

0

1

1100

1010101

00001101

10011111

multiset集合的应用

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
struct cmp
{
    bool operator()(const string &s1,const string &s2)
    {
        if(s1.length()!=s2.length())return s1.length()<s2.length();
        int c1=count(s1.begin(),s1.end(),1);
        int c2=count(s2.begin(),s2.end(),1);
        return (c1!=c2?c1<c2:s1<s2);
    }
};
int main()
{
    multiset<string,cmp>s;
    string a;
    while(cin>>a)
    {
        s.insert(a);
    }
    for(multiset<string,cmp>::iterator it=s.begin();it!=s.end();it++)
        cout<<*it<<endl;
    return 0;
}

 

01串排序

标签:queue   cst   count   opera   end   sci   int   algo   type   

原文地址:http://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7126756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!