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

ankhia
New poster
Posts: 1
Joined: Mon Oct 13, 2014 7:02 pm

WA 400 - Unix ls.. Please Help

Post by ankhia »

Hi, I'm trying this excercise a lot, but I still gettin WA... can someone please help me?? :( :( :(

Code: Select all

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.TreeMap;


public class Main{

	static char matriz[][];
	static TreeMap<String, Integer> palabras;
	public static void main(String[] args) throws Throwable {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		for (String ln ; (ln = in.readLine())!= null;) {
			palabras = new TreeMap<String, Integer>();
			int N = Integer.parseInt(ln.trim());
			int maxTam = 0;
			//Preparing the in
			for (int i = 0; i < 60; i++) 
				sb.append("-");
			sb.append("\n");
			int cantPal = 0;
			//Only doing the in
			for (int i = 0; i < N; i++) {
				String palabra = in.readLine().trim();
				cantPal++;
				if( palabras.containsKey(palabra) ){
					palabras.put(palabra, palabras.get(palabra)+1);
				}else
					palabras.put(palabra, 1);
				maxTam = Math.max(palabra.length(), maxTam);
			}
			// Calculate Columns and Rows
			int c = (int) (Math.floor( (60.-maxTam) / (maxTam+2) ) + 1);
			int r = (int) Math.ceil( (double) cantPal / (double) c);
			matriz = new char[r][60];
			
			//Words sorted
			ArrayList<String> cosa = palabras(  );
			//Fill a matrix with the words character by character
			int y = 0;
			int w = 0;
			for (String p : cosa) {
				for (int j = y, s = 0; j < p.length()+y ; j++, s++) {
						matriz[w][j] = p.charAt(s);
				}
				w++;
				w = (w%r);
				if( w == 0 ) 
					y += maxTam+2;
			}
                        //Printing the matrix withouth trailing spaces
			int newC = (int) Math.ceil( (double)cantPal/ (double)r);
			for (int i = 0; i < matriz.length; i++) {
				int h = 0;
				for (int j = 0; j < newC; j++){
					int temp = h;
					for (int j2 = h; j2 < (j < newC-1?temp+maxTam+2:temp+maxTam ); j2++, h++) {
						if( j2 == temp && matriz[i][j2]=='\0') break;
						sb.append(matriz[i][j2]);
					}
				}
				sb.append("\n");
			}
		}
		System.out.print(new String(sb));
	}
	
	//Sort the words
	public static ArrayList<String> palabras(  ){
		ArrayList<String> lista = new ArrayList<String>();
		for( String k : palabras.keySet() ){
			for (int i = 0; i < palabras.get(k); i++) {
				lista.add(k);
			}
		}
		return lista;
	}
}
I've tried everything,, with/without trailing spaces, without the last /n, without the spaces of a line without words... help :( :(
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 400 - Unix ls

Post by brianfry713 »

Your code doesn't match the sample I/O. Are you printing null chars instead of spaces?
Check input and AC output for thousands of problems on uDebug!
datovard
New poster
Posts: 1
Joined: Tue Jan 31, 2017 4:30 am

Re: 400 - Unix ls

Post by datovard »

Really need help with this one, i've been trying it for 3 days and still get WA after testing all the test cases in the replies and getting them good

Code: Select all

#include <bits/stdc++.h>

using namespace std;

int main(){
    int n;
    string line;
    while( scanf("%d", &n ) != EOF  ){
        vector<string> lines;
        int longer = 0, columns = 0;
        for( int i = 0; i < n; i++ ){
            cin >> line;
            lines.push_back(line);
            if( line.size() >= longer ) longer = line.size();
        }
        do{
            columns++;
        }while( columns*(longer+2) <= (60-longer) );
        sort( lines.begin(), lines.end() );
        string press;
        int rows = ceil( (n+0.0)/(columns) ), pos;
        printf("------------------------------------------------------------\n");
        for( int i = 0; i < rows; i++ ){
            press = "";
            for( int j = 0; j < columns; j++ ){
                pos = (j*rows)+i;
				if( pos >= 0 && pos < lines.size() ){
            		press += lines[ pos ];
            		if( j < columns-1 )
                        press += "  " ;
                    for( int k = 0; k < ((longer)-lines[pos].length()); k++ ) press += " ";
				}
            }
            printf( press.c_str() );
            printf("\n");
        }
    }
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 400 - Unix ls

Post by lighted »

I see on uhunt that you haven't got accepted yet. Your code is accepted code. :D
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Post Reply

Return to “Volume 4 (400-499)”