GOODBAD - Editorial

acm17chn
cakewalk
chn17rol
goodbad

#1

PROBLEM LINK:

Practice

Contest

Author: Praveen Dhinwa

Tester: Misha Chorniy

Editorialist: Animesh Fatehpuria

PROBLEM

You just received a message given by a string s. You don’t know whether this message is sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous and hence can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most k such flips.

Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all capital letters. Determine whether the message could have been sent only by Chef, only by the little brother, by both or by none.

EXPLANATION

Number of uppercase letters <= k implies that it could have been Chef. This is because we can consider all the uppercase letters as “flips” made by the communication channel, which would imply that our original message comprised only of lowercase letters. Similarly, number of lowercase letters <= k implies that it could have been Chef’s brother. Here is the implementation in Python:

t = int(input())
for _ in range(t):
    (n, k) = list(map(int, input().split(' ')))
    s = input()
    chef = sum([1 for ch in s if ch >= 'A' and ch <= 'Z']) <= k
    brother = sum([1 for ch in s if ch >= 'a' and ch <= 'z']) <= k
    if (chef and brother):
        print("both")
    elif (chef):
        print("chef")
    elif (brother):
        print("brother")
    else:
        print("none")

AUTHOR’S AND TESTER’S SOLUTIONS:

Author’s solution can be found here.
Tester’s solution can be found here.


#2

I need help with my code.
LINK:


#3

Chef is a really nice and respectful person, in sharp contrast to his little brother, who is a very nasty and disrespectful person. Chef always sends messages to his friends in all small letters, whereas the little brother sends messages in all capital letters.

You just received a message given by a string s. You don’t know whether this message is sent by Chef or his brother. Also, the communication channel through which you received the message is erroneous and hence can flip a letter from uppercase to lowercase or vice versa. However, you know that this channel can make at most K such flips.

Determine whether the message could have been sent only by Chef, only by the little brother, by both or by none.


#4

In the main fucntion, look at these lines:

char arr[n];
cin>>arr;

Issues:

  • You shouldn’t declare static allocation inside the main function. These allocations will be done in stack space and with each test case the allocation will be done again. An alternate nice way of doing the same is to either do a dynamic memory allocation or declare “char arr[105]” globally. Note that the size of array is taken 105 which is greater than largest value of N.

  • Use scanf for reading a string. If you want to use cin, you can do with string class in C++.

    int main() {
    string s;
    cin >> s;
    }