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

Wei-Ming Chen
Experienced poster
Posts: 122
Joined: Sun Nov 13, 2005 10:25 am
Location: Taiwan

Post by Wei-Ming Chen » Mon Feb 27, 2006 6:50 am

code cut after AC

<note>
if you want to let a run from 0 to b or c
you can't write for(a=0;a<=b,a<=c;a++)
you should write if(b>c) b=c; for(a=0;a<=b;a++)

I did not notice that and got two WAs (ACM 200 , 400)
Last edited by Wei-Ming Chen on Mon May 29, 2006 4:55 pm, edited 1 time in total.

User avatar
Roby
Experienced poster
Posts: 101
Joined: Wed May 04, 2005 4:33 pm
Location: Tangerang, Banten, Indonesia
Contact:

Post by Roby » Tue May 23, 2006 10:20 am

I got Runtime Error for this problem and I don't know where my mistake is. Here is my code:

Code: Select all

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

#define MAX 100
#define SIZE 65

int compare( const void * a, const void * b )
{
 return ( strcmp( ( char * ) a, ( char * ) b ) );
}

int main()
{
 int n = 0, i = 0, j = 0, r = 0, len = 0, div = 0;
 char data[MAX][SIZE];

 while ( scanf( "%d\n", &n ) == 1 )
 {
    for ( i = 0; i < n; i++ )
    {
       memset( data[i], '\0', sizeof( data[i] ) );
       gets( data[i] );
    }

    qsort( ( void * ) data, n, sizeof( data[0] ), compare );
    puts( "------------------------------------------------------------" );

    for ( i = 0, len = 0; i < n; i++ )
       if ( strlen( data[i] ) > len )
          len = strlen( data[i] );

    div = 60 / ( len + 2 );
    r = ( n % div ) ? ( ( n / div ) + 1 ) : ( n / div );

    for ( i = 0; i < r; i++ )
    {
       for ( j = 0; j < div; j++ )
       {
          int space;

          if ( i + ( j * r ) < n )
          {
             space = len - strlen( data[ i + ( j * r ) ] ) + 2;

             printf( "%s", data[ i + ( j * r ) ] );

             while ( space )
             {
                putchar( 32 );
                space--;
             }
          }
       }

       putchar( '\n' );
    }
 }

 return 0;
}
Somebody help me please... thanx

ausiva
New poster
Posts: 6
Joined: Sun Mar 12, 2006 7:01 pm

Post by ausiva » Mon May 29, 2006 8:39 pm

how did u get rid of the PE...i cud not no matter wat i do...ths s my code..pls let me knw if u see something missing..thanks

Code: Select all

int main()
{
    vector<string> strings;
    string temp;
    vector<string>::const_iterator iter;
    unsigned num=0;
    unsigned count=0;
    unsigned maxLength = 0;
    unsigned cols=0;
    unsigned rows=0;
    unsigned wid=0;
    int i=0;
    int j=0;

    while(cin>>num)
    {
                   getline(cin,temp);//to read the '\n' after the number
                   maxLength=rows=cols=0;
                   for(count=0;count<num;++count)
                   {
                    getline(cin,temp);
                    maxLength = max(maxLength, unsigned(temp.length()));
                    strings.push_back(temp);
                    }
                    sort(strings.begin(),strings.end());
                    //cout<<maxLength<<endl;
                    //assert(maxLength<=60);
                    cols=(60-maxLength)/(maxLength+2) + 1;
                    rows= (num%cols)?(num/cols+1):(num/cols);
                    
                    i=j=0;
                    //cout<<maxLength<<"\t"<<rows<<"\t"<<cols;
                    //draw a line of dashes
                    while(i++<60)
                                 cout<<'-';
                    for(i=0;i<rows;i++)
                    {
                           cout<<endl;
                           for(j=0;j<cols;j++)
                               if((i+j*rows)<num)
                               {
                                    cout.setf(ios::left,ios::adjustfield);
                                    wid=(j==cols-1)?(maxLength):maxLength+2;
                                    cout<<setw(wid)<<strings[i+j*rows];
                               }
                    }
                    cout<<endl;
                    strings.clear();
   }

   return 0;
}

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon » Sat Jul 22, 2006 4:14 pm

Whenever I get PE, I just leave it alone. What is the point in wasting time?

Anupam Pathak
New poster
Posts: 7
Joined: Tue Jul 25, 2006 2:01 pm
Contact:

400 P.E.

Post by Anupam Pathak » Fri Aug 04, 2006 4:17 pm

hello all,
i have solved 400, but it is accepted with presentation error.... can anyone tell me what's the problem the problem.......

Please tell me..... what's wrong........
thanx.....
Last edited by Anupam Pathak on Sat Aug 05, 2006 5:31 pm, edited 1 time in total.
Trying to reduce complexity of the World.

User avatar
emotional blind
A great helper
Posts: 383
Joined: Mon Oct 18, 2004 8:25 am
Location: Bangladesh
Contact:

Post by emotional blind » Fri Aug 04, 2006 7:30 pm

http://acm.uva.es/cgi-bin/OnlineJudge?ProblemStat:400

look here
there is a lot more presentation error than accepted
sometimes judges output format is not accurate enough.

presentation error considered solved here,
so better remove your code from here

kogorman
New poster
Posts: 14
Joined: Sat Dec 02, 2006 6:58 am

Indeed, what a tedious problem

Post by kogorman » Tue Dec 05, 2006 7:16 pm

It appears that the judge's answers do not even follow the problem specification, and one has to tease out hints from the trailing (incorrect) whitespace of the HTML sample output.

To be clear: I'm not claiming that trailing whitespace is incorrect per se, but that it does not actually conform to the problem specification about the width of the final column. Moreover, any short final line violates column widths in a different way.

What a nuisance!
Kevin

daveon
Experienced poster
Posts: 229
Joined: Tue Aug 31, 2004 2:41 am
Location: TORONTO, CANADA

Post by daveon » Sat Dec 23, 2006 9:10 pm

daveon wrote:Whenever I get PE, I just leave it alone. What is the point in wasting time?
Oh no, the judging system just got more aggressive. Fire away until you get AC!

hi!man!
New poster
Posts: 48
Joined: Fri Dec 29, 2006 1:26 pm

Post by hi!man! » Sun Jan 07, 2007 12:57 pm

this input

Code: Select all

3 
bernardo_acm_unix_ls_accepted 
finally 
no_more_p_e 
output should be:

Code: Select all

------------------------------------------------------------
bernardo_acm_unix_ls_accepted..
finally........................
no_more_p_e....................

zooom
New poster
Posts: 4
Joined: Tue Jun 12, 2007 9:20 am

Post by zooom » Mon Jan 07, 2008 7:33 am

@ above .. Noo The problem says the output must span the minimum amount of rows .. In your case its 3 while output with 2 rows too is possible ..

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 400 > Unix ls > P.E.

Post by Obaida » Sun Aug 09, 2009 9:01 am

I can't understand the error. :oops:
seems very easy. some one please help me to figure out what is wrong with my code?

Code: Select all

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<ctype.h>
using namespace std;

int sort_function(const void *a, const void *b)  {

   return( strcmp((char *)a,(char *)b) );

}

int main()
{
	char st[101][61];
	int n,i,j,max,r,c,k,l;
	while(scanf("%d",&n)==1){
		max=0;
		for(i=0;i<n;i++){
			while(1){gets(st[i]);if(st[i][0]!=0)break;}
			j = strlen(st[i]);
			if(j>max)max=j;
			for(;j<61;j++){st[i][j]=' ';break;}
		}
		qsort( (void *)st,n,sizeof(st[0]),sort_function);
		c = 60/max;
		while(c*max+(c-1)*2>60&&c>=1)c--;
		if(c){
			if(n%c==0)r = n/c;
			else r = n/c+1;
		}
		else r = n;
		printf("------------------------------------------------------------\n");
		for(i=0;i<r;i++){
			for(j=0;j<c;j++){
				for(k=0;st[i+j*r][k];k++){
					if(i+j*r<n){
						if(isspace(st[i+j*r][k])){
							for(l=k;l<max;l++)printf(" ");
							if(j==c-1)puts("");
							else printf("  ");
							break;
						}
						else
							printf("%c",st[i+j*r][k]);
					}
					else {puts("");break;}
				}
				if(i+j*r>=n)break;
			}
		}
	}
	return 0;
}
try_try_try_try_&&&_try@try.com
This may be the address of success.

sefakilic
New poster
Posts: 7
Joined: Wed Mar 11, 2009 8:12 pm

Re: 400 - Unix ls

Post by sefakilic » Tue Aug 25, 2009 3:19 pm

_.B._ wrote:FINALLY!!

ACed :D

It's such a tedious problem.
Thanks Daveon!
Here's some I/O from my ACed program that I believe will help correct P.E.s and W.A.s:


Input:

Code: Select all

5
koala.jpg
australia.exe
kangaroo.txt
foo.bar
homer.simpson

Output:

Code: Select all

------------------------------------------------------------
australia.exe..homer.simpson..koala.jpg......
foo.bar........kangaroo.txt...
Hello, I am trying to solve this problem, but getting WA.
I have a question about test case above.

In problem statement, it says
The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters.
In the output of ACed solution above, number of columns is 3.

But,

Code: Select all

length of "koala.jpg" = 9
length of "australia.exe" = 13
length of "kangaroo.txt" = 12
length of "foo.bar" = 7
length of "homer.simpson" = 13
So, the width of longest file name is 13.

(the width of longest file name + 2) * (number of columns - 1) + (the width of longest file name)
= (13 + 2) * (4 - 1) + 13 = 58 which is smaller than 60.

So, i think number of columns should be 4.

Can you help. thank you.

zobayer
Experienced poster
Posts: 110
Joined: Tue May 06, 2008 2:18 pm
Location: CSE-DU, Bangladesh
Contact:

Re: 400 - Unix ls

Post by zobayer » Sun Sep 06, 2009 1:24 pm

Code: Select all

AC
Thanks.
Last edited by zobayer on Sat Jul 31, 2010 8:57 pm, edited 1 time in total.
You should not always say what you know, but you should always know what you say.

arifcsecu
Learning poster
Posts: 64
Joined: Fri Sep 25, 2009 11:29 am
Location: Chittagong,University of chittagong
Contact:

Re: 400 - Unix ls

Post by arifcsecu » Sat Oct 10, 2009 10:12 pm

After many wrong answer
I got accepted

My approach is very simple :

1. input the string like getline(cin,s)
2. calculate the length of the maximum string
3.sort the string like sort(s,s+n)
4.calculate
ncoloum=(60-max_len)/(max_len+1) +1
nrow=(n/ncoloum)
if(n%ncoloum)
nrow++;

5.1. print (----.....---) 6o dash

5.2. now
two nested loop like

for(irow=0;irow<nrow;irow++)
for(icol=0;icol<ncoloum;icol++)
{
p=nrow*icol+irow;
if(p<n)
{
print the coressponding string as desired order loke s[p]
for(i=s[p].length();i<max_lenth;i++)
printf(" ");
}
}
6. Accepted now
Try to catch fish rather than asking for some fishes.

Ahshua
New poster
Posts: 7
Joined: Fri Jul 02, 2010 3:05 pm

Re: 400 - Unix ls

Post by Ahshua » Sun Jul 11, 2010 2:02 pm

Thank for your tests,I had found out my programming's deficiency,daveon !
NOw,I got Ac! :lol:
At " by daveon » Sat Aug 20, 2005 4:45 am "

Post Reply

Return to “Volume 4 (400-499)”