ACBALL - Editorial

#include
#include<string.h>>
using namespace std;

int main()
{
int t;
cin>>t;
while(t–)
{
char x[100005],y[100005];
char str[100005];
cin>>x>>y;

   for(int i=0;i<strlen(x);i++)
   {
       if(x[i]==y[i])
       {
           if(x[i]=='W')
           str[i]='B';
           else
           str[i]='W';
       }
       
       else
       str[i]='B';
       
   }
   cout<<str<<endl;;

}
return 0;
}

why is this ans wrong?

What’s wrong with this code?
Why it shows wrong answer on submission?

#include<stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t–)
{
int i;
char x[1000000],y[1000000];
scanf("%s",&x);
scanf("%s",&y);
for(i=0;x[i]!=’\0’&&y[i]!=’\0’;i++)
{
if(x[i]==‘B’&&y[i]==‘B’)
printf(“W”);
else printf(“B”);
}
}
return 0;
}

What’s wrong with the code?

#include<stdio.h>

int main()
{
int t;
scanf("%d",&t);
while(t–)
{
int i;
char x[1000000],y[1000000];
scanf("%s",&x);
scanf("%s",&y);
for(i=0;x[i]!=’\0’&&y[i]!=’\0’;i++)
{
if(x[i]==‘B’&&y[i]==‘B’)
printf(“W”);
else printf(“B”);
}
}
return 0;
}

@akjais-

Make sure you print the output of each test case in a new line. What you’re lacking, is a printf ("\n") character at the end of your loop (to make the result of next test case in new line).

Eg-

If the answer for some X and Y for T=2 is-

BBWB
WWBB

Then your code is printing BBWBWWBB

Rectify that and I think you will go fine! :slight_smile:

Here I think that the output of the give testcase can be different also.
as mention in the testcase

Input:
1
WBWB
WBBB

Output:
BWBW

as mentioned here that the maximum hamming distance is 7 but is we are taking a string ‘BWWW’ it also have the hamming distance is 7 so why it can not be the correct output??
New here please help.

Your approach is correct. You can directly print each character without adding it with K.

I am not an expert in java. But I can say that you have created Z[] correctly. The answer is Z[] itself. I have no idea what you have done after the forloop to create Z[] as I am not familiar with the syntax of java.

I converted the array to a string and printed it.

Is it really required? I mean, I don’t know. I think we can simply print each character. The result will be same if we print a complete string or individual characters one by one without any space.

Yes, that’s true, however it still does not explain why the result is WA. Even if i print the whole string the answer should be correct.

link of the codes?

CodeChef: Practical coding for everyone TLE
CodeChef: Practical coding for everyone AC

Ok, I got it :P. You have to print the output for each test case in a new line. Or just give a space after each output. Bad luck bro…

I just tried with your code in the practice section by adding a space…

https://www.codechef.com/viewsolution/10231723

Using strlen() repeatedly wastes sometime…

In Java concatenating String takes more time as each time it creates a new instance of String. You can use StringBuilder in Java for concatenation the string or print each character.

There is a gap of 0.00s and 1.01s

First of all, why are you taking so small string? The maximum length will be 10^5, so you should take at least 1 greater than the maximum possible length.

The second point is, your code will not work. In the case when a[i]!=b[i] the output should be B, but in your code, it will give W.

It is actually not 1.01s…it is >=1.01s…that means, the system has stopped the code after 1.01s because the code is taking more than 1s to give the output.

the string has to be lexicographcally smallest
hence the output!