Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Comparator;
  6. import java.util.PriorityQueue;
  7.  
  8. public class Main {
  9.     /**
  10.      * Iterate through each line of input.
  11.      */
  12.     public static void main(String[] args) throws IOException {
  13.         InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
  14.         BufferedReader in = new BufferedReader(reader);
  15.  
  16.         int n = Integer.parseInt(in.readLine().trim());
  17.  
  18.         PriorityQueue<String> pq = new PriorityQueue<>(n, new Comparator<String>() {
  19.             @Override
  20.             public int compare(String o1, String o2) {
  21.                 if (o1.length() == o2.length()) {
  22.                     return 0;
  23.                 }
  24.  
  25.                 return (o1.length() < o2.length()) ? 1 : -1;
  26.             }
  27.         });
  28.  
  29.         String line;
  30.         while ((line = in.readLine()) != null) {
  31.             if (!line.isEmpty()) {
  32.                 pq.add(line.trim());
  33.             }
  34.         }
  35.  
  36.         for (int i=0; i<n; i++) {
  37.             System.out.println(pq.poll());
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement