前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces】1220A - Cards

【Codeforces】1220A - Cards

作者头像
喜欢ctrl的cxk
发布2019-11-08 10:34:39
5900
发布2019-11-08 10:34:39
举报
文章被收录于专栏:Don的成长史Don的成长史

版权声明:本文为博主原创文章,遵循 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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Problem Description:
  • Input Specification:
  • Output Specification:
  • Sample Input1:
  • Sample Output1:
  • Sample Input2:
  • Sample Output2:
  • 解题思路:
  • AC代码:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档