【Codeforces】1220A – Cards
Problem Description:
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.
Input Specification:
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.
Output Specification:
Print the maximum possible number in binary notation. Print binary digits separated by a space. The leading zeroes are allowed.
Sample Input1:
4
ezor
Sample Output1:
Sample Input2:
10
nznooeeoer
Sample Output2:
1 1 0
解题思路:
水题,先unordered_map统计每个字符的个数,然后贪心算法,能拼成一个one是一个one,不能one了再来拼zero。
AC代码:
#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);
//先按照one来排再zero
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;
}
原文链接:【Codeforces】1220A - Cards
麦芽雪冷萃 版权所有,转载请注明出处。
还没有任何评论,你来说两句吧!