Help for "Baking Business" problem

Hi all,

i submitted my code for “baking business problem” - BAKE Problem - CodeChef
It was giving me correct answer for all my test cases but it gives “wrong answer” when i submit

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stdlib.h>
using namespace std;

int main()
{
/* The number of inputs*/
int numberOfInputs;

/* The Map storing values of input type I*/
map<int,vector<string> > Imap;

/* The Vector storing values of input type Q*/
vector<string> Qvec;

/*Vector stporing the value of Output*/
vector<int> output;

cin  >> numberOfInputs;

for (int i=0; i< numberOfInputs; i++)
{
 vector<string> inputValues;  
 string inputType;
 string productSize;
 string provinceDetail;
 string gender;
 string age;
 string unitsSold;
 cin >> inputType;
 if ( inputType == "I")
 {
    cin >> productSize >> provinceDetail >> gender >> age >> unitsSold;
    inputValues.push_back(productSize);
    inputValues.push_back(provinceDetail);
    inputValues.push_back(gender);
    inputValues.push_back(age);
    inputValues.push_back(unitsSold);
    Imap.insert(pair<int,vector<string> >(i,inputValues) );
 }
 
 else if(inputType == "Q")
 {
    int result = 0;
    string ageRange;
    cin >> productSize >> provinceDetail >> gender >> ageRange;
    Qvec.push_back(productSize);
    Qvec.push_back(provinceDetail);
    Qvec.push_back(gender);
    Qvec.push_back(ageRange);
	
for(map<int,vector<string> > ::iterator itI = Imap.begin();itI != Imap.end();itI++)
{
        int prodI,prodQ,sizeI,sizeQ;
        int posI = itI->second.at(0).find(".");
        int posQ = Qvec.at(0).find(".");
      /* If there is no decimal separation*/
        if(posI == -1) { prodI = atoi(itI->second.at(0).c_str());  sizeI =0;}   
        if(posQ == -1) { prodQ = atoi(Qvec.at(0).c_str());  sizeQ =0;}
        /*If there is subdivision, then the string values are converetd to integer*/
        if(posI != -1)
        {
          prodI =  atoi(itI->second.at(0).substr(0,posI).c_str());
          int size1 = itI->second.at(0).size();  
          sizeI = atoi(itI->second.at(0).substr(posI+1,size1).c_str());
        }
        if(posQ != -1)
        {
          prodQ =  atoi(Qvec.at(0).substr(0,posI).c_str());
          int size1 = Qvec.at(0).size();  
          sizeQ = atoi(Qvec.at(0).substr(posI+1,size1).c_str());
        }
    string IProv,IReg,ICity;
        string QProv,QReg,QCity;
        int Ip,Ic,Ir;
        int Qp,Qc,Qr;
        int posPI = itI->second.at(1).find(".");
        int posPQ = Qvec.at(1).find(".");
        if( posPI == -1) {Ip = atoi(itI->second.at(1).c_str()); Ic = 0; Ir=0;}
        if( posPQ == -1) {Qp = atoi(Qvec.at(1).c_str()); Qc = 0; Qr=0;}
    if(posPI != -1)
        {
          Ip =  atoi(itI->second.at(1).substr(0,posPI).c_str());
          int sizePI = itI->second.at(1).size(); 
          ICity = itI->second.at(1).substr(posPI+1,sizePI);
          Ic = atoi(itI->second.at(1).substr(posPI+1,sizePI).c_str()); 
          int posPI1 = ICity.find(".");
          if(posPI1 == -1) { Ir = 0;}   
          if(posPI1 != -1) 
          {
             int sizePI2 = itI->second.at(1).substr(posPI+1,sizePI).size(); 
             Ic = atoi(ICity.substr(0,posPI1).c_str()); 
             Ir = atoi(ICity.substr(posPI+1,sizePI2).c_str());
           }   
        }
        if(posPQ != -1)
        {
          Qp =  atoi(Qvec.at(1).substr(0,posPI).c_str());
          int sizePI = Qvec.at(1).size(); 
          QCity = Qvec.at(1).substr(posPI+1,sizePI);
          Qc = atoi(Qvec.at(1).substr(posPI+1,sizePI).c_str()); 
          int posPI1 = QCity.find(".");
          if(posPI1 == -1) { Qr = 0;}   
          if(posPI1 != -1) 
          {
             int sizePI2 = Qvec.at(1).substr(posPI+1,sizePI).size(); 
             Qc = atoi(QCity.substr(0,posPI1).c_str()); 
             Qr = atoi(QCity.substr(posPI+1,sizePI2).c_str());
           }   
        } 
		
    if( ((prodI == prodQ && sizeI == sizeQ) || ( prodI == prodQ && sizeQ == 0) ||(Qvec.at(0) == "-1")) &&( (prodI >=  0) && (prodI <= 10) && (sizeI >= 0) && ( sizeI <= 3)) )
        { 
           /*Check for matching Province the city and the Division*/  
           if(((Qp==Ip && Qc== Ic && Qr == Ir) || (Qp==Ip && Qc== Ic && Qr == 0) || (Qp==Ip && Qc== 0 && Qr == 0) || (Qvec.at(1) == "-1") ) &&( (Ip >= 0) && ( Ip <= 10) && (Ic >= 0) && (Ic <= 20 ) && (Ir >= 0 ) &&(Ir <= 5)) )
           {
              /*Check for matching gender*/
              if(itI->second.at(2) == Qvec.at(2))
              {
                 int range = Qvec.at(3).find("-");
                 int rng_siz = Qvec.at(3).size();
                 /*Condition to Check if the age in Queery is given as a range*/
                 if(range == -1)
                 {
                   int age = atoi(Qvec.at(3).substr(0,2).c_str());
                   if( atoi(itI->second.at(3).c_str()) == age && atoi(itI->second.at(3).c_str()) >=1 && atoi(itI->second.at(3).c_str()) <= 90)
                   {
                     /*Calculating the number of units sold totally for given Querry*/
                     if(atoi(itI->second.at(4).c_str()) <= 100) {result=result+atoi(itI->second.at(4).c_str());}
                   }
                 }
                 if (range != -1)
                 {
                    int age1 = atoi(Qvec.at(3).substr(0,range).c_str());
                    int age2 = atoi(Qvec.at(3).substr(range+1,rng_siz).c_str());
                    /*Checking for correct Age with the given age range*/
                    if( atoi(itI->second.at(3).c_str()) >= age1 && atoi(itI->second.at(3).c_str()) <= age2 && (atoi(itI->second.at(3).c_str())  >=  1) && (atoi(itI->second.at(3).c_str())  <= 90)  )
                    {
                      /*Calculating the number of units sold totally for given Querry*/
                      if(atoi(itI->second.at(4).c_str()) <= 100 ){ result=result+atoi(itI->second.at(4).c_str());}
                    }
                  }
              }
           }
        }
     }
 output.push_back(result);
    }
     Qvec.clear();
  }
 for(vector<int> :: iterator it= output.begin();it != output.end();it++)
 {
 cout << (*it) << endl;
 }

return 0;
}

Your code fails here-

Input
6
I 1.1 2.11.1 F 70 22
Q 1 2.11.5 F 70
I 1 4.1 F 37 73
I 2 4.2 M 63 24
Q -1 -1 M 1-100
Q -1 -1 F 1-100
Your Output
22
24
95
Expected Output
0
24
95

Though I gotta admit, this question is repulsive af.

firstly, thank you very much for the time you took and pointing out the mistake :slight_smile:

I fixed my code and still getting wrong answer :frowning:

The fixed code is follows

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<stdlib.h>

using namespace std;

int main()

{
 /* The number of inputs*/
 int numberOfInputs;

 /* The Map storing values of input type I*/
 map<int,vector<string> > Imap;

 /* The Vector storing values of input type Q*/
 vector<string> Qvec;

 /*Vector stporing the value of Output*/
 vector<int> output;

 cin >> numberOfInputs;


 for (int i=0; i< numberOfInputs; i++)
 {
 vector<string> inputValues;  
 string inputType;
 string productSize;
 string provinceDetail;
 string gender;
 string age;
 string unitsSold;
 cin >> inputType;
 if ( inputType == "I")
 {
    cin >> productSize >> provinceDetail >> gender >> age >> unitsSold;
    inputValues.push_back(productSize);
    inputValues.push_back(provinceDetail);
    inputValues.push_back(gender);
    inputValues.push_back(age);
    inputValues.push_back(unitsSold);
    Imap.insert(pair<int,vector<string> >(i,inputValues) );
 }
 
 else if(inputType == "Q")
 {
    int result = 0;
    string ageRange;
    cin >> productSize >> provinceDetail >> gender >> ageRange;
    Qvec.push_back(productSize);
    Qvec.push_back(provinceDetail);
    Qvec.push_back(gender);
    Qvec.push_back(ageRange);
    /* Obtaining the values of the product and size of the Querry*/
    int prodQ,sizeQ;
    int posQ = Qvec.at(0).find(".");		
    if(posQ == -1) { prodQ = atoi(Qvec.at(0).c_str());  sizeQ =0;}
    if(posQ != -1)
    {
       prodQ =  atoi(Qvec.at(0).substr(0,posQ).c_str());
       int size1 = Qvec.at(0).size();  
       sizeQ = atoi(Qvec.at(0).substr(posQ+1,size1).c_str());
     }
    /* Obtaining the values of the Province,City and Region of the Querry*/
    string QProv,QReg,QCity;
    int Qp,Qc,Qr;
    int posPQ = Qvec.at(1).find(".");
    if( posPQ == -1) {Qp = atoi(Qvec.at(1).c_str()); Qc = 0; Qr=0;}
    if(posPQ != -1)
    {
      Qp =  atoi(Qvec.at(1).substr(0,posPQ).c_str());
      int sizePQ = Qvec.at(1).size(); 
      QCity = Qvec.at(1).substr(posPQ+1,sizePQ);
      Qc = atoi(Qvec.at(1).substr(posPQ+1,sizePQ).c_str()); 
      int posPQ1 = QCity.find(".");
      if(posPQ1 == -1) { Qr = 0;}   
      if(posPQ1 != -1) 
      {
         int sizePI2 = Qvec.at(1).substr(posPQ+1,sizePQ).size(); 
         Qc = atoi(QCity.substr(0,posPQ1).c_str()); 
         Qr = atoi(QCity.substr(posPQ+2,sizePI2).c_str());
       }   
     } 
for(map<int,vector<string> > ::iterator itI = Imap.begin();itI != Imap.end();itI++)
{
    /* Obtaining the values of the product and size of the Inputs*/
        int prodI,sizeI;
        int posI = itI->second.at(0).find(".");
      /* If there is no decimal separation*/
        if(posI == -1) { prodI = atoi(itI->second.at(0).c_str());  sizeI =0;}   
        /*If there is subdivision, then the string values are converetd to integer*/
        if(posI != -1)
        {
          prodI =  atoi(itI->second.at(0).substr(0,posI).c_str());
          int size1 = itI->second.at(0).size();  
          sizeI = atoi(itI->second.at(0).substr(posI+1,size1).c_str());
        }
       /* Obtaining the values of the Province,City and Region of the Inputs*/
    string IProv,IReg,ICity;
        int Ip,Ic,Ir;
        int posPI = itI->second.at(1).find(".");
        if( posPI == -1) {Ip = atoi(itI->second.at(1).c_str()); Ic = 0; Ir=0;}
    if(posPI != -1)
        {
          Ip =  atoi(itI->second.at(1).substr(0,posPI).c_str());
          int sizePI = itI->second.at(1).size(); 
          ICity = itI->second.at(1).substr(posPI+1,sizePI);
          Ic = atoi(itI->second.at(1).substr(posPI+1,sizePI).c_str()); 
          int posPI1 = ICity.find(".");
          if(posPI1 == -1) { Ir = 0;}   
          if(posPI1 != -1) 
          {
             int sizePI2 = itI->second.at(1).substr(posPI+1,sizePI).size(); 
             Ic = atoi(ICity.substr(0,posPI1).c_str()); 
             Ir = atoi(ICity.substr(posPI+2,sizePI2).c_str());
           }   
        }	
    if( ((prodI == prodQ && sizeI == sizeQ) || ( prodI == prodQ && sizeQ == 0) ||(Qvec.at(0) == "-1")) &&( (prodI >=  0) && (prodI <= 10) && (sizeI >= 0) && ( sizeI <= 3)) )
        { 
           /*Check for matching Province the city and the Division*/  
           if(((Qp==Ip && Qc== Ic && Qr == Ir) || (Qp==Ip && Qc== Ic && Qr == 0) || (Qp==Ip && Qc== 0 && Qr == 0) || (Qvec.at(1) == "-1") ) &&( (Ip >= 0) && ( Ip <= 10) && (Ic >= 0) && (Ic <= 20 ) && (Ir >= 0 ) &&(Ir <= 5)) )
           {
              /*Check for matching gender*/
              if(itI->second.at(2) == Qvec.at(2))
              {
                 int range = Qvec.at(3).find("-");
                 int rng_siz = Qvec.at(3).size();
                 /*Condition to Check if the age in Queery is given as a range*/
                 if(range == -1)
                 {
                   int age = atoi(Qvec.at(3).substr(0,2).c_str());
                   if( atoi(itI->second.at(3).c_str()) == age && atoi(itI->second.at(3).c_str()) >=1 && atoi(itI->second.at(3).c_str()) <= 90)
                   {
                     /*Calculating the number of units sold totally for given Querry*/
                     if(atoi(itI->second.at(4).c_str()) <= 100) {result=result+atoi(itI->second.at(4).c_str());}
                   }
                 }
                 if (range != -1)
                 {
                    int age1 = atoi(Qvec.at(3).substr(0,range).c_str());
                    int age2 = atoi(Qvec.at(3).substr(range+1,rng_siz).c_str());
                    /*Checking for correct Age with the given age range*/
                    if( atoi(itI->second.at(3).c_str()) >= age1 && atoi(itI->second.at(3).c_str()) <= age2 && (atoi(itI->second.at(3).c_str())  >=  1) && (atoi(itI->second.at(3).c_str())  <= 90)  )
                    {
                      /*Calculating the number of units sold totally for given Querry*/
                      if(atoi(itI->second.at(4).c_str()) <= 100 ){ result=result+atoi(itI->second.at(4).c_str());}
                    }
                  }
              }
           }
        }
     }
 output.push_back(result);
    }
     Qvec.clear();
  }
 for(vector<int> :: iterator it= output.begin();it != output.end();it++)
 {
 cout << (*it) << endl;
 }

return 0;
}

Thanks again

Another TC-

Input
6
I 9.2 2.11.2 F 70 22
I 2 4.1 F 37 73
Q 9.2 2.11.0 F 70
I 2 4.2 M 63 24
Q 1 -1 F 30-70
Q 2 4.1 F 37
Your Output
22
0
73
Expected Output
0
0
73

Also, please refrain from using “award point” feature. It subtracts your karma! I am returning those points back to you. In case you want to give karma to a user for his help, use “Accept Answer” option by clicking on “tick in circle” beside the answer. It also gives you some karma as well.