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

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

Did you test your code with the sample input? Your code does not even work for the sample input.

I get the following output when I ran your code with the sample input:

Code: Select all

012345678967 1
66 2
65 3

49 1
50 2
51 3

0 0

0 0

0 0

0 0

0 0

0 0

0 0

0 0
Did you notice you have an extra cout in your code here:

Code: Select all

long int y,local; 
   y=10; 
   for( local=0;local<10;local++) 
   { 
      cout<<local;cin.getline(tabla[local],1000); 
   } 
It is better to explain your code to allow others to help you better.
smartNET
New poster
Posts: 2
Joined: Fri Apr 14, 2006 12:05 am

This code works good with the last test case

Post by smartNET »

#include<iostream.h>
#include<string.h>
//DEFINICION DE FUNCIONES
void ordena(int);
void cuenta(int);
void imprimir(int);
//DEFINICION DE ESTRUCTURAS
struct palabrasdetalle{
int simbolo[96];
int frecuencia[96];
};
palabrasdetalle final[1000];
//DEFINICION DEL ARREGLO CONTENEDOR DE INGRESO
char contenedor[1000][1000];
//DEFINICION DE VARIABLES GLOBALES
int cantidadpalabras=0;
int estadolocal=0;
void main(void)
{
do
{
cin.getline(contenedor[cantidadpalabras],1000);
cantidadpalabras++;
}while(contenedor[cantidadpalabras-1][0]!=0);
ordena(cantidadpalabras);
cuenta(cantidadpalabras);
//imprimir(cantidadpalabras);
}
void ordena(int cantidadpalabras)
{
//utilizando metodo burbuja de menor a mayor
for(int estado=0;estado<cantidadpalabras;estado++)
{
int longitudactual;
longitudactual=strlen(contenedor[estado]);
int i,j;
char temp;
for(i=1;i<longitudactual;i++)
{
for(j=longitudactual-1;j>=i;j--)
{
if(contenedor[estado][j-1]>contenedor[estado][j])
{
temp=contenedor[estado][j-1];
contenedor[estado][j-1]=contenedor[estado][j];
contenedor[estado][j]=temp;
}
}
}
}






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

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


}
if (b>0)
{

if(contenedor==contenedor[b-1])
{
final[i].frecuencia[es]++;


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

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

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

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


if (final[palabra].frecuencia[ba-1]>final[palabra].frecuencia[ba])
{
//cout<<"intercambio "<<final[palabra].cantidad[ba-1]<<""<<final[palabra].cantidad[ba]<<endl;
num=final[palabra].frecuencia[ba-1];
sym=final[palabra].simbolo[ba-1];

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

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

//cout<<"candidades"<<final[palabra].cantidad[ba-1]<<final[palabra].cantidad[ba];

if((final[palabra].frecuencia[ba-1])==(final[palabra].frecuencia[ba]))
{
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<cantidadpalabras;pala++)
{
for(int cont=0;cont<dir[pala][1]+1;cont++)
{
if(final[pala].simbolo[cont]>=32 && final[pala].simbolo[cont]<128)
cout<<final[pala].simbolo[cont]<<" "<<final[pala].frecuencia[cont]<<endl;
}
cout<<endl;

}

}
mak(cse_DU)
Learning poster
Posts: 72
Joined: Tue May 30, 2006 5:57 pm
Location: bangladesh

10062 why WA

Post by mak(cse_DU) »

this is my code
pls find worng answer


#include<stdio.h>

void main()
{
out:
int a,x,p,i=0,j,tm1,tm2,y=0,k[128]={0},l[128]={0};
char m;
while((char)(m=getchar())!=EOF)
{
if(m=='\n'||m=='\r')
goto end;
a=(int)m;
if(a<32||a>=128)
goto out;
for(j=0;j<=i;j++)
if(a==k[j])
{
l[j]++;
y--;
break;
}
else if(j==i)
{
k[y]=a;
l[y]++;
}
y++;
i++;
if(m=='\n'||m=='\r')
{ end:

for(x=0;x<y;x++)
{
for(p=x+1;p<y;p++)
if(l[x]>l[p])
{
tm1=l[x];
l[x]=l[p];
l[p]=tm1;
tm2=k[x];
k[x]=k[p];
k[p]=tm2;
}
else if(l[x]==l[p])
if(k[p]>k[x])
{
tm2=k[x];
k[x]=k[p];
k[p]=tm2;
}
printf("%d %d\n",k[x],l[x]);
}
goto out;
}

}

}
beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Post by beloni »

hello,

I'm totally confused with your code:
1) read the string input with gets or scanf... ("The given lines will contain none of the first 32 or last 128 ASCII characters")
2) use loops instead goto
3) broke your program in functions

dont forget that you must order the result first by frequence, second by ASCII order
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor
David Jones
New poster
Posts: 3
Joined: Fri Jun 30, 2006 5:49 am

Re: 10062, Tell me the frequencies, PE, please help

Post by David Jones »

I got P.E. when my code is like
while (gets(str))
{
length = strlen(str);
if (length == 0) continue;

if (first == 1)
first = 0;
else
printf("\n");

.........
}

I got accepted without P.E. when I changed my code to
while (gets(str))
{
if (first == 1)
first = 0;
else
printf("\n");

length = strlen(str);
if (length == 0) continue;

.........
}
From_1987
New poster
Posts: 1
Joined: Thu Aug 03, 2006 10:09 pm

10062 AC (P.E.) please help!!!

Post by From_1987 »

This is my code:
=================
type mt=record
c: char;
f: longint;
end;

var a: array[32..128] of mt;
j,k,fr: integer;
ch,symb: char;
cases,first: longint;
begin

cases:=0;
repeat
inc(cases);
if cases<>1 then begin writeln; writeln; end;
fillchar(a,sizeof(a),0);
repeat
read(symb);
a[ord(symb)].c:=symb;
a[ord(symb)].f:=a[ord(symb)].f+1;

until eoln;
for j:=32 to 127 do
for k:=j+1 to 128 do
if a[j].f>a[k].f then
begin
fr:=a[j].f;
ch:=a[j].c;
a[j].f:=a[k].f;
a[j].c:=a[k].c;
a[k].f:=fr;
a[k].c:=ch;
end else
if (a[j].f=a[k].f)and(ord(a[j].c)<ord(a[k].c)) then
begin
fr:=a[j].f;
ch:=a[j].c;
a[j].f:=a[k].f;
a[j].c:=a[k].c;
a[k].f:=fr;
a[k].c:=ch;
end;
first:=1;
for j:=32 to 128 do
if a[j].f<>0 then
begin
if (first=1) then begin write(ord(a[j].c),' ',a[j].f); first:=0; end
else begin writeln; write(ord(a[j].c),' ',a[j].f); end;
end;
if not(eof(input)) then readln;
until eof(input);

end.
=======================

I don't put a blank line after the output, but it's still p.e. :-? why?
bongssi
New poster
Posts: 14
Joined: Mon Jul 31, 2006 10:35 am

10062 WA;;;

Post by bongssi »

I really don't know why I got WA. What's the matter with this code?
Plz help me~~

-----------------------------------------------------------------------------

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

#define MAX_LENGTH 1000

char string[MAX_LENGTH];
typedef struct _data{

char char_code;
int frequency;
}data;

data *freq;
int *freq_order, freq_num;
int main(void){

int i, j, dup, string_length;

while(scanf("%[^\n\r]", string) == 1){

freq_num = -1;
string_length = strlen(string);
freq = (data *)malloc(sizeof(data) * string_length);
freq_order = (int *)malloc(sizeof(int) * string_length);

for(i=0; i<string_length; i++){

dup = 0;
for(j=0; j<=freq_num; j++)
if(string == freq[j].char_code){
freq[j].frequency++;
dup = 1;
}

if(!dup){
freq[++freq_num].char_code = string;
freq[freq_num].frequency = 1;
}
}

for(i=0; i<=freq_num; i++)
for(j=i+1; j<=freq_num; j++){
if(freq.frequency > freq[j].frequency){

freq.char_code += freq[j].char_code;
freq[j].char_code = freq.char_code - freq[j].char_code;
freq.char_code -= freq[j].char_code;

freq.frequency += freq[j].frequency;
freq[j].frequency = freq.frequency - freq[j].frequency;
freq.frequency -= freq[j].frequency;
}

else if(freq.frequency == freq[j].frequency){

if(freq[i].char_code < freq[j].char_code){

freq[i].char_code += freq[j].char_code;
freq[j].char_code = freq[i].char_code - freq[j].char_code;
freq[i].char_code -= freq[j].char_code;

freq[i].frequency += freq[j].frequency;
freq[j].frequency = freq[i].frequency - freq[j].frequency;
freq[i].frequency -= freq[j].frequency;
}
}
}


for(i=0; i<=freq_num; i++){

printf("%d %d\n", freq[i].char_code, freq[i].frequency);
}

printf("\n");
fflush(stdin);
free(freq);
free(freq_order);
}
return 0;
}
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post by Darko »

I don't know if that's the reason, but my MAX_LENGTH is 1010.
kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi »

i thought this problem is a easy 1....but i got WA for my code too :(
i just simply count the fequencies and then print them into their ascending order....

Code: Select all

#include<stdio.h>
#include<string.h>
#define MAX 1010
int main()
{
	char str[MAX];
	while(gets(str))
	{
		int sum[257]={0},len,i,j,temp;
		
		len=strlen(str); 
		for(i=0;i<len;i++) 
		sum[str[i]]++; 
	
		for(i=0;i<256;i++)
			{ 
				int min=MAX;
				for(j=0;j<256;j++)
				{
					if((sum[j]<min)&&(sum[j]!=0))
					{
						min=sum[j];
						temp=j;
					}
					
				}
				sum[temp]=MAX;
				if(min!=MAX) printf("%d %d\n",temp,min); 
			} 
		
	}
	
	return 0;
	
}
plzz sumbuddy help meee......
Sanjana
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

hi kb !!

Post by sohel »

hello kb,

a line from the problem statement.

Code: Select all

If two characters are present the same time print the information of the ASCII character with higher ASCII value first.
meaning if the input is
AABB

output should be:
66 2
65 2


but your code outputs the other way round.

Changing your inner loop from ( j = 0 --> 255 ) to ( j = 255 --> 0 ) should solve this problem.

However, another line from problemstatement:

Code: Select all

A blank line should separate each set of output. 
You are not handling this properly. Print necessary newlines to avoid presentation error.

Hope it helps.
kolpobilashi
Learning poster
Posts: 54
Joined: Mon Jan 02, 2006 3:06 am
Location: Dhaka,Bangladesh
Contact:

Post by kolpobilashi »

thanx a lot BOSS Bro....i got AC (without PE) atlast!! :D
Sanjana
f.eliel
New poster
Posts: 10
Joined: Wed Feb 08, 2006 4:31 pm

Post by f.eliel »

I got WA too, i think its becouse of the '\r' '\n'... but i dont know what to do abaut it. Here is my code.

Code: Select all

#include <stdio.h>

struct freq {
   int code, tem;
} f[1010];

int compara(const *x, const *y)
{
   struct freq *n = (struct freq *)x;
   struct freq *m = (struct freq *)y;
   if (n->tem == m->tem) return (m->code - n->code);
   else return (n->tem - m->tem);
}


int main()
{
   int i, j, w, t, first = 1;
   char line[1010];
   
   while (scanf("%s", &line) == 1) {
      if (first) first = 0;
      else printf("\n");
      memset(f, 0, sizeof(f));
      for (i = 0; line[i] != '\0'; i++) {
         t = 0;
         w = line[i];
         for (j = 0; f[j].code != 0; j++) {
            if (w == f[j].code) {
               t = 1;
               f[j].tem++;
               j++;
               break;
            }
         }
         if (!t) {
            f[j].code = w;
            f[j].tem++;
            j++;
         }
      }
      qsort(f, j, sizeof(struct freq), compara);
      
      for (i = 0; i < j; i++) printf("%d %d\n", f[i].code, f[i].tem);
   }
   return 0;
}
bongssi
New poster
Posts: 14
Joined: Mon Jul 31, 2006 10:35 am

huu...

Post by bongssi »

I changed the size of array to 1010, but still get WA. Give me a suggestion or valid test case i/o...plz
tan_Yui
Experienced poster
Posts: 155
Joined: Sat Jul 10, 2004 12:41 am

Re: huu...

Post by tan_Yui »

bongssi wrote:I changed the size of array to 1010, but still get WA. Give me a suggestion or valid test case i/o...plz
Your code can read only first line of input data.
Input is terminated by end of file.

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

Post by tan_Yui »

Hi, f.eliel.
I changed your code several parts, and got Accepted.

First of all, the code should be included stdlib.h (for qsort) and string.h (for memset).

Then changed the main parts like following :

Code: Select all

int main() 
{ 
  int i, j, w, t, first = 1, cnt; 
  char line[1010]; 
    
  while (fgets(line, 1010, stdin) != NULL) { 
    cnt = 0;
    if (first) first = 0; 
    else printf("\n");
    memset(f, 0, sizeof(f)); 
    for (i = 0; line[i] != '\n'; i++) { 
      t = 0; 
      w = line[i]; 
      for (j = 0; j<cnt; j++) { 
        if (w == f[j].code) { 
          t = 1; 
          f[j].tem++; 
          break; 
        } 
      } 
      if (!t) { 
        f[j].code = w; 
        f[j].tem++; 
        cnt++; 
      } 
    } 
    qsort(f, cnt, sizeof(struct freq), (int (*)(const void*, const void*))compara); 
      
    for (i = 0; i < cnt; i++) printf("%d %d\n", f[i].code, f[i].tem); 
  } 
  return 0; 
}
Best regards.
Post Reply

Return to “Volume 100 (10000-10099)”