## 10062 - Tell me the frequencies!

Moderator: Board moderators

asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

### Hello

Hello sanbinHo I'm not a good helper and also not a good programmer.I've also solved this problem with the help of forum.Anyway,I've just run a buuble sort to print the output in the ascending order of the frequencies.And create three int arrays to put the values and frequencies and the sorted value.
Anyway,Ur code seems me as a bit complex.Hope I can help u little bit.And U can try to solve the problem with my procedure.

jasiu--
New poster
Posts: 3
Joined: Tue May 31, 2005 8:49 pm
i see you are counting letters x, for which 32 < x <= 128. as far as i remember, it should be 32 <= x < 128. perhaps this will help. regards,

jasiu

Piotr
New poster
Posts: 2
Joined: Mon Jun 13, 2005 2:12 pm

### 10062. Why got WA??

program p10062;

{\$APPTYPE CONSOLE}

uses
SysUtils;

var
t: array[0..256] of integer;
i,min,liczba: integer;
b: boolean;
s: ansistring;

begin
while not eof(input) do
begin

if s<>'' then
begin
for i:=1 to length(s) do
inc(t[ord(s)]);

repeat
min:=1001;
b:=true;
for i:=33 to 128 do
if (t<=min)and(t<>0) then
begin
min:=t;
liczba:=i;
end;

writeln(liczba,' ',min);

t[liczba]:=0;
for i:=33 to 128 do
if t<>0 then b:=false;

until b=true;
writeln;
end;
end;
end.

GeorgeBusch
New poster
Posts: 10
Joined: Fri Jun 10, 2005 5:30 pm
Hello, i just found out your mistake.
It is really really sad mistake:
In the statement
for i:=33 to 128 do
you should begin with 32, because the first 32 chars are 0..31!!!!

i got your code acc but with PE cause you print a newline after last case.

thirddawn
New poster
Posts: 9
Joined: Sat Oct 15, 2005 4:41 am

### 10062 WA...Need help

Code: Select all

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

int main(){
char word[1001];
int number[1001];
int i,max,j;
while (scanf("%s",&word)==1){
max = strlen(word);
for (i = 0; i < 257;i++)
number[i] =0;
for (i = 0; i < max;i++)
number[word[i]] = number[word[i]] + 1;
for (j = 1; j <=max ; j ++){
for (i = 256; i >=0 ;i--){
if (number[i] == j)
printf("%d %d\n",i,number[i]);
}
}
}
}
``````
My code are as above

I get passed all the sample input and the input which is offered on this board...but I can't figure out why I always get WA...

Thanks for helping

tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

### Re: 10062 WA...Need help

Your code should handle the space character as one character.
To avoid this matter, it's easy to use gets() function instead of scanf() for implementation.

Input :

Code: Select all

``````a a a
``````
Output :

Code: Select all

``````32 2
97 3
``````
And, see this to avoid PresentationError.
A blank line should separate each set of output.
Best regards.

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil
try to write your code in C

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Contact:
I don't remember but I think once I found in the forum that there contains characters of ASCII greater than 128. Search the forum. That's what you should do before posting your question.

ashikzinnatkhan
New poster
Posts: 8
Joined: Wed Jan 25, 2006 6:25 pm

Here is my code:

#include <stdio.h>

void main()
{
unsigned char ch ;
int i , j , start = 0;
int ascii[96][2] , temp0 , temp1 ;

for(i=0 ; i<96 ; i++)
{
ascii[0] = i+32 ;
ascii[1] = 0 ;
}

while( (char) ( ch = getchar() ) != EOF )
{

if((ch == '\n') || (ch == '\r')) start=1;

i = ch;

ascii[i-32][1]++ ;

if(start == 1)
{
for( j=0 ; j<48; j++)
{
temp0 = ascii[j][0];
temp1 = ascii[j][1];

ascii[j][0] = ascii[95-j][0];
ascii[j][1] = ascii[95-j][1];

ascii[95-j][0] = temp0;
ascii[95-j][1] = temp1;

}

for( i=1; i<96 ; ++i)
for(j=95; j>=i; --j)
{
if(ascii[j-1][1] > ascii[j][1] )
{
temp0 = ascii[j-1][0];
temp1 = ascii[j-1][1];

ascii[j-1][0] = ascii[j][0];
ascii[j-1][1] = ascii[j][1];

ascii[j][0] = temp0;
ascii[j][1] = temp1;

}

}

for( i=0; i<96; i++)
{
if(ascii[1] > 0) printf("%d %d\n",ascii[0] , ascii[1] );

}

printf("\n");

start = 0;

for(i=0 ; i<96 ; i++)
{
ascii[0] = i+32 ;
ascii[1] = 0 ;
}

fflush(stdin);

}

}

}

It is giving PE.
I cannot understand why.
Ashik

tWd2
New poster
Posts: 1
Joined: Wed Jan 25, 2006 6:53 pm

Here is my code:

#include<iostream>
using namespace std;
int main()
{
int a[200];
for(int i=0;i<=200;i++)a=0;
int min=1000,max=0,n,t;
char c;

while(cin.get(c))
{
n=c;
if(cin.fail())return 0;
if(n==10||n==32)
{
if(t==1)
{
for(int i=200;i>=0;i--)
{
if(a<min && a>0 ) min=a;
if(a>max) max=a;
}
for(;min<=max;min++)
for(int i=200;i>=0;i--)
if(a==min)
cout << i+1 << " " << a << endl;
cout<<endl;
}
min=1000;
max=0;
t=0;
for(int i=0;i<=200;i++)
a=0;
}
else if(n>32)
{
a[n-1]++;
t=1;
}
}
}

I've tried many times but it is still Runtime Error.

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Contact:
In several places you're trying to access a[200] where size of a is 200.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
there shouldn't be a blank line at the end of output..

you're output is like ths..

Code: Select all

``````67 1
66 2
65 3

49 1
50 2
51 3

52 2
51 4
48 4
50 5
49 5
<---- delete this line..
``````

Ankur Jaiswal
New poster
Posts: 31
Joined: Sat Apr 01, 2006 6:24 am
Contact:

### 10062

I am using the straightforward algo to solve this problem. However I am using an array of size just 97 (for storing the values from 32 to 128 as given in the question). Is this the problem. I also understood the question as follows : if the frequency of two or more characters is the same, then print the character with the greater ASCII value first. Is this the problem? If not any one of the above mentioned, then what else can be the problem?

serur
A great helper
Posts: 251
Joined: Thu Feb 23, 2006 11:30 pm

### Tell him the frequencies and have done with it!

Try to use multifield sorting( if you didnt do that already) - this being the most advanced algo for problems such these.

smartNET
New poster
Posts: 2
Joined: Fri Apr 14, 2006 12:05 am

this is my code , can you help me to find my wrong please ????
C++

Code: Select all

``````#include<iostream.h>
#include<string.h>
char tabla[1000][1000];

struct tablita{
int simbolo[1000];
};
tablita final[1000];
void ordena(int);
void cuenta(int);
void main()
{
long int y,local;
y=10;
for( local=0;local<10;local++)
{
cout<<local;cin.getline(tabla[local],1000);
}

ordena(local);

for(int v=0;v<y;v++)
{
int w;
w=strlen(tabla[v]);
for(int h=0;h<w;h++)
{
//	cout<</*v<<h<<" "<<*/tabla[v][h];
}
//cout<<endl;
}
cuenta(y);

}

void ordena(int a)
{
for(int u=0;u<a;u++)
{
int n;
n=strlen(tabla[u]);

int i,j;
char temp;
for(i=1;i<n;i++)
{
for(j=n-1;j>=i;j--)
{
if(tabla[u][j-1]>tabla[u][j])
{
temp=tabla[u][j-1];
tabla[u][j-1]=tabla[u][j];
tabla[u][j]=temp;
}
}
}
}
}

void cuenta(int y)
{
int dir[1000][2];
for(int i=0;i<y;i++)
{
int c,es=0;

c=strlen(tabla[i]);
for(int b=0;b<c;b++)
{
dir[i][0]=i;
if (b==0)
{
if((tabla[i][b]>=32) &&  (tabla[i][b]<=128))
{
final[i].simbolo[es]=tabla[i][b];
dir[i][1]=es;
}

}
if (b>0)
{
if((tabla[i][b]>32) &&  (tabla[i][b]<128))
{
if(tabla[i][b]==tabla[i][b-1])
{

}
if(tabla[i][b]!=tabla[i][b-1])
{
es++;
final[i].simbolo[es]=tabla[i][b];

dir[i][1]=es;
}
}
}
}
}

int num,sym,bu,ba;
for(int palabra=0;palabra<y;palabra++)
{

for(bu=1;bu<dir[palabra][1]+1;bu++)
{
for(ba=(dir[palabra][1]);ba>=bu;ba--)
{

{
sym=final[palabra].simbolo[ba-1];

final[palabra].simbolo[ba-1]=final[palabra].simbolo[ba];

final[palabra].simbolo[ba]=sym;
}

{
int app;
app=final[palabra].simbolo[ba-1];
//	abb=final[palabra].simbolo[ba];
final[palabra].simbolo[ba-1]=final[palabra].simbolo[ba];
final[palabra].simbolo[ba]=app;

}

}
}
}

for(int pala=0;pala<y;pala++)
{
for(int cont=0;cont<dir[pala][1]+1;cont++)
{