【Codeforces】1220A - Cards

2019-11-08 10:34:39 浏览数 (1)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/102400319

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:

代码语言:javascript复制
4
ezor

Sample Output1:

代码语言:javascript复制
0

Sample Input2:

代码语言:javascript复制
10
nznooeeoer

Sample Output2:

代码语言:javascript复制
1 1 0

解题思路:

水题,先unordered_map统计每个字符的个数,然后贪心算法,能拼成一个one是一个one,不能one了再来拼zero。

AC代码:

代码语言:javascript复制
#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;
}

0 人点赞