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

rahit
New poster
Posts: 1
Joined: Tue Dec 27, 2011 8:11 am

Re: 400 - Unix ls RA

Post by rahit »

I'm getting Runtime Error :-? . Plz help.....

Code: Select all


#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <vector>
#include <cstdio>
#include <vector>
#include <string.h>

using namespace std;

void qckSort(vector<string> &vec, int left, int right);
int getHighStringLength(vector<string> &vec);

int main() {
    int total_file;
    
    while(cin>>total_file){ 
	    string s;
    vector<string> fileNames;
        int i=0, j=0, k=0, l=0, distence=0, column=0, row=0;
        char c;
        
        //get file names
        for (i = 0; i < total_file; i++) {
            cin>>s;
            fileNames.push_back(s);
        }
       
        
        qckSort(fileNames, 0, (fileNames.size()-1));
        
             //print 60 "-"
	    for (i = 0; i < 60 ; i++) {
		    cout<<"-";
        }
        
        distence = getHighStringLength(fileNames);
        
	    
        column = 60/distence;
        row    = (total_file/column);
	    

	    cout<<endl;
	    
        for(i=0; i < row; i++){
            for(j=i; j<fileNames.size(); j=j+row){
                cout<<fileNames[j];
                l = fileNames[j].size();
                l = distence - l;
                for(k=0; k<l; k++)      {cout<<" ";}
            }
            cout<<endl;
        }
        
        total_file = 0;
        fileNames.clear();
    
    }
    return 0;
}


void qckSort(vector<string> &vec, int left, int right){
    int i = left;   int j = right;
    int pvt = (left+right)/2;
    do{
        while(vec[i]<vec[pvt])  i++;
        while(vec[pvt]<vec[j])  j--;
        if(i <= j){
            swap(vec[i],vec[j]);
            i++;
            j--;
        }
    }while(i<=j);
    
    if(i<right) qckSort(vec,i,right);
    if((left)<j) qckSort(vec,left,j);
 }



int getHighStringLength(vector<string> &vec){
    int i, temp_length,high_length;
    string s;
    for(i=0; i<vec.size(); i++){
        temp_length = vec[i].size();
        if(temp_length>high_length){
            high_length = temp_length ;
        }
    }
    return (high_length+2);
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 400 - Unix ls

Post by brianfry713 »

rahit, you're program prints over 60 columns for the third sample test case. You also include <vector> three times.
Check input and AC output for thousands of problems on uDebug!
Caren
New poster
Posts: 7
Joined: Thu Oct 25, 2012 11:42 pm

Re: 400 - Unix ls-PE

Post by Caren »

Please Help, this is the third problem that I am stuck on PE . I have posted others in respective threads. Why this code gives PE?. I have tried with trailing spaces up to maximum length for last column , then trailing spaces up to maximum available space and even with out trailing spaces!!! Now I cant find anything else. Please help. Thanks in advance

Code: Select all

Removed after AC . Codes were fine.There was an online judge problem . Now it's solved and all the solutions are accepted
asitmahato
New poster
Posts: 11
Joined: Mon Feb 20, 2012 11:04 am

Re: 400 - Unix ls

Post by asitmahato »

Code: Select all

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int num;
	string temp;
	vector<string>::iterator it;
	while(cin>>num)
	{
		vector<string> files;
		int maxSize=0;
		for(int i=0;i<num;i++)
		{
			cin>>temp;
			files.push_back(temp);
		}
		sort(files.begin(),files.end());
		for(it=files.begin();it!=files.end();it++)
			cout<<*it<<endl;
		for(int i=0;i<files.size();i++)
		{
			if(files[i].length()>maxSize)
				maxSize=files[i].length();
		}
		int col=60/(maxSize+2);
		int row =num/col;
		for(int i=1;i<61;i++)
			cout<<"-";
		cout<<endl;
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<col;j++)
			{
				if(j==0)
				{
					cout<<files[i];
					for(int idx=files[i].length();idx<=maxSize+2;idx++)
						cout<<" ";
				}
				else if(j==col-1)
				{
					cout<<files[i+row*j];
					cout<<endl;
				}
				else
				{
					cout<<files[i+row*j];
					for(int idx=files[i].length();idx<=maxSize+2;idx++)
						cout<<" ";
				}
			}
		}
		
	}
}
getting Runtime Error.
plz help me
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 400 - Unix ls

Post by brianfry713 »

Doesn't match the sample I/O.
Check input and AC output for thousands of problems on uDebug!
asitmahato
New poster
Posts: 11
Joined: Mon Feb 20, 2012 11:04 am

Re: 400 - Unix ls

Post by asitmahato »

Code: Select all

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
	int num;
	string temp;
	vector<string>::iterator it;
	while(scanf("%d",&num))
	{
		vector<string> files;
		int maxSize=0;
		for(int i=0;i<num;i++)
		{
			cin>>temp;
			files.push_back(temp);
			if((int)files[i].length()>maxSize)
				maxSize=files[i].length();
		}
		sort(files.begin(),files.end());
		//cout<<maxSize<<endl;
		int col=(60-maxSize)/(maxSize+2) +1;
		int row =num/col;
		if(num%row)
			row++;
			cout<<"------------------------------------------------------------";
		cout<<endl;
		for(int i=0;i<row;i++)
		{
			for(int j=0;j<col && i+row*j< (int)files.size();j++)
			{
				cout<<files[i+row*j];
				for(int idx=files[i+row*j].length();idx<maxSize;idx++)
					cout<<" ";
				if(j<col-1)
					cout<<" ";
			}
			cout<<endl;
		}
		
	}
	return 0;
}

getting time Limit ..help required.
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 400 - Unix ls

Post by brianfry713 »

This runs in an infinite loop on the sample input.
Check input and AC output for thousands of problems on uDebug!
udoy
New poster
Posts: 7
Joined: Sat Sep 28, 2013 11:27 am

unix is-400 .................wa......plz help

Post by udoy »

:cry:

Code: Select all

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define max 100000
#define valid(i,j,x,y) (i>=0 && i<x &&j<y && j>=0 )

using namespace std;

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

}
int main()
{
    int a,i,n,b,d,r,f,l,c;
    //  char str[max][160];
    string str[max];
    char st[max];
    memset(st,'-',60);
    for (; (scanf("%d",&n))==1; )
    {
        for (i=0; i<n ; i++)
        {
            cin>>str[i];
        }
        sort(str,str+n);
//        for (i=0; i<n ; i++)
//        {
//            cout<<str[i]<<endl;
//        }
        printf("%s\n",st);
        int     g,best=0;
        for (i=0; i<n ; i++)
        {
            if((str[i].length())>best)
                best=str[i].length();
      //      cout<<best<<" "<<str[i].length()<<endl;
        }
        int better=best;
        g=60-best;
        // cout<<g<<endl;
        best=1+g/(best+2);
//cout<<best<<endl;
        int v,j,row=n/best,col=best;
       // cout<<best<<endl;
if((double)row<(double)((double)n/(double)best))
    row++;
//cout<<row<<endl<<col<<" " <<endl;
            for (j=0;j<row ;j++ ){
        {
        for (i=0; i<col ; i++){
         if(i!=col-1)
         v=better+2;
         else v=better;

                printf("%-*s",v,str[i*row+j].c_str());
        }
        }
            printf("\n");
    }
    }
return 0;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: unix is-400 .................wa......plz help

Post by brianfry713 »

Don't print trailing spaces
Check input and AC output for thousands of problems on uDebug!
udoy
New poster
Posts: 7
Joined: Sat Sep 28, 2013 11:27 am

Re: unix is-400 .................wa......plz help

Post by udoy »

brother
i've optimized it like this but still wa !

Code: Select all

#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#define max 100000
#define valid(i,j,x,y) (i>=0 && i<x &&j<y && j>=0 )

using namespace std;

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

}
int main()
{
    int a,i,n,b,d,r,f,l,c;
    //  char str[max][160];
    string str[max];
    char st[max];
    memset(st,'-',60);
    for (; (scanf("%d",&n))==1; )
    {
        for (i=0; i<n ; i++)
        {
            cin>>str[i];
        }
        sort(str,str+n);
//        for (i=0; i<n ; i++)
//        {
//            cout<<str[i]<<endl;
//        }
        printf("%s\n",st);
        int     g,best=0;
        for (i=0; i<n ; i++)
        {
            if((str[i].length())>best)
                best=str[i].length();
            //      cout<<best<<" "<<str[i].length()<<endl;
        }
        int better=best;
        g=60-best;
        // cout<<g<<endl;
        best=1+g/(best+2);
//cout<<best<<endl;
        int v,j,row=n/best,col=best;
        // cout<<best<<endl;
        if((double)row<(double)((double)n/(double)best))
            row++;
//cout<<row<<endl<<col<<" " <<endl;
        for (j=0; j<row ; j++ )
        {
            {
                for (i=0; i<col ; i++)
                {
                    if(i!=col-1)
                    {
                        v=better+2;
                        printf("%-*s",v,str[i*row+j].c_str());
                    }
                    else
                        printf("%-s",str[i*row+j].c_str());

                }
            }
            printf("\n");
        }
    }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: unix is-400 .................wa......plz help

Post by brianfry713 »

Don't print trailing spaces on the last line of the sample output.
Check input and AC output for thousands of problems on uDebug!
udoy
New poster
Posts: 7
Joined: Sat Sep 28, 2013 11:27 am

Re: unix is-400 .................wa......plz help

Post by udoy »

ive edited else last loop like this......but still wa......u've been a great helper ......thanx in advance

Code: Select all

for (j=0; j<row ; j++ )
        {
            {
                for (i=0; i<col ; i++)
                {
                    if(i!=col-1)
                    {
                        v=better+2;
                   //   printf("%s",str[i*row+j].c_str());//previous code

                        printf("%-*s",v,str[i*row+j].c_str());
                    }
                    else
                        printf("%s",str[i*row+j].c_str());

                }
            }
            printf("\n");
        }
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: unix is-400 .................wa......plz help

Post by brianfry713 »

You're still printing trailing spaces on the last line of the sample output. Next time post your full updated code.
Check input and AC output for thousands of problems on uDebug!
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: ACed.

Post by lighted »

_.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

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
4
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgz
123456789112345678921234567893123456789412345678951234567896
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh
123456789112345678921234567893123456789412345678951234567896
9
tiny
2short4me
very_long_file_name
shorter
size-1
much_longer_name
12345678.123
mid_size_name
z
52
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
z
y
x
w
v
u
t
s
r
q
p
o
n
m
l
k
j
i
h
g
f
e
d
c
b
a
11
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
zz
5
koala.jpg
australia.exe
kangaroo.txt
foo.bar
homer.simpson
2
www_algorithmist_com
acm.uva.es
3
bernardo_acm_unix_ls_accepted
finally
no_more_p_e
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.......
------------------------------------------------------------
123456789112345678921234567893123456789412345678951234567896
123456789112345678921234567893123456789412345678951234567896
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgz
------------------------------------------------------------
12345678.123.........size-1.............
2short4me............tiny...............
mid_size_name........very_long_file_name
much_longer_name.....z..................
shorter..............
------------------------------------------------------------
a..b..d..e..g..h..j..k..m..n..p..q..s..t..v..w..y..z..
a..c..d..f..g..i..j..l..m..o..p..r..s..u..v..x..y..
b..c..e..f..h..i..k..l..n..o..q..r..t..u..w..x..z..
------------------------------------------------------------
12345678.123.........size2..............
2short4me............size3..............
mid_size_name........tiny...............
much_longer_name.....very_long_file_name
shorter..............zz.................
size-1...............
------------------------------------------------------------
australia.exe..homer.simpson..koala.jpg......
foo.bar........kangaroo.txt...
------------------------------------------------------------
acm.uva.es............www_algorithmist_com
------------------------------------------------------------
bernardo_acm_unix_ls_accepted..no_more_p_e..................
finally........................
I got accepted but my outputs doesn't match with outputs in this thread. Is my output wrong? :-?
My 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       
------------------------------------------------------------
123456789112345678921234567893123456789412345678951234567896
123456789112345678921234567893123456789412345678951234567896
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgh
abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefgz
------------------------------------------------------------
12345678.123         size-1               
2short4me            tiny                 
mid_size_name        very_long_file_name  
much_longer_name     z                    
shorter              
------------------------------------------------------------
a  b  d  e  g  h  j  k  m  n  p  q  s  t  v  w  y  z  
a  c  d  f  g  i  j  l  m  o  p  r  s  u  v  x  y  
b  c  e  f  h  i  k  l  n  o  q  r  t  u  w  x  z  
------------------------------------------------------------
12345678.123         size2                
2short4me            size3                
mid_size_name        tiny                 
much_longer_name     very_long_file_name  
shorter              zz                   
size-1               
------------------------------------------------------------
australia.exe  homer.simpson  koala.jpg      
foo.bar        kangaroo.txt   
------------------------------------------------------------
acm.uva.es            www_algorithmist_com  
------------------------------------------------------------
bernardo_acm_unix_ls_accepted  
finally                        
no_more_p_e                    
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 400 - Unix ls

Post by brianfry713 »

This problem has a special judge. My code gets AC without printing any trailing spaces. However, for input:

Code: Select all

3
bernardo_acm_unix_ls_accepted
finally
no_more_p_e
I get:

Code: Select all

------------------------------------------------------------
bernardo_acm_unix_ls_accepted  no_more_p_e
finally
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.
29 + 2 + 29 will fit in 60 characters.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 4 (400-499)”