UVa : 400 (Unix ls)



// http://uva.onlinejudge.org/external/4/400.html

Notes : 
1. each line of the output file will be <= 60 chars

2. to minimize number of rows, u should use as much column as possible. 

3. totalColumn = 62 / (maximum string length + 2)
totalRow = N / totalColumn

4. print the strings row by row
if its not the last column then, 
current string must contain (maximum string length + 2) chars
suppose, maximum string length = 10
current string length = 5
make the current string in a 12 length string by adding (12 - 5) = 7 spaces after it


// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
#include <bitset>
#include <utility>
#include <set>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define pi acos(-1.0)
#define N 1000000
using namespace std;

string a [100 + 5];

int main ()
{
    int n;

    while ( scanf ("%d", &n) != EOF ) {
        for ( int i = 0; i < n; i++ )
            cin >> a [i];

        sort (a, a + n);

        int maxLen = 0;

        for ( int i = 0; i < n; i++ ) {
            int len = a [i].length ();
            if ( len > maxLen ) maxLen = len;
        }

        int totalColumn = 62 / (maxLen + 2);
        int justified = maxLen + 2;
        int row = ceil (n / (double)totalColumn);

        printf ("------------------------------------------------------------\n");

        for ( int i = 0; i < row; i++ ) {
            for ( int j = i; j < n; j += row ) {
                cout << a [j];
                if ( j + row < n ) {
                    for ( int k = a [j].length (); k < justified; k++ )
                        printf (" ");
                }
            }
            printf ("\n");
        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE

2 thoughts on “UVa : 400 (Unix ls)

  1. @Tawsif khan
    I guess you missed the line,
    “… 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….”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s