My code is working fine on my cpu compiler but on codechef it shows wrong answers

the link to the problem is : https://www.codechef.com/JAN19B/problems/FANCY
my attempt is :

#include <iostream>
#include <string.h>
 using namespace std;
 
 int main () {
     string str2 (" not "); 
     string str3 ("not "); 
     string str4 ("not"); 
     
     int T;
     cin>>T;
     for (int k=0;k<T;k++){
     string str ;


    cin >> str;

// read the newline character
getchar();
     int len = str.size();
    //condition for only not as a sentence
             if ((str.find(str4) != string::npos) && len ==3) {
cout<<"Real Fancy";
} 

// condition to check for not word in middle of a sentence eg. this is not good
else if ((str.find(str2) != string::npos) ) {
cout<<"Real Fancy";
} 

// condition if the statement ends with the word not 
else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){
    cout<<"Real Fancy";
}

// code to check for if statement starts with word not
else if ((str.find(str3) != string::npos) ) {
cout<<"Real Fancy";
} 
else {
    cout<<"regularly fancy";
}
cout<<endl;
}
     return 0;
 }

codechef custom input trial was :

but my output on some online compiler was correct :

Welcome to codechef is all I can say✌️

2 Likes

you dont know how to fix this ? i m all screwed up because of this

how much time did you take to gain your first star ?

Well I have been doing competition from 3-4 months …I have been very near to 3 * and right now I am at very low rating

You should use something like
If(s.find(“not”)!=-1)
Cout<<“realfancy”;
else
Cout<<“regular fancy”;
This will check if you have not in the string or not

yup when i saw you participated quite well in lunchtime and other cookoffs
i got amazed how come you are still a one star coder.
so whats your story how did you lose and slide down to one star ?

It keeps happening in competitive programming …nothing to worry… You learn from experiences

1 Like

but but this will give wrong answer
suppose the string is “This is nothing”
then also we will get output as “Real Fancy”
but this will be wrong.
you got my point ?

Then change the string to (space) not(space)

You can check the editorial it will help

then what if the input string is “(no space)not(no space)”
again the output will be wrong

besides can you help me how to start learning for CP
as i just started coding in C++ and straight away came to codechef as its really challenging
So what all topics should i start to learn first ??

Well if you have just started then you should get your basics fixed first…try to solve easy problems first then move to codechef contests . Like you can go to codeforces and start solving problems in descending order of submission it will help you to boost your confidence and for C++ you must learn STL so that you can use concepts of map stack set and all in you programme .don’t just rush because you won’t be a great programmer in a day .it will take time and if you keep working hard then results will follow so for now I will suggest you two things

  1. Do codeforces from descending order of submission
    2.learn from geek for geeks
    And keep trying in the live challenges no matter how your rating is as it will increase eventually if you are working hard constantly…hope this helps
1 Like

I am about to post a collection of link’s I have found out for preparing for ACM-ICPC …it might help you a lot … I am just working to fix it’s alignment asap

thnx for support
BTW i found the problem myself teh problem is that once i take an input from the user what my compiler does is it stores a all characters before a whitespace and then the next word is automatically stored into another string for instance
suppose my custom input console ;looks like :
3
not working
hey
what is this
the output i wil get will be
Real FAncy // as it sees not as one string
regularly fancy // sees working as the 2nd string 2nd word of the first string
regularly fancy // sees hey as the third string

can anyone help me get all the words entered once into a single string variable ???

https://en.cppreference.com/w/cpp/string/basic_string/getline

3 Likes

with getline i got problem too

code is here : (using getline)

#include <iostream>
#include <string.h>
 using namespace std;
 
 int main () {
     string str2 (" not "); 
     string str3 ("not "); 
     string str4 ("not"); 
     
     int T;
     cin>>T;
     for (int k=0;k<T;k++){
     string str ;


getline(cin,str);
     int len = str.size();
    //condition for only not as a sentence
             if ((str.find(str4) != string::npos) && len ==3) {
cout<<"Real Fancy";
} 

// condition to check for not word in middle of a sentence eg. this is not good
else if ((str.find(str2) != string::npos) ) {
cout<<"Real Fancy";
} 

// condition if the statement ends with the word not 
else if (str[len-1]=='t' && str[len-2]== 'o' && str[len-3]== 'n' && str[len-4]== ' '){
    cout<<"Real Fancy";
}

// code to check for if statement starts with word not
else if ((str.find(str3) != string::npos) ) {
cout<<"Real Fancy";
} 
else {
    cout<<"regularly fancy";
}
cout<<endl;
}
     return 0;
 }

bu the output i got :