-2

I am solving a question on Codechef BUY1GET1.Here is the question.

One day Alice visited Byteland to purchase jewels for her upcoming wedding anniversary. In Byteland, every Jewelry shop has their own discount methods to attract the customers. One discount method called Buy1-Get1 caught Alice's attention. That is, Alice buys one jewel, then she can get one additional jewel with the same color without charge by Buy1-Get1. Alice lists the needed jewels as a string S, each letter denotes one jewel, and the same letters denote the same colors of jewels, and the different letters denote the different colors of jewels. The cost of each jewel is 1. Your task is to calculate the minimum cost for getting all the jewels Alice listed. Input

The first line of input contains a single line T, which represents the number of test cases. Then T lines will follow, and each contains a string S, which represents the jewels Alice needed. Output

Output the minimum cost for each test case. Constraints

1 ≤ T ≤ 100 1 ≤ |S| ≤ 200, where |S| represents the length of the string S. The string S is case sensitive, and will contain only English characters in the range [a-z], [A-Z]. Sample

Input:
4
ssss
ssas
sa
s

Output:
2
3
2
1

I am always getting wrong answer on Codechef but on my PC it give the same output as the sample cases. I can't figure out why? Here is my code.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
    int t;
    scanf("%d\n",&t);
    while(t--)
    {
        int i,j,n,m=0,k=0;
        char a[201];
        scanf("%s",a);
        n=strlen(a);
        int count=0;
        for(i=0;i<n;i++)
        {
            count=0;
            if(isalpha(a[i]))
            {
                count++;
                for(j=i+1;j<n;j++)
                {
                    if(a[j]==a[i])
                    {
                        count++;
                        a[j]=++k;
                    }
                }
            }
            //printf("%dc\n",count );
            if(count%2==0)
            {
                m+=(count/2);
                //printf("%dm1\n",m);
            }
            else if(count%2!=0)
                m+=(1+count/2);
        }
        printf("%d\n",m );
    }
    return 0;
}
Abhishek
  • 49
  • 6

3 Answers3

0

solution:

#include<stdio.h>

int main()
{
int t,h[256],i,cost;
scanf("%d",&t);
char s[201];
while(t--)
{
    for(i=0;i<256;i++)
        h[i]=0;
    scanf("%s",s);
    i=0;
    while(s[i])
    {
        h[s[i]]++;
        i++;
    }
    cost = 0;
    for(i='A';i<='z';i++)
        if(h[i]&1)
            cost += h[i]/2 + 1;
        else                
            cost += h[i]/2;
    printf("%d\n",cost);
 }
 return 0;
}   
  • No the answer will be 4. For input "scscsc" there are 3 's' and 3 'c'. Two 's' and two 'c' cost 2 money and left 1 's' and 1'c' cost 2 . hence total will be 4. – Abhishek Mar 04 '16 at 06:42
0
int sum = 0;
//search number of occurences for each alphabet in the string
for (i=0; i<26; i++) {
   sum += (numchar(str, 'a' + i) +1) / 2; //round up
}

You could write your own numchar function that finds how many characters c exist in a string s:

int numchar(char[] s, char c) {
   int i = 0, n = 0;
   while ( s[i] != '\0' ) {
      if (s[i] == c) n++;
      i++;
   }
   return n;
}
Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
0

OK

just replace the following line:

a[j]=++k;

with

a[j]= '\0';

and submit again.

Explanation

for certain cases the value of k comes to the range 65-90 and 97-122 then k is treated as a character as it obtains the ascii value of a character.

so assign a null value to the array a[] every time and i think it will work.

cjahangir
  • 1,773
  • 18
  • 27