Page 7 of 7

Posted: Mon Oct 13, 2014 7:20 pm
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.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 {
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++) {
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++) {
}
}
return lista;
}
}
``````
I've tried everything,, with/without trailing spaces, without the last /n, without the spaces of a line without words... help

### Re: 400 - Unix ls

Posted: Mon Oct 13, 2014 9:58 pm
Your code doesn't match the sample I/O. Are you printing null chars instead of spaces?

### Re: 400 - Unix ls

Posted: Tue Jan 31, 2017 5:06 am
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");
}
}
}
``````

### Re: 400 - Unix ls

Posted: Fri Mar 10, 2017 1:12 pm
I see on uhunt that you haven't got accepted yet. Your code is accepted code.