版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42449444/article/details/102400319
When Serezha was three years old, he was given a set of cards with letters for his birthday. They were arranged into words in the way which formed the boy's mother favorite number in binary notation. Serezha started playing with them immediately and shuffled them because he wasn't yet able to read. His father decided to rearrange them. Help him restore the original number, on condition that it was the maximum possible one.
The first line contains a single integer n (1⩽n⩽105) — the length of the string. The second line contains a string consisting of English lowercase letters: 'z', 'e', 'r', 'o' and 'n'.
It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either "zero" which corresponds to the digit 00 or "one" which corresponds to the digit 11.
Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.
4
ezor
0
10
nznooeeoer
1 1 0
水题,先unordered_map统计每个字符的个数,然后贪心算法,能拼成一个one是一个one,不能one了再来拼zero。
#include<bits/stdc++.h>
using namespace std;
#define Up(i,a,b) for(int i = a; i <= b; i++)
int main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n;
cin >> n;
cin.ignore();
string str;
getline(cin,str);
unordered_map<char,int> m;
for(auto it : str)
{
m[it]++;
}
vector<int> ans; //用来存放结果
while(m['o'] && m['n'] && m['e'])
{
m['o']--,m['n']--,m['e']--;
ans.push_back(1);
}
while(m['z'] && m['e'] && m['r'] && m['o'])
{
m['z']--,m['e']--,m['r']--,m['o']--;
ans.push_back(0);
}
int sz = ans.size()-1;
Up(i,0,sz)
{
cout << (i==0?"":" ") << ans[i];
}
return 0;
}