Bear and Milky Cookies Codechef Solution

Limak is a little polar bear, who loves eating cookies and drinking milk. For this reason he often visits Chef’s kitchen.

Limak is going to spend N minutes in the kitchen. Each minute he either eats a cookie or drinks milk. Cookies are very sweet and thus Limak’s parents have instructed him, that after eating a cookie, he has to drink milk in the next minute.

You are given whether he ate a cookie or drank milk in each of the N minutes. Your task is to check if Limak followed his parents’ instructions. That is, you need to verify whether after each eaten cookie he drinks milk in the next minute. Print “YES” or “NO” for each test case accordingly.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains an integer N denoting the number of minutes.

The second line of a test case contains N space-separated strings S1S2, …, SN. The string Si is either “cookie” (if Limak eats a cookie in the i-th minute) or “milk” (otherwise).

Output

For each test case, output a single line containing the answer — “YES” if Limak followed his parents’ instructions, and “NO” otherwise, both without the quotes.

Constraints

  • 1 ≤ T ≤ 50
  • 1 ≤ N ≤ 50
  • Each Si is either “cookie” or “milk” (without the quotes).

Subtasks

  • Subtask #1 (40 points) N = 2
  • Subtask #2 (60 points) Original constraints

Sample Input 1 

4
7
cookie milk milk cookie milk cookie milk
5
cookie cookie milk milk milk
4
milk milk milk milk
1
cookie

Sample Output 1 

YES
NO
YES
NO

Bear and Milky Cookies   – CodeChef Solution in JAVA

import java.util.*;
import java.io.*;
public class Main {
	
	public static void main(String[] args) throws IOException {
try{	
    Scanner sc=new Scanner(System.in);
   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int t=Integer.parseInt(br.readLine());
   while(t-->0){   
    int n=Integer.parseInt(br.readLine());
   String s1=br.readLine();
   String[] s=s1.split(" ");
    boolean flag=true;
   String c="cookie";
   if(s[n-1].compareTo(c)==0)
    flag=false;
   for(int i=0;i<n-1;i++){
    	if(s[i].compareTo(s[i+1])==0&&s[i].compareTo(c)==0)
    	 flag=false;
    }
   
   if(flag)
    System.out.println("YES");
   else
    System.out.println("NO");
	}
	
	}catch(Exception e){return;}
	}
}

Bear and Milky Cookies – CodeChef Solution in CPP

#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<b;i++) 
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        string s[n];
        For(i,0,n){
            cin>>s[i];
        }
        bool ans=1;
        For(i,0,n){
            if(s[i]=="cookie" && s[i+1]!="milk"){
                ans=0;
                break;
            }
        }
        if(ans) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

Bear and Milky Cookies   -CodeChef Solution in Python

for t in range(int(input())):
    N = int(input())
    S = input()
    if (S[-4:] != "milk"):
        print("NO")
    else:
        if "cookie cookie" in S:
            print("NO")
        else:
            print("YES")

Disclaimer: The above Problem (Bear and Milky Cookies  ) is generated by CodeChef but the solution is provided by Codeworld19.This tutorial is only for Educational and Learning purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *