i came across the problem Spell Bob. I made the following solution to the problem. Note that the code is well commented explaining logic in each step. Can anybody find slightest of any error which is causing the online judge to mark it ‘wrong answer’
#include<bits/stdc++.h>
using namespace std;
void ispossible(vector<int> &, vector<int> &);
int main()
{
int t;
cin>>t;
cin.get();
for(int i=0;i<t;i++)
{
string s1,s2;
int input;
getline(cin,s1);
getline(cin,s2);
vector<int> bp;
vector<int> op;
for(int i=0;i<3;i++)
{
if(s1.at(i)=='b')
{
input=10; //just making the first digit 1 so that it is identified to which side of the card it belong. but it is not used anywhere
input+=i;
bp.push_back(input);
}
if(s1.at(i)=='o')
{
input=10;
input+=i;
op.push_back(input);
}
}
for(int i=0;i<3;i++)
{
if(s2.at(i)=='b')
{
input=20; //marking b's and o's obtained from other side as 2 and then its place. ex - 20 means second side and zeroth card
input+=i;
bp.push_back(input);
}
if(s2.at(i)=='o')
{
input=20;
input+=i;
op.push_back(input);
}
}
ispossible(bp,op);
}
}
void ispossible(vector<int> & bp, vector<int> & op)
{
int b1,b2,o,cnt;
if(bp.size()<2 || op.size()<1) //if there are b's and o's less than required to just spell 'bob' print no
{
cout<<"no"<<endl;
}
else
{
if(bp.size()==2)
{
if(bp.at(0)%10==bp.at(1)%10) //if two b are at same position then no
{
cout<<"no"<<endl;
}
else
{
b1=bp.at(0)%10;
b2=bp.at(1)%10;
for(unsigned int i=0;i<op.size();i++) //find a single instance of o which is not at the same position of the two b's
{
o=op.at(i)%10;
if(o!=b1 && o!=b2)
{
cout<<"yes"<<endl;
break;
}
}
if(o==b1 || o==b2) //if no o is found at place other than the two b's then this statement will be surely executed
{
cout<<"no"<<endl;
}
}
}
if(bp.size()==3) //if there are three instances of b
{
if(op.size()==1) //if there is only one o
{
o=op.at(0)%10; //note its position
for(unsigned int i=0;i<bp.size();i++)
{
if(bp.at(i)%10!=o)
{
cnt++; //count the instances of b which are not at the location of o
}
}
if(cnt==3) //if there were three b's not at position of o then surely bob can be made
{
cout<<"yes"<<endl;
}
else //when there are two b's not the position of o
{
b1=b2=-1;
for(unsigned int i=0;i<bp.size();i++) //then these two b's must not be at the same position
{
if(bp.at(i)%10!=o) //we know that one b is already at the position of o, thus we are left with two b's only
{
if(b1==-1) //assign each b its unique position
{
b1=bp.at(i)%10;
}
else
{
b2=bp.at(i)%10; //as there are two b's left once the if statement will be executed and then the else
}
}
}
if(b1!=b2) //if the two b's are at different location then print yes
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}
}
}
if(op.size()==2 || op.size()==3) //when there are two or three o's(when number of b's is atleast three) then surely bob can be made
{
cout<<"yes"<<endl;
}
}
if(bp.size()==4) //if there are 4 b's then at least one o is present therefore bob can be spelt
{
cout<<"yes"<<endl;
}
if(bp.size()==5) //when there are 5 b's at least one o is present according to initial condition hence bob can be spelt
{
cout<<"yes"<<endl; //when all 6 are b's then the initial condition that number of o's is not less than 1 is not satisfied, hence no
}
}
}