BUY1GET1 - Editorial

#include<stdio.h>
#include<string.h>
int main()
{
int t,j,i,count;
char a[201];
scanf("%d",&t);
while(t–)
{
count = 0;
scanf("%s",a);
for(i=0;i<strlen(a)-1;i++)
{
if(a[i]!=’’)
{
for(j=i+1;j<strlen(a);j++)
{
if(a[i]==a[j])
{
a[i]=’
’;
a[j]=’*’;
count++;
}
}
}
}
printf("%d\n",strlen(a)-count);
}
return 0;
}

what is wrong with this solutions works fine on my pc but gives wrong answer here. please help.

Why i’m getting wrong answer?
My code is
#include<stdio.h>
int check(int [],char);
int main()
{
int T,i,A[205]={0},cnt=0,x;
char ch[205];
scanf("%d",&T);
while(T–)
{ A[0]=-1;
cnt=0;
scanf("%s",ch);
for(i=0;ch[i]!=’\0’;i++)
{ x=check(A,ch[i]);
if(x)
{
cnt++;
}

        }
        for(i=0;A[i]!=-1;i++)
        {
            if(A[i]!=0)
            cnt++;
        }
        printf("%d",cnt);
}

return 0;
}

int check(int A[],char c)
{
int x,i;
for(i=0;A[i]!=-1;i++)
{
if(A[i]==c)
{
A[i]=0;
return 1;
}
}
A[i]=c;
A[i+1]=-1;
return 0;

}

I have tried my best to debug this code but can’t find the test cases am failing in , please correct my mistakes , it’s not very lengthy :

http://www.codechef.com/viewsolution/7301396

Can you tell me why this soln is wrong ?
It’s working perfectly fine in my m/c for all sorts of i/p
https://www.codechef.com/viewsolution/8339530

can u please tell my mistake in this code…
https://www.codechef.com/viewsolution/8534920

use someother value instead of “0” when u are marking, your loop is ending when its reaching any index where a[i]==0(0==NULL). -1 will work.

I have implemented the following solution. CodeChef: Practical coding for everyone

I am not sure in which test case it is failing… ? It worked for all the inputs provided in the Question. Can someone please point out the error in the logic.

Why is my answer coming wrong?CodeChef: Practical coding for everyone
Please help

my solution

Can someone please tell me what’s wrong with my solution.

The only mistake is the small size of the array s.
Instead of 200 there should be 201. Simply fixing this gets AC:
http://www.codechef.com/viewsolution/1835761
(I should also add string.h library to get this compiled at C++)

If ‘A’ … ‘Z’ is 0 to 25, then it makes sense to have ‘a’ as 26 etc. 97-71 = 26, so it seems fine.

1 Like

ohkay…got it…removing the “enter the number of test cases” is helping…thanx!!
and yeah… storing it in an array was not required.

@vineetpaliwal : I made sure that every letter gets a unique index.(uncomment cout on line 42)If possible may I know the test cases where my code fails.

The reason is that input files were generated under Windows and has character '\r' before each newline.
It is explained in the FAQ:

Refer to my comment to this answer.

Also just a suggestion - you can output the answer of each test case before input next test case. It should simplify your code :wink:

The reason is that input files contains hidden '\r' characters in the end of each line. Refer to this:

yeah I was getting WA, then simply added

abc = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"


if c in abc:

And got AC :slight_smile:

http://www.codechef.com/viewsolution/4144022

Please me the problem in the solution.
Thanks in advance!

@soumya_13106, you are supposed to count the number of times each character occurs in the string. Now in your solution you are not counting the frequency of the character, you are just storing if they occur or not. Suppose ‘A’ occurs 5 times the ans will be incremented by 5/2+5%2; do this for all the characters. Store their frequency in the array val[](instead of storing 1), and then increment the answer for all the non- zero values of val[].
try it first and if still there is a problem, I have corrected your solution: CodeChef: Practical coding for everyone

#include<stdio.h>

int main()
{
int t,i,count;
char ch;
scanf("%d",&t);
scanf("%c",&ch);
while(t–)
{
int a[125]={0};

	scanf("%c",&ch);
 	while(ch!='\n')
 	 {
 	 	a[ch]++;
 	 	scanf("%c",&ch);
 	 }
 	
 	count=0;
 	for(i=65;i<=90;i++)
 	 {	    
 	    count+=((a[i]+1)/2);
 	 }
 	 
    for(i=97;i<=122;i++)
 	 { 
 	    count+=((a[i]+1)/2);
 	 }

     printf("%d\n",count);
 }
return 0; 

}

please , tell me the problem with the code.