10062 - Tell me the frequencies!

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

Moderator: Board moderators

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Post by Zhao Le »

Because there should no blank line in the last output.
AC makes me feels good,
But WA makes me thinks hard.
r.z.
Learning poster
Posts: 56
Joined: Thu Jun 05, 2003 1:57 pm

10062

Post by r.z. »

what's wrong with my code?

[c]
#include<stdio.h>


void main()
{
int f[256];
char line[1001];
int i,min,idx;
int ctr;


/*
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
*/

while(gets(line)!=NULL)
{

for(i=0;i<256;i++)
{
f=0;
}

for(i=0;line!='\0';i++)
{
f[line]++;
}

do{
ctr=0;
min=255;
for(i=32;i<128;i++)
{
if(f!=0 && f<=min)
{
min=f;
idx=i;
ctr++;
}
}
if(!ctr)
break;
printf("%d %d\n",idx,min);
f[idx]=0;

}while(1);
printf("\n");
}

/*
fcloseall();
*/


}
[/c]

I've tested it and it's seems fine to me....

any critical input?
noren
New poster
Posts: 3
Joined: Tue Nov 18, 2003 1:07 pm
Location: Ume

Post by noren »

arc16: are you sure about the range 33-128?
The given lines will contain none of the first 32 or last 128 ASCII characters
/johan
Kamanashish
New poster
Posts: 10
Joined: Wed Dec 17, 2003 3:12 pm
Location: Dhaka
Contact:

10062 Why wrong answer?

Post by Kamanashish »

I got wrong answer. Can somebody check my code.

[cpp]
The code is removed.
[/cpp]
Last edited by Kamanashish on Tue Oct 26, 2004 3:18 pm, edited 1 time in total.
Work hard.
Tomislav Novak
New poster
Posts: 44
Joined: Fri Feb 20, 2004 5:52 pm

10062 - Tell me the frequences

Post by Tomislav Novak »

Hi.

Here is the code I keep getting WA for:
[c]
#include <stdio.h>

int main()
{
char line[1001];
int freq[96], i, j, max;

while(fgets(line, 1000, stdin))
{
line[strlen(line) - 1] = '\0';
max = 0;
memset(freq, 0, sizeof(freq));

for (i = 0; i < strlen(line); i++)
{
if (line >= 32 && line < 128)
{
freq[line - 32]++;
if (freq[line - 32] > max) max = freq[line - 32];
}
}

for (i = 1; i <= max; i++)
for (j = 96; j >= 0; j--)
if (freq[j] == i) printf("%d %d\n", j + 32, i);

printf("\n");
}

return 0;
}
[/c]

Can anyone tell me where the mistake is, or at least give me some more test data?

Thanks.
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Re: 10062 - Tell me the frequences

Post by little joey »

[quote="Tomislav Novak"]Hi.

Here is the code I keep getting WA for:
[c]
#include <stdio.h>

int main()
{
char line[1001];
// make it a little bigger, 1024 will do

int freq[96], i, j, max;
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

I have give this input in your program:

AAAAAAAAABBBBBBBBBBBBCCCDDDDDDEEEEEEEEFFFFFFaaaaaaaarrrrrrrrrrrrrrrrrrrrrrrrrr

and it give output:

67 3
114 6
70 6
97 8
69 8
65 9
66 12
68 26

you see here 114(r) is 6 and 68(D) is 26

but output should be 68 6 and 114 26

try to overcome this problam. :)

note:you don't need to declare c[1001] two time. just remove the first declaration.
A1
Experienced poster
Posts: 173
Joined: Wed Jan 28, 2004 3:34 pm
Location: Bangladesh

Post by A1 »

when i compile your code using g++ compiler it give this message:

" the `gets' function is dangerous and should not be used."

But I think it should not be a reason for WA.
for all input (i test) your code gives same output as my AC code give. :-?
there may be some lack in gets function.
I solve it using getchar();
So, try again :wink:
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

10062 > Tell me the frequencies! > Code changed.

Post by _.B._ »

Greetings!.
Can anyone PLEASE tell me why I got "Runtime Error (SIGSEGV)" in this problem?? :o

The limits for the "ordinal" of the characters read are 33 to 127??. No character read, except for eoLn and eoF, will be out of those limits??.
since there is a Max = 1000 for the characters in a line, the maximum value for a char will be 1000, rite??.
Thanks in advance.

The message on my e-mail is:
Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

Before crash, it ran during 0.045 seconds.
My code:
[pascal]{ Bernardo E. L
Last edited by _.B._ on Wed Apr 14, 2004 9:26 pm, edited 1 time in total.
_.
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR »

Your sample output is ok.

Perhaps you should change the limit for valid data to 256.
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Post by _.B._ »

Hello again WR!.
Thanks for the tip, but nada :o
Changed the code again.
I hate this... it was supposed to be a simple code...
Thanks, one more time.
Keep posting!.
_.
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR »

What about blanks (ASCII 32)??!!
_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

OUCH!.

Post by _.B._ »

WR wrote:What about blanks (ASCII 32)??!!
OUCH!.
Seems to be that I forgot how to substract :o
Thanks again WR!.
Got AC/PE. Will check for the PE now.
Keep posting!, and I'll re-study mathe basics :lol:
_.
Pavl0
New poster
Posts: 16
Joined: Sun Apr 18, 2004 2:57 pm

10062 tell me frequences!!!! WA 10062

Post by Pavl0 »

hello
why j get wa ??


#include<stdio.h>

char stat [512];

void pw(void)
{
int i,j;

for(i=1;i!=1024;i++)
{
j=500;
while(j--)
{
if(stat[j]==i)printf("%d %d\n",j,stat[j]);
}

}

}

main()
{
int i;
char ch;

while(1)
{
ch=getchar();
if(ch=='\n'|| ch=='\r'){pw(); for(i=0;i!=512;i++)stat=0; printf("\n");}
else stat[ch]++;
if(ch==EOF){pw(); break;}



}



}
dark man
New poster
Posts: 7
Joined: Fri Mar 26, 2004 11:07 pm
Contact:

Post by dark man »

you have to give a blank line after each set of output but not after the last set.May be this will make your code AC.
Try hard . May be you will get the right answer.
Post Reply

Return to “Volume 100 (10000-10099)”