GOODBAD - Editorial

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.

I need help with my code.
LINK:

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.

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;
    }

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	ll t;
	cin >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		int upper = 0;
		int lower = 0;
		for(int i = 0; i < n; i++) {
			if(int(s[i]) >= 65 && int(s[i]) <= 90) {
				upper++;
			}  else {
				lower++;
			}
		}
		if(lower <= k && upper > k) {
			cout << "brother" << endl;
		} else if(lower > k && upper <= k) {
			cout << "chef" << endl;
		} else if(lower <= k && upper <= k) {
			cout << "both" << endl;
		} else {
			cout << "none" << endl;
		}
	}
}

I did a very simple way but instead of doing if the character of the string is uppercase I used the int of the character. I think that my way is worse. But I still got it right.

#include <bits/stdc++.h> 

#define ll long long int
#define ln "\n"
#define fast ios::sync_with_stdio(0); cin.tie(nullptr);

using namespace std;

string solve(string s, int k){
    int up=0;
    int lo=0;
    for(char c: s){
        if(isupper(c)){
            up++;
        }
        else if(islower(c)){
            lo++;
        }
    }
    // cout << up << " " << lo;
    if(lo <= k && up > k){
        return "brother";
    }
    else if(up <= k && lo <= k){
        return "both";
    }
    else if(lo > k && up <= k){
        return "chef";
    }
    return "none";
}


int main(){
    fast;

    int t; 
    cin >> t;

    while(t--){
        int n, k;
        string s;
        cin >> n >> k;
        cin >> s;
        cout << solve(s, k) << ln;
    }
    return 0;
}

MY CODE PLZ SEE THE SOLVE FUNCTION

#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

#define fio ios_base::sync_with_stdio(false), cin.tie(NULL),cout.tie(NULL)
#define PI 3.141592653589793238462
#define MOD 1000000007
#define INF 1e18
#define nl ‘\n’
#define pb push_back
#define ppb pop_back
#define f first
#define s second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define py cout << “YES” << nl
#define pn cout << “NO” << nl
#define loop(i,a,b) for (int i = a; i <= b; i++)
#define rloop(i,a,b) for (int i = a; i >= b; i–)
#define tc(t) int t; cin >> t; while (t–)
#define prec(n) fixed<<setprecision(n)
#define ini(a, i) memset(a, i, sizeof(a))
#define lli long long int
#define us unordered_set
#define um unordered_map
#define ll long long
#define ull unsigned long long
#define maxpq priority_queue
#define pii pair<int, int>
#define minpq priority_queue<int, vector, greater >

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
void google(int t) {cout << “Case #” << t << ": ";}
template <class T, class V> void _print(pair <T, V> p);
template void _print(vector v);
template void _print(set v);
template <class T, class V> void _print(map <T, V> v);
template void _print(multiset v);
template <class T, class V> void _print(pair <T, V> p) {cerr << “{”; _print(p.f); cerr << “,”; _print(p.s); cerr << “}”;}
template void _print(vector v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << “]”;}
template void _print(set v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << “]”;}
template void _print(multiset v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << “]”;}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << “]”;}

vector<vector> pre(int N){
vector<vector> divs(N);
loop(i, 1, N-1){
for(int j=i;j<N;j+=i)
divs[j].pb(i);
}
return divs;
}

vector sieve(int n) {intarr = new intn + 1; vector vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
ll add(ll x, ll y, ll m) {ll res=x+y; return res>=m ? res-m:res;}
ll mul(ll x, ll y, ll m) {ll res=x
y; return res>=m? res%m:res;}
ll sub(ll x, ll y, ll m) {ll res=x-y; return res<0? res+m:res;}
ll power(ll x, ll y, ll m) {ll res=1; x%=m; while(y){ if (y&1) res=mul(res, x, m); y >>=1; x=mul(x, x, m);} return res;}

void solve(){

tc(t){
    lli n,k;
    cin>>n>>k;
    string x;
    cin>>x;
    string y=x;
    string z=x;
    lli l=0;
    lli u=0;
    transform(y.begin(),y.end(),y.begin(),::tolower);
    transform(z.begin(),z.end(),z.begin(),::toupper);
    loop(i,0,x.length()-1){
        if(x[i]==y[i]){
            l++;
        }
        else if(x[i]==z[i]){
            u++;
        }
    }
    if(k>=l && k>=u){
        cout<<"both"<<nl;
    }
    else if(k<l && k<u){
        cout<<"none"<<nl;
    }
    else{
        if(k>=l){
            cout<<"brother"<<nl;
        }
        else{
            cout<<"chef"<<nl;
        }
    }
    
    
}

}

int main(){
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif

#ifndef ONLINE_JUDGE
freopen(“Error.txt”, “w”, stderr);
#endif

fio;
auto start1 = high_resolution_clock::now();
solve();
auto stop1 = high_resolution_clock::now();
auto duration = duration_cast(stop1 - start1);

#ifndef ONLINE_JUDGE
cerr << "Time: " << duration . count() / 1000 << endl;
#endif
}