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

willy
New poster
Posts: 11
Joined: Fri Aug 01, 2003 2:20 pm
Location: Singapore

Thank you

Post by willy »

Oh, :oops: Calculation error :oops: heehee thanks for pointing it out
Kamanashish
New poster
Posts: 10
Joined: Wed Dec 17, 2003 3:12 pm
Location: Dhaka
Contact:

499 What's the frequency Kenneth

Post by Kamanashish »

Hi, i am getting RTE(SIGSEGV), please help.
[cpp]
CUT
[/cpp]
Last edited by Kamanashish on Sun Dec 21, 2003 8:12 am, edited 1 time in total.
Work hard.
CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Re: 499 What's the frequency Kenneth

Post by CDiMa »

Kamanashish wrote:Hi, i am getting RTE(SIGSEGV), please help.
[cpp]
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main ()
{
int i,j,n,last[1000],max;
char *a;

while(gets(a))
[/cpp]
char *a allocates space for an uninitialized pointer, so you are reading your input into random mem -> segmentation fault.
You can use malloc to dinamically allocate sufficient storage for the input data or declare an array big enough.

Ciao!!!

Claudio
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel »

Your code is perfectly alright apart from the correction suggested.
I have changed that part of your code and got it AC. Edit that part and Submit.
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

In this problem, one of trap is input data is "Blank line".
When blank line received, your program must print

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0

Plz check it :)
soyoja
Experienced poster
Posts: 106
Joined: Sun Feb 17, 2002 2:00 am
Location: Seoul, South Korea
Contact:

Post by soyoja »

In this problem, one of trap is input data is "Blank line".
When blank line received, your program must print

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0

Plz check it :)
prince56k
New poster
Posts: 33
Joined: Fri Dec 12, 2003 10:32 pm
Location: BANGLADESH

Post by prince56k »

To soyoja,

It's not the trap u r thinking. my code doesn't print any thing incase of a blank line. there is no blank line in the input file. If there were blank line in the judges input file then u might got WA :lol:
GreenPenInc
Learning poster
Posts: 53
Joined: Sat May 01, 2004 9:31 pm
Contact:

Post by GreenPenInc »

499 is disgusting and abhorrent. It smacks of ignorance and should immediately be removed from the problem set.
_-(GPI)-_

"Finally I have freed myself from the clutches of the garbage fairy!"
midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

Post by midra »

I got TLE in this problem...
is there any special Input/Output for this problem???

Here is my code:
[c]
#include <stdio.h>

char swap (char *a,char *b)
{
*a^=*b;
*b^=*a;
*a^=*b;
}


int main()
{
int i,j,x,k; /*counters*/
char c[10000]; /*Characters for every line*/
char temp[10000];
int rep[10000];
int index,min;
while(1)
{
i=index=0;
while(c[i-1]!='\n')
{ /*Read until NewLine*/
scanf("%c",&c);
if (c=='\n' && i==0)
printf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0\n");
else if (c!=' ')
i++;
}
i--;

for (j=0;j<i;j++)
for (x=j+1;x<=i;x++) /*Compare since first letter*/
if (c[j]==c[x])
rep[j]++; /*If they are equals rep[number of letter]++*/


temp[0]=c[0];
for (x=1;x<i;x++)
{
if (rep[x]>rep[0])
{
temp[index]=c[x];
rep[0]=rep[x];
rep[x]=0;
}
}

for (x=1;x<i;x++)
if (rep[x]==rep[0])
{
index++;
temp[index]=c[x];
}

for (k=0;k<index;k++)
{
min=k;
for (j=k+1;j<=index;j++) /*Selection Sort*/
if (temp[j]<temp[min])
swap(&temp[j],&temp[min]);
}


for (k=0;k<=index;k++)
printf("%c",temp[k]);
printf(" %d\n",rep[0]+1);
for (j=0;j<=i;j++) /*Put all 0's in the array*/
rep[j]=0;
}
return 0;
}[/c]
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

You have a while(1) that never breaks.
midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

Post by midra »

thanks... but
I have changed for this:
[c]
#include <stdio.h>

char swap (char *a,char *b)
{
*a^=*b;
*b^=*a;
*a^=*b;
}


int main()
{
int i,j,x,k; /*counters*/
char c[10000]; /*Characters for every line*/
char temp[10000];
int rep[10000];
int index,min;
while(1)
{
i=index=0;
while(c[i-1]!='\n')
{ /*Read until NewLine*/
scanf("%c",&c);
if (c==EOF) /*I ADD THIS PART IN THE CODE*/
return 0;
else if (c=='\n' && i==0)
printf("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0\n");
else if (c!=' ')
i++;
}
i--;

for (j=0;j<i;j++)
for (x=j+1;x<=i;x++) /*Compare since first letter*/
if (c[j]==c[x])
rep[j]++; /*If they are equals rep[number of letter]++*/


temp[0]=c[0];
for (x=1;x<i;x++)
{
if (rep[x]>rep[0])
{
temp[index]=c[x];
rep[0]=rep[x];
rep[x]=0;
}
}

for (x=1;x<i;x++)
if (rep[x]==rep[0])
{
index++;
temp[index]=c[x];
}

for (k=0;k<index;k++)
{
min=k;
for (j=k+1;j<=index;j++) /*Selection Sort*/
if (temp[j]<temp[min])
swap(&temp[j],&temp[min]);
}


for (k=0;k<=index;k++)
printf("%c",temp[k]);
printf(" %d\n",rep[0]+1);
for (j=0;j<=i;j++) /*Put all 0's in the array*/
rep[j]=0;
}
return 0;
} [/c]

And I still got TLE.
Could it be another reason???
thanks a lot!!
bye
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 »

Same reason. Your not reading input correctly. scanf returns EOF "if an input failure occurs before any conversion such as an end-of-file occurs" (man scanf)
midra
Experienced poster
Posts: 119
Joined: Fri Feb 13, 2004 7:20 am
Contact:

Post by midra »

Thanks! I Finally Got AC!!!:D :D :D
good luck!
byee!
[senu]
New poster
Posts: 6
Joined: Tue May 18, 2004 6:59 pm

Post by [senu] »

I got WA and I dont know why, Can You help me?

BTW I think ACM.UVA should delete/change problems like that. I hate easy problems with stupid inputs and outputs like 739.


[cpp]
#include <stdio.h>
#include <iostream>
#define MAX 256
#define f(x)for(int i=0; i<=x; i++)
#define f2(x,y)for(int i=x; i<=y; i++)

using namespace std;

int i,k,j,l,m,n,qq,a,b,c,maxc;
char s[MAX];
int t[255];

void rob(char *s)
{
f(254) t=0;
maxc=0;
int i=0,j,k,l;
while(s!='\0' && s!='\n')
{
t[int(s)]++;
if(t[int(s)]>maxc && int(s)!=32) maxc=t[int(s)];
/* previous version */
// if(t[int(s)]>maxc && ((int(s)>=65 && int(s)<=90) ||(int(s[i])>=97 && int(s[i])<=122))) maxc=t[int(s[i])];
i++;
}
f2(35,255) /* there were 2 fors (1st 65->90 & 2nd 97->122)*/
{
if (t[i]==maxc) cout << char(i);
}
cout << " " << maxc << endl;
}

int main()
{
while(cin)
{
cin.getline(s,MAX);
rob(s);
}
return 0;
}
[/cpp]
txandi
New poster
Posts: 25
Joined: Sun Feb 29, 2004 2:06 am

499 - WA

Post by txandi »

:evil: I hate problems like this one... too easy... :evil:

I'm getting WA all the time... and I can't understand why. There must be a tricky input...

I've 2 questions:

1. Does the last line of input finish with '\n' or just with feof ???
2. What we are suposed to do if there is a blank line or a line with no valid characters ?? do we have to print "ABC...XYZabc....xyz 0" in both cases??

Thanks a lot!
Post Reply

Return to “Volume 4 (400-499)”