The Republic Day speech - Problem

Why I Am Getting WA . IN my View there are problem with my Code At all . Please Help .
Link : http://ideone.com/i8fF50
Problem Link : http://www.codechef.com/PSYC2015/problems/REPUB/

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{
    char a[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    char str[100050], x, y;
    int t;
    int i, count, len, max, j;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(str);
        len=strlen(str);
        for(i=0;i<len;i++)
            str[i]=tolower(str[i]);
        max=0;
        for(i=0;i<26;i++)
        {
            count=0;
            for(j=0;j<len;j++)
            {
                if(str[j]==a[i])
                    count++;
            }
            if(count>=max)
            {
                    max=count;
                    x=a[i];
            }

        }
        printf("%c\n",x);
    }
    return 0;
}

Hey AK795 I have Ran Your Inputs with My Code . It generates same output as I have run an AC code of the contest . I have edited My code . I submitted it during the contest but It got WA all the time .

**Can Anyone Please Help ?? Is there Anyone Who Can Help Me ??
**

I just ran your code for certain test data, its giving me wrong outputs, i think probably you are missing the cases when special symbols are attached to the strings .

Please also post a link to the original problem. That would save time for anyone trying to help! :slight_smile:

Your solution doesn’t produce correct results for many test cases. Eg-

1

hhhhaaaa aaa

Your output is ‘h’ while it should be ‘a’.

Also, your algorithm is highly inefficient, you are traversing the string for each possible character in [a-z], which isn’t required. Just keep a count of the characters present in the string and find the character with maximum number of occurrences.

http://ideone.com/BGxX1J - look at this gives WA for certain inputs

Can I Have SOme Test Cases Regarding Your Comment ?? That will Help for me in identifying problems .

Hey See This One . I edited It and Last Moment I Have Submitted This . But It Also Getting WA .


#include<stdio.h>
#include<string.h>
#include<ctype.h>

int main()
{
    char a[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    char str[100050], x, y, t;
    
    int i, count, len, max, j;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        gets(str);
        len=strlen(str);
        for(i=0;i<len;i++)
            str[i]=tolower(str[i]);
        max=0;
        for(i=0;i<26;i++)
        {
            count=0;
            for(j=0;j<len;j++)
            {
                if(str[j]==a[i])
                    count++;
            }
            if(count>=max)
            {
                    max=count;
                    x=a[i];
            }

        }
        printf("%c\n",x);
    }
    return 0;
}

The problem with your solution is that it doesn’t read input properly. Try printing the input and you’ll see the error, See here.
The problem is with gets();

Problem Statement

try creating a hashmap of the alphabets

int a[26];
memset(a,0,sizeof(a));
if s[i]>='A' and s[i]<='Z':
   	a[s[i]-65] += 1
elif s[i]>='a' and s[i]<='z':
   	a[s[i]-97] += 1

now traverse the array and check if two alphabets have same count then compare their ASCII values and print the output as required in the problem statement.

ans = a[25]
pos = 25
for i in range(24,-1,-1):
	if a[i]>ans:
		ans = a[i]
		pos = i
			
print 'a' + (char)pos

hope it helps :slight_smile:

You can use something like this also

  String x....;


   for(int j=65;j<=90;j++)


         {


                  int c=0;


                for(int i=0;i<=l-1;i++)

{

                               char ch=x.charAt(i);


                                if(ch==j||ch==(j+32))


                                    {

c++;

                                      }


                       }


               }.......

With the help of above function you can calculate the frequency of only alphabets…and you know what to do after that :wink:

Hey What Will Be the Output For those Inputs ?? can You Please Provide those too ??

One of the reasons, why your code is wrong, is because, your variable “t” should have been an integer, and not a char.

Since, you have declared it as a char, the variable is only allocated only 1 byte. But, when you are reading an integer value into it, 4-bytes of data are written. Now, that extra 3-bytes of data is overwriting data in some other memory location. (My test runs show that they are overwriting first three locations of your array ‘a’. Making your array a as {'\0', '\0', '\0', 'd', 'e', ...}). So, you basically end up never counting the a’s, the b’s and the c’s in your strings. (You can verify this by printing the contents of array a, before and after reading the value of t, and check whether they are same or not.)

For example, your code gives answer as d for the input string aaaad (during my test run)

That is one problem. The logic seems correct.

Why I can’t Submit My code Right Now ?? I have to solve It .

Unfortunately, the contest has finished. :frowning:

You will need to wait until those problems are moved to the practice section (look under the section ‘Peer’) It might take some days (or even weeks) for problems from external contests hosted on codechef, to be available in the practice section.

Thanks for the Information . I didn’t know it.