#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
struct Comp {
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, Comp> ms;
string s;
while(cin >> s) {
ms.insert(s);
}
for(multiset<string, Comp>::iterator it = ms.begin(); it != ms.end(); it ++) {
cout << *it << endl;
}
return 0;
}zoj1204给01串排序
最新推荐文章于 2024-04-17 09:51:31 发布
本文介绍了一个使用C++实现的字符串集合排序程序。该程序利用了multiset容器结合自定义比较器来对输入的字符串进行排序。排序依据为字符串长度及其中包含的'1'字符的数量,并最终按照字典序进行微调。
2198

被折叠的 条评论
为什么被折叠?



