11577 - Letter Frequency

All about problems in Volume 115. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11577 - Letter Frequency help me :'(

Post by brianfry713 » Wed May 07, 2014 9:32 pm

Try changing cnt to an int.
Try input:

Code: Select all

1
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Check input and AC output for thousands of problems on uDebug!

tanvir cse
New poster
Posts: 2
Joined: Wed Oct 22, 2014 11:20 am

11577

Post by tanvir cse » Sun Jun 14, 2015 11:00 pm

all sample input is right but why wrong ans??????

Code: Select all

int main()
{
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    char a[205];
    vector<char>vc;
    int b[27];
    int t;
    scanf("%d\n",&t);
   // getchar();
    while(t--)
    {
        gets(a);
        vc.clear();
        memset(b,0,sizeof(b));
        for(int i=0;a[i];i++)
        {
            if((a[i]>='A'&&a[i]<='Z')||(a[i]>='a'&&a[i]<'z')){
           char ch= tolower(a[i]);
                    b[ch-97]++;
            }

        }
        int m=-1;
        for(int i=0;i<26;i++)
        {
            if(b[i]>m)
            {
                vc.clear();
                vc.push_back(i+97);
                m=b[i];
            }
            else if(b[i]==m)
                vc.push_back(i+97);
        }
        for(int i=0;i<vc.size();i++)
            cout<<vc[i];
        cout<<endl;
    }

    return 0;
}
Last edited by brianfry713 on Fri Jun 19, 2015 6:19 am, edited 1 time in total.
Reason: Added code blocks

yasir.nabil
New poster
Posts: 2
Joined: Thu Jun 16, 2016 11:31 pm

Re: 11577 - Letter Frequency

Post by yasir.nabil » Thu Jun 16, 2016 11:38 pm

Code: Select all

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

int main()
{
    int T,p,i,j,max,n,ar[26];
    char ch[210];
    char al[]="abcdefghijklmnopqrstuvwxyz";
    scanf("%d",&T);
    while(T--)
    {
        gets(ch);
        if(ch[0]=='\0')
            gets(ch);
        for(i=0;i<26;i++)
        {
            ar[i]=0;
        }
        al[26]='\0';
        n=strlen(ch);
        for(i=0;i<n;i++)
        {
            if(ch[i]>='A'&&ch[i]<='Z')
                ch[i]=ch[i]-'A'+'a';
            p=ch[i]-'a';
            ar[p]++;
        }
        max=-1;
        for(i=0;i<26;i++)
        {
            if(ar[i]>max)
                max=ar[i];
        }
        for(i=0;i<26;i++)
            if(ar[i]==max)
                printf("%c",al[i]);
        printf("\n");
    }
    return 0;
}
Why I am getting Wrong Answer when my code passes all input and output.......if not, would you please give me some critical case?

saadmanheavy
New poster
Posts: 2
Joined: Fri Dec 11, 2015 9:43 pm

Re: 11577 - Letter Frequency

Post by saadmanheavy » Thu Nov 17, 2016 2:54 pm

Can anyone point out the mistake ? Thanks !

Code: Select all

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

int main()
{
    int n,len,j,ar[26],i,k,index=0,temp,max;
    char c[202],alp[26],a;
    while(scanf("%d\n",&n)==1)
    {
        for(i=0; i<n; i++)
        {
            gets(c);
            len = strlen(c);
            while( c[index] )
            {
                c[index]=(tolower(c[index]));
                index++;
            }
            for(j=0,a='a'; j<26; j++,a++)
            {
                ar[j]=0;
                alp[j]=a;
            }
            for(j=0; j<len; j++)
            {
                if(c[j]>= 'a' && c[j] <= 'z')
                {
                    for(k=0; k<26; k++)
                    {
                        if(c[j]==alp[k])
                        {
                            ar[k]+=1;
                        }
                    }
                }
            }
            for(j=0; j<25; j++){
                for(k=j+1;k<26;k++){
                    if(ar[j]<ar[k]){
                        temp=ar[j];
                        ar[j]=ar[k];
                        ar[k]=temp;

                        a=alp[j];
                        alp[j]=alp[k];
                        alp[k]=a;
                    }
                }
            }
            max=ar[0];
            for(j=0;j<26;j++){
                if(max!=ar[j]){
                    alp[j]='\0';
                }
            }
            printf("%s\n",alp);

        }
        return 0;
    }
}

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11577 - Letter Frequency

Post by lighted » Fri Mar 10, 2017 3:19 pm

Check input

Code: Select all

2
Computers account for only 5% of the country's commercial electricity consumption.
Computers account for only 5% of the country's commercial electricity consumption.
Output

Code: Select all

co
co
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Post Reply

Return to “Volume 115 (11500-11599)”