01串排序
将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; }