CORTSENT - Editorial

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3

Author: Daanish Mahajan
Tester: Manan Grover
Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Chef knows two languages spoken in Chefland but isn’t proficient in any of them. The first language contains characters [a−m] and the second language contains characters [N−Z].

Given K words of the sentence as S_1, S_2,…, S_K tell whether it is a possible sentence framed by Chef, i.e, it contains only the characters from the two given languages and each word contains characters from a single language.

EXPLANATION:

We are given K words of the sentence, we just need to check whether each word belongs to exactly one language.

A word belongs to the first language if all the characters of the word belong to [a-m], and it belongs to the second language if all the characters of the word belong to [N-Z]. If a word contains the characters of both the languages or some characters are neither in language first nor in second, then the sentence is not valid.

Pseudo Code to check the word
// s is some word of a sentence

for(auto ch: s)
{
      if(ch>='a' && ch<='m')
        fst=false;
      else if(ch>='N' && ch<='Z')
        snd=false;
      else
        ok=false;
}

if(!fst && !snd)
    ok=false;
 

TIME COMPLEXITY:

O(|S|) per test case

where |S| is the length of sentence

SOLUTIONS:

Setter
Tester
Editorialist
#include<bits/stdc++.h>
using namespace std;
 
#define int long long
 
void solve()
{
  int k;
  cin>>k;
 
  bool ok=true;
 
  for(int i=0;i<k;i++)
  {
    string s;
    cin>>s;
 
    if(!ok)
      continue;
 
    bool fst=true,snd=true;
 
    for(auto ch: s)
    {
      if(ch>='a' && ch<='m')
        fst=false;
      else if(ch>='N' && ch<='Z')
        snd=false;
      else
        ok=false;
    }
 
    if(!fst && !snd)
      ok=false;
  }
 
  if(ok)
    cout<<"Yes"<<"\n";
  else
    cout<<"NO"<<"\n";
}
 
int32_t main()
{
  ios_base::sync_with_stdio(0);
  cin.tie(0);
 
  int t;
  cin>>t;
 
  while(t--)
    solve();
 
return 0;
}
4 Likes

can you please find the error in this solution - CodeChef: Practical coding for everyone

I also tried to solve with this similar algo and i also cant find why the output is coming wrong.
https://www.codechef.com/viewplaintext/47096784

it will fail for the case where only one type of language is used, like a case
1
2 a a

Someone, please help me too:CodeChef: Practical coding for everyone

Solution: 47029244 | CodeChef you might be going wrong on some condition you can check the solution here

can anyone help me to find error in this solution Solution: 47053606 | CodeChef

yeah. thanks! I interpreted the question wrong

please tell me where my code will go wrong

what is wrong with my code.
https://www.codechef.com/viewsolution/47107750

There are “K” words to be checked and you’re calling return on the first wrong word. What about the other words?

cant understand why we have to check for all other words if we got a wrong word??

1 Like

Can any one find any error in this code why it is not submitting…

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

void solve();
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);

int t;  cin>>t;
while(t--)
{
   solve();
}

return 0;

}

void solve()
{
int k;
cin>>k;

    string s;
    bool flag=true,c1=false,c2=false,c3=false,c4=false;
    for(int i=0;i<k;k++)
    {   
        cin>>s;
         flag=true;c1=false;c2=false;c3=false;
         if((s[0]>='A' && s[0]<='M') || ( s[0]>='n' && s[0]<='z' ) )
         {    c1=true;
              cout<<"NO\n" ;
              return;
              //break;
         }
         else if(s.size()==1 && ( (s[0]>='A' && s[0]<='M') || ( s[0]>='n' && s[0]<='z' ) ) )
         {
              c4=true;
              cout<<"NO\n" ;
              return;
         }
         else if(s[0]>='a' && s[0]<='m') {
              for(int j=0;j<s.size();j++)
              {
                   if(s[j]<'a' && s[j]>'m') {
                    // flag=false;
                    c2=true;
                     cout<<"NO\n" ;
                     return;
                    //break;
                    }
              }
         }
         else if(s[0]>='N' && s[0]<='Z') {
                for(int j=0;j<s.size();j++)
                {
                   if(s[j]<'N' && s[j]>'Z') {
                     //flag=false;
                     c3=true;
                     cout<<"NO\n" ;
                     return;
                    // break;
                   }
              }
         }
        

     //   flag ? cout<<"YES\n" : cout<<"NO\n" ;


        
    }
    if(!c1 || !c2 || !c3 || !c4)
    cout<<"YES\n";

}

You don’t have to check for all words but you still need to take input for all k.
Consider the case -

2
2 no no
1 AB

The desired output should be-

NO
NO

@itaachi

Edit: Also @hackbhai , whatever you logic is, just consider the case above and amend your code accordingly.

3 Likes

i am not able to figure out why this is not working properly.
for 3 test cases:
3
2 a a
2 NO
2 M m
for the last test case its giving me a yes which is not correct. Please help
#include
#include
using namespace std;
void solver()
{
long long int t;
cin>>t;

while(t--)
{
     int k;
     cin>>k;
     int flag=0;
     for(int i=0;i<k;i++)
     {
         string s;
         
         cin>>s;
         
         if(s[0]>=97 && s[0]<=109)
         {
             
             int j=0;
             while(s[j]!='\0')
             {
                 if(s[j]<97 || s[j]>109)
                 {
                     flag=1;
                     cout<<"NO"<<endl;
                     break;
                 }
                 j++;
             }
             if(flag==1)
             {
                 break;
             }
         }else if(s[0]>=78 && s[0]<=90)
         {
              int j=0;
             while(s[j]!='\0')
             {
                 if(s[j]<78 || s[j]>90)
                 {
                     flag=1;
                     cout<<"NO"<<endl;
                     break;
                 }
                 j++;
             }
             if(flag==1)
             {
                 break;
             }
             
         }else
         {
             flag=1;
             cout<<"NO"<<endl;
             break;
         }
     }
     if(flag==0)
     {
         cout<<"yes"<<endl;
     }
   
           
}  

}
int main() {
// your code goes here
solver();
return 0;
}

your input is invalid

do not return unless you take each of the input of current test case, otherwise they will be considered as the input for the next test case

how?

each test case should have number of strings equal to k , see the second test case
3
2 a a
2 NO ???
2 M m

that was a typo
but its still give me wrong answer, can you help me what i m doing wrong