499 - What's The Frequency, Kenneth?

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

Moderator: Board moderators

JaviGS
New poster
Posts: 6
Joined: Thu Aug 05, 2004 5:24 pm
Location: Spain

Post by JaviGS »

I think there is a \n after the last line
Regards
Javi

ginie
New poster
Posts: 4
Joined: Sun Feb 20, 2005 11:46 pm

499 SOS

Post by ginie »

Hi everybody!
I'm feed up of #499 as i can't make out what's wrong with my code - i get WA-. The only part i'm not sure about it is the way the program may end executing (ive tried with while(cin) and While(!feof(stdin)) ). Please help

#include <stdio.h>
#include <iostream.h>
#include <string.h>
void main()
{
int llista[52],sol[52],i,veg,repe,mi,ma,j,swap,min,k,lon;
char x[128],resp;

while (cin)
{
cin.getline(x,128,'\n');
lon=strlen(x);
for(k=0; k<52; k++)
llista[k]=llista[k]^llista[k];
for(k=0; k<lon; k++)
{
ma= ((int) x[k])-'A';mi= ((int) x[k])-'a';
if( (ma<26) && (ma >=0) ) llista[ma]++;
else if( (mi< 26) && (mi>=0) ) llista[mi+26]++;
}

veg=veg^veg; sol[0]=sol[0]^sol[0]; repe=repe^repe;
for( i=0; i<52; i++)
{
if( llista>veg ){ veg=llista; sol[0]=i; repe=repe^repe;}
else if( llista==veg ){ repe++; sol[repe]=i;}
}

for(i=0; i<=repe; i++)
{
min=sol;swap=i;
for(j=i+1; j<=repe; j++)
if(sol[j]<min) {min=sol[j];swap=j;}
sol[swap]=sol; sol=min;
}

if(lon==0)
{
cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0"<<endl;
}

else
{
for (i=0; i<=repe; i++)
{
if(sol>25) resp=(char)(sol-26+'a');
else resp=(char)(sol+'A');
cout<<resp;
}
cout<<" "<<veg<<endl;
}
}
}

Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho »

Code: Select all

The only part i'm not sure about it is the way the program may end executing (ive tried with while(cin) and While(!feof(stdin)) ). 
That's your BUG, nice guess.

Just two changes and you'll get ACC.

1) Read simply by using stdio.h.
Just do this :

Code: Select all

while (gets(x))  {
    /* YOUR CODE */ ; 
}
2) And as a subsequence of 1), of course you
should remove the first line in your while, which is :

Code: Select all

cin.getline(x,128,'\n');

Without these changes your program was giving WA
in the case when after the last line in INPUT there's
no NEWLINE character, but just directly an EOF.
Maybe it was giving WAs in other test cases too.

ginie
New poster
Posts: 4
Joined: Sun Feb 20, 2005 11:46 pm

THANKS

Post by ginie »

THANKS A LOT, I'VE GET ACC :D :D :D
I'LL PAY MORE ATENTION ON THAT KIND OF ERRORS, IT'S JUST THAT IT UPSETS ME WA WHEN YOUR CODE IS CORRECT BUT UNABLE TO READ THE INPUT'S FORMAT

elmedin
New poster
Posts: 3
Joined: Wed Apr 13, 2005 7:30 pm

499 WA

Post by elmedin »

please help

Code: Select all

#include <iostream>
#include <map>
#include <string>

using namespace std;
int main()
{
   string s;
   char hej;
   int i=0, k=0;
   char maj[1000];
   map<char, int> counter;
   while(getline(cin,s))
   {
      for(int l=0; l<s.length();l++)
      {  
         if(s[l]!=' ')     
         {
            hej=s[l];
            counter[hej]++;
         }
      }
      for(map<char, int>::const_iterator memo = counter.begin(); memo!=counter.end(); memo++)
      {
         if(memo->second > k)
         {
            i=0;
            k = memo -> second;
            maj[i]= memo->first;
         }
         else if(memo->second == k)
         {
            i++;
            maj[i]= memo->first;
         }
      }   
      for(int j=0; j<=i; j++)
      {
         cout<<maj[j];
      }
      cout<<" "<<k<<endl;
      k=0;
      counter.erase(counter.begin(), counter.end());
   }
   return 0;
}

Salman
New poster
Posts: 25
Joined: Thu Jun 26, 2003 9:45 am

About 499

Post by Salman »

why WA ????
SOME ONE PLEASE HELP!







Code: Select all


#include<stdio.h>
 
int main(){

     char  c;
     int my[52]={0},i,max;
     
        
        while(scanf("%c",&c)!=EOF){
         if(c!='\n'){
            if(c>=65 && c<=90){
                my[c-65]++;
            }    
            else if(c>=97 && c<=122){
                my[c-97+26]++;
            }    
            
            
         } 
         if(c=='\n'){
             max=0;
             for(i=0;i<26;i++){
                 if(my[i]>=my[i+26]){
                     my[i]+=my[i+26];
                     if(max<my[i]){
                         max=my[i];
                      }     
                     my[i+26]=0;
                 }    
                 else{
                     my[i+26]+=my[i];
                     if(max<my[i+26]){
                         max=my[i+26];
                     }    
                     my[i]=0;
                           
                 }    
             }  
   
             for(i=0;i<52;i++){
                 if(my[i]==max){
                    if(i<26){ 
                     printf("%c",i+65);
                    }
                    else{
                     printf("%c",i+97-26);   
                    }         
                 }                 
                 my[i]=0; 
             }    
             printf(" %d",max);
             printf("\n");
         }     //newline;  
  }    //while


      return 0;
}

sahand
New poster
Posts: 19
Joined: Sat Mar 12, 2005 5:56 pm
Location: Halifax, Nova Scotia, Canada
Contact:

Post by sahand »

I replaced this part of your code

Code: Select all

 for(i=0;i<26;i++){
                 if(my[i]>=my[i+26]){
                     my[i]+=my[i+26];
                     if(max<my[i]){
                         max=my[i];
                      }     
                     my[i+26]=0;
                 }   
                 else{
                     my[i+26]+=my[i];
                     if(max<my[i+26]){
                         max=my[i+26];
                     }   
                     my[i]=0;
                           
                 }    
with:

Code: Select all

max=0;
			for(i=0;i<52;i++)			
				if(my[i]>max)
					max=my[i];			
And got AC.

Upper case letters are supposed to be different letters from their relative lower case ones.

59557RC
New poster
Posts: 26
Joined: Sun Mar 20, 2005 9:28 pm
Location: bangladesh
Contact:

499-PLS help

Post by 59557RC »

i dont understantd why my code outputs somethin strange for inputin 1st samp in. for 1st time. but when 2nd time i input somethin it outputs correctly. some 1 pls explain me whats wrong ?

#include<stdio.h>

int main(void)
{

char a[200],str[200],max;
int i,j,k;

while(gets(str)!=NULL){
max=0;
for(i='a';i<='z';i++) a=0;
for(i='A';i<='Z';i++) a=0;

for(i=0;str!='\0';i++){
if(a[str]==0){
for(j=i;str[j]!='\0';j++) if(str==str[j]) a[str]++;
if(a[str]>max) max=a[str];
}
}
for(i='A';i<='Z';i++) if(a==max) printf("%c",i);

for(i='a';i<='z';i++) if(a==max) printf("%c",i);
printf (" %d",max);
printf("\n");
}
return 0;
}
aaa

ronaldsaha
New poster
Posts: 5
Joined: Tue Jun 28, 2005 3:50 am
Location: Bangladesh
Contact:

Post by ronaldsaha »

Before opening a new thread. pls look for what you are looking for.
visit this link bellow you will be benefited.

http://online-judge.uva.es/board/viewtopic.php?t=2712

for input

Code: Select all

When riding your bicycle backwards down a one-way street, if the
wheel falls of a canoe, how many ball bearings does it take to fill
up a water buffalo?
Hello Howard.
Hello                  Howard.
(this is a blank line)
-------( - are spaces only)
~!@#$%^&*()_+{}:"<>?`1234567890-=[];',./
my accepted code prints

Code: Select all

e 6
al 7
a 3
Hlo 2
Hlo 2
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0
hope this will help you & like to hear you from soon that you got accepted.

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

If there are are several letters that have the highest frequency u should print all of them alphabetically.
Did you consider this???
Ami ekhono shopno dekhi...
HomePage

pajaskowiak
New poster
Posts: 1
Joined: Fri Sep 16, 2005 6:08 am

499 - Argggg, help pls

Post by pajaskowiak »

i tryed most of the inputs in other msgs and got WA :evil:

here is my code...

Code: Select all

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

int main() {
   int tamanho = 0;
   int i = 0;
   int aux1;
   char aux2;
   int j=0;   
   int maior=0;   
   char *linha = new char [4000];;
   char letras[300];
   int quantidade[300];
   int indice=0;
   strcpy(linha,"");
   bool teste = false;
   while (gets(linha)) {      
      for(i=0; i<300; i++) {
         quantidade[i] = 0;
      }
      tamanho = strlen(linha);            
      indice = 0;      
      maior = 0;          
      for (i=0; i<tamanho; i++) {         
         teste = false;
         for(j=0; j<indice; j++) {            
            if (linha[i] == letras[j]) {
               quantidade[j]++;
               teste = true;
            }
         }
         if (!teste) {
            if (isalpha(linha[i])!=0) {                  
               letras[indice] = linha[i];                  
               quantidade[indice] = 1;
               indice++;
            }
         }
      }      
      if (indice > 0) {     
         for(i=0; i<(indice-1); i++) {
            for(j=(indice-1); j>1; j--) {
               if (letras[j-1] > letras[j]) {
                  aux2 = letras[j-1];
                  aux1 = quantidade[j-1];
                  letras[j-1] = letras[j];
                  quantidade[j-1] = quantidade[j];
                  letras[j] = aux2;
                  quantidade[j] = aux1;
               }
            }
         }   
         for(i=0; i<indice; i++) {            
            if (quantidade[i]>quantidade[maior]) {
               maior = i;
            }
         }
         
         maior = quantidade[maior];
         
         for (i=0; i<indice; i++) {            
            if (quantidade[i] == maior) {
               cout<<letras[i];
            }
         }
         cout<<" "<<maior<<endl;
      }
      else {
         cout<<"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0 "<<endl;
      }      
   }      
   return 0;
}
can pls someone help me? i know its a silly problem, but i cant get AC =////

n00i3
New poster
Posts: 20
Joined: Thu Sep 15, 2005 8:49 pm
Contact:

Post by n00i3 »

^^^ map is sorterd by default :)

hey man in your check you are using

Code: Select all

if(s[l]!=' ')
what about , . and other special characters? do an isChar check and try submitting your solution :) e.g

Code: Select all

When riding your bicycle backwards down a one-way street, if the
wheel falls of a canoe, how many ball bearings does it take to fill
up a water buffalo?
Hello Howard.
this, is, abso, lutely, crazy, dude.

Code: Select all

e 6
al 7
a 3
Hlo 2
, 5
you dont want that comma there now do you ;)

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

The problem states
The list of letters should be an alphabetical list of upper case letters followed by an alphabetical list of lower case letters.
Input:

Code: Select all

bbaa
Output:

Code: Select all

ab 2
But your code returns...

Code: Select all

ba 2
Hope it works.
Ami ekhono shopno dekhi...
HomePage

=viki=
New poster
Posts: 23
Joined: Mon Jan 02, 2006 6:23 pm
Contact:

Post by =viki= »

im getting wrong answer(but its working for the input given with the problem)

Code: Select all

#include<stdio.h>

int count[51];
void _initialize()
{
	int i;
	for(i=0;i<52;i++)
		count[i]=0;
}
int main(void)
{
	char ch,line[100];
	int i,j,max=0;

	while(1)
	{
		ch=getchar();
		if(ch>=65 && ch<=90)
		{
			count[ch-65]++;
		}
		if(ch>=97 && ch<=122)
		{
			count[ch-71]++;
		}
		if(ch=='\n')
		{
			for(j=0;j<52;j++)
			{
				if(max<count[j])
					max=count[j];
			}
			for(j=0;j<52;j++)
			{
				if(count[j]==max && j<=25)
				{
					printf("%c",j+65);
				}
				if(count[j]==max && j>25)
				{
					printf("%c",j+71);
				}
			}
			printf(" %d\n",max);
			max=0;
			_initialize();
		}
		if(ch=='.')
		{
			for(j=0;j<52;j++)
			{
				if(max<count[j])
					max=count[j];
			}
			for(j=0;j<52;j++)
			{
				if(count[j]==max && j<=25)
				{
					printf("%c",j+65);
				}
				if(count[j]==max && j>25)
				{
					printf("%c",j+71);
				}
			}
			printf(" %d\n",max);
			break;
		}
	}
	return 0;
}
plz help where am i wrong....

=viki=
New poster
Posts: 23
Joined: Mon Jan 02, 2006 6:23 pm
Contact:

499 freq.. WA (getting output on my system though..)

Post by =viki= »

help me plz...
for the inputs supplied with the problem it works dunno why im getting WA

Code: Select all

#include<stdio.h>

int count[51];
void _initialize()
{
	int i;
	for(i=0;i<52;i++)
		count[i]=0;
}
int main(void)
{
	char ch,line[100];
	int i,j,max=0;

	while(1)
	{
		ch=getchar();
		if(ch>=65 && ch<=90)
		{
			count[ch-65]++;
		}
		if(ch>=97 && ch<=122)
		{
			count[ch-71]++;
		}
		if(ch=='\n')
		{
			for(j=0;j<52;j++)
			{
				if(max<count[j])
					max=count[j];
			}
			for(j=0;j<52;j++)
			{
				if(count[j]==max && j<=25)
				{
					printf("%c",j+65);
				}
				if(count[j]==max && j>25)
				{
					printf("%c",j+71);
				}
			}
			printf(" %d\n",max);
			max=0;
			_initialize();
		}
		if(ch=='.')
		{
			for(j=0;j<52;j++)
			{
				if(max<count[j])
					max=count[j];
			}
			for(j=0;j<52;j++)
			{
				if(count[j]==max && j<=25)
				{
					printf("%c",j+65);
				}
				if(count[j]==max && j>25)
				{
					printf("%c",j+71);
				}
			}
			printf(" %d\n",max);
			break;
		}
	}
	return 0;
}
looks big but isnt that complicated....
im getting WA .. plz help...

Post Reply

Return to “Volume 4 (400-499)”