CORTSENT - Editorial

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

can anyone tell me what is wrong with this?

#include
#include
using namespace std;

void abc(int k)
{
int c=0;
while(k–)
{
string s;
cin>>s;
int q=0,r=0;
for(int i=0;i<s.length();i++)
{
if(int(s[i])>=97 && int(s[i])<=109)
q++;
else if(int(s[i])>=78 && int(s[i])<=90)
r++;
}
if(q==s.length() || r==s.length())
c++;
}
if(c==k)
cout<<“YES”<<endl;
else
cout<<“NO”<<endl;
}

int main() {
// your code goes here
long long int t;
cin>>t;
while(t–)
{
int k;
cin>>k;
abc(k);
}
return 0;
}

Solution: 47069486 | CodeChef
Can anyone help me with this? I have tried all possible test cases which I can get.

No need to check all words just put a condition that will break out of the loop in case any condition hits wrong.

even if you find a wrong word, you should take all the inputs of the given test case ( so do not break the loop , instead take all the inputs and then break), otherwise the program will start reading input from where it ended in previous case,
eg
3
2 A a
4 A a a N
3 a b c
expected output :
no
no
yes
but the program will give output
no
yes
yes

(to realize this , you need to dry run on pen-paper after changing every value to their respective ASCII values, and when you break the loop, next time start another just after the point you broke )

2 Likes

@anon64872407 I have not looked very carefully, but maybe because at line no 17 you have run the loop till ‘n’, while the question says till ‘m’

thanks bro, You are right and now it’s working properly

your are right bro now it’s working properly

ya i got it now thank you.

https://www.codechef.com/viewsolution/47134599
please tell me why my solution is wrong…!!
please help

Bro if i break it,then its just taking the value of k to be 0. I m not able to understand that why break is creating a problem even after a dry run. please help