400 - Unix ls

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

kurnia w
New poster
Posts: 18
Joined: Fri Dec 06, 2002 3:53 pm
Location: Indonesia
Contact:

Post by kurnia w »

Hi...deddy, post your source code here...maybe i can check what wrong with your source code... :D
"Those who cannot remember the past are condemned to repeat it"
deddy one
Experienced poster
Posts: 120
Joined: Tue Nov 12, 2002 7:36 pm

Post by deddy one »

thx for your help kurnia, got Accepted now.
Tango
New poster
Posts: 1
Joined: Thu Mar 11, 2004 4:59 am

400 WA... @@a

Post by Tango »

please help me....

I got WA so many times ... :cry:

the following is my code:

[c]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int num, maxlen, cols, rows;
int size[100];
char fname[100][100];

void clean()
{
int i, j;

num = maxlen = cols = rows = 0;

for(i = 0 ; i < 100 ; i++)
{
for(j = 0 ; j < 100 ; j++)
fname[j] = '\0';
size = 0;
}
}

void print_blank(char str[])
{
int len = strlen(str);
int b_len = maxlen + 2 - len;
int i;

for(i = 0 ; i < b_len ; i++)
printf(" ");
}

void print()
{
int i, j;

for(i = 0 ; i < 60 ; i++)
printf("-");
printf("\n");

if(maxlen + 2 > 60)
cols = 1;
else
cols = 60 / (maxlen + 2);
rows = num / cols;
if(num % cols != 0)
rows += 1;

for(i = 0 ; i < rows ; i++)
{
for(j = 0 ; j < cols ; j++)
{
printf("%s", fname[i + rows * j]);
if(i == rows - 1 && j == (num % cols) - 1)
break;
if(j != cols - 1)
print_blank(fname[i + rows * j]);
}
printf("\n");
}
}

void fsize()
{
int i, tmp;

maxlen = 0;

for(i = 0 ; i < num ; i++)
{
tmp = strlen(fname);
size = tmp;
if(tmp > maxlen)
maxlen = tmp;
}
}

void sort()
{
int i, j;
char tmp[100];

for(i = 0 ; i < num - 1 ; i++)
{
for(j = i + 1 ; j < num ; j++)
{
if(strcmp(fname, fname[j]) > 0)
{
strcpy(tmp, fname[j]);
strcpy(fname[j], fname);
strcpy(fname, tmp);
}
}
}
}

int main()
{
int i;
char tmp[100];

while(fgets(tmp, sizeof(tmp), stdin) != NULL)
{
num = atoi(tmp);
for(i = 0 ; i < num ; i++)
{
fgets(tmp, sizeof(tmp), stdin);
strncpy(fname, tmp, strlen(tmp) - 1);
}
sort();
fsize();
print();
clean();
}

return 0;
}
[/c]
Raiyan Kamal
Experienced poster
Posts: 106
Joined: Thu Jan 29, 2004 12:07 pm
Location: Bangladesh
Contact:

Re: 400 WA... @@a

Post by Raiyan Kamal »

I think you should revise the print() function you've made. In the part where you are calculating the number of collumns and rows seems little strange to me because I did not do it in this way. May this is the problem, may be not. Would you please explain why you are doing this ?
[c]
if(maxlen + 2 > 60)
cols = 1;
else
cols = 60 / (maxlen + 2);

rows = num / cols;
[/c]

By the way, you can use memset() to clear the array instead of the clean() function you used
awbush
New poster
Posts: 1
Joined: Thu Jun 10, 2004 9:34 pm

400 is unclear

Post by awbush »

Problem 400 is unclear:
http://acm.uva.es/p/v4/400.html

The last sentence of the second paragraph says "Your program should use as few rows (R) as possible with rows being filled to capacity from left to right."

But in the output section it says "The sorted filenames 1 to R will be listed down column 1; filenames R+1 to 2R listed down column 2; etc." which suggests that columns should be filled to capacity and not rows.

I am aware of a solution posted at:
http://online-judge.uva.es/board/viewto ... hlight=400
but even it is only accepted with presentation error.

So which is it? Fill to capacity the rows or the columns?


Lets create a test case where columns > rows:
size=10, max_length=13 which implies 4 columns
Filling rows to capacity we have:

Code: Select all

1aaaaaaaaaaaa  4              7              9            
2              5              8              10           
3              6              
Filling columns to capacity we have:

Code: Select all

1aaaaaaaaaaaa  4              7              10           
2              5              8              
3              6              9              

Now a test case where columns < rows:
size=10, max_length=14 which implies 3 columns
Filling rowss to capacity we have:

Code: Select all

1aaaaaaaaaaaaa  5               8             
2               6               9             
3               7               10            
4               
Filling columns to capacity we have:

Code: Select all

1aaaaaaaaaaaaa  5               9             
2               6               10            
3               7               
4               8               

Perfect square test case (columns > rows or columns == rows):
size=16, max_length=10 which implies 5 columns
Filling rows to capacity we have:

Code: Select all

1aaaaaaaaa  5           8           11          14        
2           6           9           12          15        
3           7           10          13          16        
4
Filling columns to capacity we have:

Code: Select all

1aaaaaaaaa  5           9           13          ??????????
2           6           10          14          ??????????
3           7           11          15          ??????????
4           8           12          16          ??????????
                                                ^
what happened to the 5th row? ------------------|
In the perfect square test case the fifth row could be eliminated if filling columns to capacity but that should be counted wrong according the to problem statement: "There will be as many columns as will fit in 60 characters." And since the last column is to be max_length and the others max_length+2 we have 1+(60-max_length)/(max_length+2) many columns; in this case 5. I suppose a good question then would be "does an empty column still count as a column?"

It is clear that "filling rows to capacity" and "filling columns to capacity" are two different things. It seems like the correct answer is to fill rows to capacity. Maybe the problem description could be corrected?
kasturi
New poster
Posts: 3
Joined: Sat Jan 24, 2004 5:05 am

have problems with 400 the UNIX ls one..

Post by kasturi »

i tried all the input cases.. had gone thru the previous messages. but still get WA. i dont know why? could anyone please figure out the problem. the code is as follows:
[cpp]
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
int num; /* num indicates the number of names */
while(cin>>num)
{
char str[num][61];
unsigned int max_len=0;
for(int k=0;k<60;k++)
cout<<"-";
if(num==0)
continue;
for(int i=0;i<num;i++)
{
cin>>str;
// cout<<"\n"<<str;
int dum=strlen(str);
str[dum+1]='\0';
if(max_len<strlen(str))
max_len=strlen(str);
}
int num_r;
int num_c=(60-max_len)/(max_len+2)+1;
if(num_c!=0)
num_r=(num%num_c==0)? (num/num_c):(num/num_c+1);
else
num_c=1;
int count=0;
/* sorting */
for(int i=0;i<num;i++)
for(int j=i+1;j<num;j++)
if(strcmp(str[j],str)<0)
{
char k[60];
strcpy(k,str);
strcpy(str,str[j]);
strcpy(str[j],k);
}

/* printing in the order */
for(int i=0;i<num_r && count<num;i++)
{
// printf("\n%-*s",max_len,str);
// count++;
printf("\n");
for(int j=0;j<num_c&&count<num;j++)
{
if(j==num_c-1)
printf("%-*s",max_len,str[i+j*(num_r)]);
else
printf("%-*s",max_len+2,str[i+j*(num_r)]);
count++;
}
}
cout<<"\n";
}
}
[/cpp]

I also have another doubt. do we need to print a line for a case when input is 0 number of filenames? i tried both the cases .. one in which the output line was printed and the other in which it was not.. but there was problem with both.i got WA about 5 times. please help me. :oops:.
Life is like that
JaviGS
New poster
Posts: 6
Joined: Thu Aug 05, 2004 5:24 pm
Location: Spain

400 - Unix ls - PE

Post by JaviGS »

Hi!
I got PE in this problem and can't fix it!
I would be very appreciate if someone can find the bug in my code
Thanks in advance!
Regards
Javi

[cpp]

Code: Select all

Code removed
[/cpp]
Last edited by JaviGS on Sat Aug 14, 2004 2:19 pm, edited 1 time in total.
Observer
Guru
Posts: 570
Joined: Sat May 10, 2003 4:20 am
Location: Hong Kong

Re: 400 - Unix ls - PE

Post by Observer »

There are many other posts about this issue. Please check them before posting a new topic.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
HYri
New poster
Posts: 5
Joined: Thu Oct 21, 2004 6:00 pm

400 unix ls P.E!!!

Post by HYri »

400. unix ls

i think my answer is right.

but, always P.E

do you know any tip for 400?

how do i make a correct answer?
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

In 24 hr judge, PE is as good as AC, but incase you are wondering why they occur, here is the reason:

If you have extra spaces and newlines in your code, you will get PE. Since formatting of Output for this problem is more difficult than others, this problem is more prone to get PE, as extra spaces go unnoticed.
HYri
New poster
Posts: 5
Joined: Thu Oct 21, 2004 6:00 pm

then, ?

Post by HYri »

then how can i fix my program?

i'll show you input & output

input

Code: Select all

10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben
5
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghik
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghid
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghix
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghib
output

Code: Select all

------------------------------------------------------------
12345678.123         size-1             
2short4me            size2              
mid_size_name        size3              
much_longer_name     tiny               
shorter              very_long_file_name
------------------------------------------------------------
Alfalfa        Cotton         Joe            Porky        
Buckwheat      Darla          Mrs_Crabapple  Stimey       
Butch          Froggy         P.D.           Weaser       
------------------------------------------------------------
Alice       Chris       Jan         Marsha      Ruben     
Bobby       Cindy       Jody        Mike        Shirley   
Buffy       Danny       Keith       Mr._French  Sissy     
Carol       Greg        Lori        Peter     
------------------------------------------------------------
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghib
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghid
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghik
abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghix

i really really want to get accept without P.E
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim »

It is difficult to tell, but perhpas you have extra spaces at the end of each line of output.

e.g
[c]
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
[/c]

there could be some extra spaces in your output after size-1, Which should not be there.
Well this is just a suggestion. :roll:
WR
Experienced poster
Posts: 145
Joined: Thu Nov 27, 2003 9:46 am

Post by WR »

I verified that there are no additional spaces but it's still P. E.! I even added spaces so that all lines within a block are equally long, doesn't work, still P. E.

After more than 10 submissions just to get rid of the presentation error I quit.
Sakib
New poster
Posts: 24
Joined: Fri Jan 28, 2005 5:27 pm
Location: Bangladesh

Runtime Error (SIGFPE) in 400??????

Post by Sakib »

i just want to know what is Runtime Error (SIGFPE)??
/* Sorry For Nothing */
..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. »

You are doing something like this:
a = b / 0;
or
a = b % 0;
where b is an integer.

Read this:
http://online-judge.uva.es/board/viewforum.php?f=31
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.
Post Reply

Return to “Volume 4 (400-499)”