Codeforces 1220 – A. Cards solution

  • by
Codeforces 1220a - A. Cards solution in C/ C++

This problem is very simple. First You have to input an integer n. Then input a randomized string of n consisting of these five English lowercase letters: ‘z’, ‘e’, ‘r’, ‘o’ and ‘n’.

Now the problem said you have to sort/restore the randomized/shuffled string and 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 0 or “one” which corresponds to the digit 1.

Here is where the problem gets tricky. If you think hard and try to find each “one” and “zero”s from the rearranged string you may need to make a complicated code and possibly a timeout error.

Instead, we can solve this pretty easily. If you analyse carefully: Out of the five characters that formed the string, between the two words “zero” and “one” z,r are unique to the word “zero” and “n” is unique to the word “one” and occurs only once.

So, If we count the number of “z” or “r” we can get the total number of “zero” in the string.
Similarly, If we count “n” we will get the total occurrence of “one”.

Now that we have found the total number of “one” and “zeros” we have to print the maximum binary number possible of them. Which is easy. Print all the ‘1’ first then “0”

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,one = 0, zero = 0;
    cin >> n;
    char str[n];
    cin >> str;
    for(int i = 0; i<strlen(str); i++) {
        if(str[i] == 'z')
            ++zero;
        else if(str[i] == 'n')
            ++one;
    }
    while(one--) cout << "1 ";
    while(zero--) cout << "0 ";
}

Click to rate this post!
[Total: 9 Average: 4.2]