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

ahmed
New poster
Posts: 9
Joined: Tue Mar 17, 2009 6:36 pm

11577 - Letter Frequency

Post by ahmed »

I am getting WA in this problem.As far as I have checked my outputs are ok.Can anyone give me any suggestion.My code is given below:-

Code: Select all

Accepted.



Thanks a lot :D
Last edited by ahmed on Sun Mar 29, 2009 10:50 am, edited 1 time in total.
Igor9669
Learning poster
Posts: 85
Joined: Sun Jun 08, 2008 12:58 pm

Re: 11577-Letter Frequency

Post by Igor9669 »

change scanf("%d",&count); to scanf("%d\n",&count); get Acc and delete your code :)
tasnif
New poster
Posts: 3
Joined: Fri Apr 03, 2009 5:53 am

Re: 11577 - Letter Frequency

Post by tasnif »

i am repeatedly getting wrong answer.but cant find any mistake.here is my code.

#include<stdio.h>

int array[26];
int main(void)
{
int testcase,testcount=1,i;
char ch,dummy,largest;
scanf("%d\n",&testcase);
dummy=getchar();
while(testcount<=testcase)
{


scanf("%c",&ch);
if(ch=='\n')
{
largest=array[0];
for(i=1;i<=25;i++)
{
if(array>largest)
{
largest=array;

}
}
for(i=0;i<=25;i++)
{
if(array==largest)
{
printf("%c",(i+97));
}
}

testcount++;
printf("\n");
for(i=0;i<=25;i++)
{
array=0;
}
}





else
{
if(ch>='a'&&ch<='z')
{
array[ch-'a']++;
}
else if(ch>='A'&&ch<='Z')
{
array[ch-'A']++;
}



}


}
return 0;
}
helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

Re: 11577 - Letter Frequency

Post by helloneo »

Try this case

1
a
fitzpatr
New poster
Posts: 1
Joined: Sat Dec 05, 2009 3:47 am

Re: 11577 - Letter Frequency

Post by fitzpatr »

Hello. I've tried to solve this problem, but I continue to get a Wrong Answer. Any assistance is appreciated, since I can't even find a case where I get a wrong answer.

Thank you,
fitzpatr

Code: Select all

#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
    char input[201];
    char alphabet[26]="abcdefghijklmnopqrstuvwxyz";
    char *p, *q;
    int x, y, z, length, highest, localCount, cases;
    int num[26];
    scanf("%d",&cases);
    fflush(stdin);
    for(z=0; z < cases; z++){
    highest = 0;
    gets(input);
    length = strlen(input);
    p = alphabet;
    q = input;
    for(x = 0; x < 26; x++)
    {
          localCount=0;
          num[x] = 0;
          q = input;
          for(y = 0; y < length; y++)
          {
                if(*p == tolower(*q))
                {
                                            num[x]++;
                                            localCount++;
                }
                q++;
          }
          p++;
          if(localCount>highest)
                                highest = localCount;
    }
    if(highest !=0){
    for(x = 0; x < 26; x++)
    {
          if(num[x] == highest)
          {
                    printf("%c",alphabet[x]);
          }
    }
}
    printf("\n");
}
return 0;
}
    
naseef_07cuet
Learning poster
Posts: 62
Joined: Sat Nov 21, 2009 10:17 pm
Location: CUET,Chittagong,Bangladesh

Re: 11577 - Letter Frequency

Post by naseef_07cuet »

try this:-
input:
5
safdsad
afafafafsdaf
dsgsdagsdag
ssfde
nnaaaann

Output will be:
ads
af
dgs
s
an
If you have determination, you can do anything you want....:)
c0nan
New poster
Posts: 1
Joined: Thu Feb 10, 2011 2:23 am

Re: 11577 - Letter Frequency

Post by c0nan »

i really disappointed
every try in this problem , got WA
i tried all test cases
like
1
a
and mr naseef_07cuet cases
and all correct
but finally got WA
any one could
any one could help ?!!!
my code >>

Code: Select all

#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
void INSERTION_SORT(char[],int) ;
using namespace std ;
int main ()
{
	int Cnt = 0  , freq = 0,oo =-1000,Cases,index=0,LettCnt=0;
	char c;
	char Arr[200],Lett[100];
	char Comp ;
    int Count [200];
	cin>>Cases;
	for (int K=0;K<Cases+1;K++)
	{
			for (int i=0; i<200; i++)
			{
				scanf("%c", &c);
				Arr[i] = c;
				Cnt++;
				if (c == '\n')
					break;
			}
			for (int i=Cnt;i<200;i++)
			{
				Arr[i] = '\0';
			}
			for (int i=0;i<Cnt;i++)
			{
				if (isdigit(Arr[i])||Arr[i]==' '||Arr[i]=='\n')
				{
					Count[i]=oo;
				}
				else
				{
					Count[i] = 1;
				}
			}
		   for (int i=Cnt;i<200;i++)
			{
				Count[i] = '\0';
			}
			for (int i=0;i<Cnt;i++)
			{
				Comp = Arr[i];
				for (int j=i+1;j!=Cnt+1;j++)
				{
					if (Arr[j]==Comp)
					{
						Count[i]++;
					}
				}
			}
		for (int i=0;i<Cnt;i++)
		{
			if (Count[i]>freq)
			{
				freq = Count[i];
			}
		}
		for (int i=0;i<Cnt;i++)
		{
			if (Count[i]==freq)
			{
		    Lett[LettCnt++]=Arr[i];
			index++;
			}
		}
		if(K!=0)
		{
			INSERTION_SORT(Lett,index);
	//		if(K!=Cases)
	//		{
		      cout<<endl;
	//		}
		}
		//========================= Re Initialize Arrays & Data =================
		for (int i=0;i<Cnt;i++)
		{
			Arr[i] = '\0';
			Count[i]= '\0';
		}
		for (int i=0;i<index;i++)
		{
			Lett[i] = '\0';
		}
		freq = 0;
		Cnt = 0;
		oo = -1000;
		LettCnt=0;
		index=0;
	}
return 0 ;
}
void INSERTION_SORT(char Array[],int ArrayLength) 
{ 
  for(int j=1;j<ArrayLength;j++) 
  { 
    int key=Array[j]; 
    int i=j-1; 
    while(i>=0&&Array[i]>key) 
	{
      Array[i+1]=Array[i--]; 
	}
    Array[++i]=key; 
  }
  	for (int i=0;i<ArrayLength;i++)
	{
		cout<<Array[i];
	}
} 
sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

11577 - Letter Frequency

Post by sith »

Hi,

I got TL.

I am sure that complexity of my solution is n+nlogn. Is it enough? or there is exits faster algorithm
Or this is Java I/O problems?


Please give me advice.



Code: Select all

AC
Last edited by sith on Sun Dec 09, 2012 5:27 am, edited 1 time in total.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 11577 - Letter Frequency TLE

Post by brianfry713 »

you can solve it in JAVA in less than .3 sec. Just keep an array of size 26 with all the frequencies.
Check input and AC output for thousands of problems on uDebug!
sith
Learning poster
Posts: 72
Joined: Sat May 19, 2012 7:46 pm

Re: 11577 - Letter Frequency TLE

Post by sith »

Thank you.
Got AC
xplosive
New poster
Posts: 4
Joined: Sat Aug 03, 2013 2:49 pm

Re: 11577 - Letter Frequency

Post by xplosive »

what's wrong... i get many wa.

Code: Select all

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
     freopen("uvainput.txt", "r", stdin);
    //freopen("uvaoutput.txt", "w", stdout);

    char s[10000],rs[30],tch;
    int t,tt,i,j,l,max,min,c;

    const int r=123;
    scanf("%d",&tt);
    char gr=getchar();

    for(t=1; t<=tt; t++)
    {
        max=0,c=0;
        int a[r]={0};
        gets(s);
        l=strlen(s);

        for(i=0;i<l;i++)
        {
            if(isalpha(s[i]))
            {
                if(isupper(s[i]))
                {
                    j=s[i]+32;
                }
                else
                    j=s[i];
                a[j]++;
            }
        }
        //65 to 122

        for(i=65;i<r;i++)
        {
            if(a[i]>max)
                max=a[i];
        }

        for(i=65;i<r;i++)
        {
            if(a[i]==max)
            {
                rs[c]=i;
                c++;
            }
        }

        for(i=0; i<c-1; i++)
        {
            min=rs[i];
            for(j=i+1; j<c; j++)
            {
                if(rs[j]<min)
                {
                    min= rs[j];
                    tch=rs[i];
                    rs[i]=rs[j];
                    rs[j]=tch;
                }
            }
        }

        for(i=0; i<c; i++)
        {
            printf("%c",rs[i]);
        }


    }

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

Re: 11577 - Letter Frequency

Post by brianfry713 »

Don't read from a file.
Check input and AC output for thousands of problems on uDebug!
okokkook
New poster
Posts: 4
Joined: Wed Apr 30, 2014 1:55 pm

11577 - Letter Frequency

Post by okokkook »

Code: Select all

#include <stdio.h>
#include <ctype.h>
int main(){
    int n;
    scanf("%d",&n);
    getchar();
    while(n--){
        char s[1000],cnt[26]={0};
        gets(s);
        int i;
        for(i=0;s[i];i++){
            if(s[i]>='A' && s[i]<='Z'){
                cnt[s[i]-'A']++;
            }
            else if(s[i]>='a' && s[i]<='z'){
                cnt[s[i]-'a']++;
            }
        }
        int max=cnt[0];
        for(i=0;i<26;i++){
            if(cnt[i]>max){
                max=cnt[i];
            }
        }
        for(i=0;i<26;i++){
            if(cnt[i]==max){
                printf("%c",i+'a');
            }
        }
        printf("\n");


    }
    return 0;
}
okokkook
New poster
Posts: 4
Joined: Wed Apr 30, 2014 1:55 pm

11577 - Letter Frequency help me :'(

Post by okokkook »

Who can help me I can't find error.

Code: Select all

#include <stdio.h>
#include <ctype.h>
int main(){
    int n;
    scanf("%d",&n);
    getchar();
    while(n--){
        char s[1000],cnt[26]={0};
        gets(s);
        int i;
        for(i=0;s[i];i++){
            if(s[i]>='A' && s[i]<='Z'){
                cnt[s[i]-'A']++;
            }
            else if(s[i]>='a' && s[i]<='z'){
                cnt[s[i]-'a']++;
            }
        }
        int max=cnt[0];
        for(i=0;i<26;i++){
            if(cnt[i]>max){
                max=cnt[i];
            }
        }
        for(i=0;i<26;i++){
            if(cnt[i]==max){
                printf("%c",i+'a');
            }
        }
        printf("\n");


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

Re: 11577 - Letter Frequency

Post by brianfry713 »

Don't double post.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 115 (11500-11599)”