< Back to forum

my code is giving right output but in codechef it is giving wrong ans to the problem LINK TO QUESTN (CHEFGP)... https://www.codechef.com/problems/CHEFGP

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

int main()
{
int t;
cin>>t;
while(t--)
{
int i,z,a1,b1,len;
string s;
cin>>s;
int x,y;
cin>>x>>y;
int m=0,n=0;
len=s.length();
for(i=0;i<len;i++)
{
 if(s[i]=='a')
        m++;
 if(s[i]=='b')
        n++;
}

z=m+n;
a1=0,b1=0;

for(i=0;i<z;i++)
{
if(m>0||n>0)
{

 if((a1<x)&&(m!=0))
  {
      a1++;
      cout<<"a";
      b1=0;
      m--;
  }
 else if((b1<y)&&(n!=0))
  {
      b1++;
      cout<<"b";
      a1=0;
      n--;
  }
  else if(a1>=x&&n==0)
   {

       cout<<"*a";
       a1=1;
       b1=0;
       m--;
   }
   else if(b1>=y&&m==0)
   {
       cout<<"*b";
       a1=0;
       b1=1;
   }
}

}

cout<<endl;

}


    return 0;
}

 

Asked by: Manish_Kumar_Savita on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




0 Answer(s)

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.