Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.charset.StandardCharsets;
- import java.util.Comparator;
- import java.util.PriorityQueue;
- public class Main {
- /**
- * Iterate through each line of input.
- */
- public static void main(String[] args) throws IOException {
- InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8);
- BufferedReader in = new BufferedReader(reader);
- int n = Integer.parseInt(in.readLine().trim());
- PriorityQueue<String> pq = new PriorityQueue<>(n, new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- if (o1.length() == o2.length()) {
- return 0;
- }
- return (o1.length() < o2.length()) ? 1 : -1;
- }
- });
- String line;
- while ((line = in.readLine()) != null) {
- if (!line.isEmpty()) {
- pq.add(line.trim());
- }
- }
- for (int i=0; i<n; i++) {
- System.out.println(pq.poll());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement