์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step12] 1181 : ๋‹จ์–ด ์ •๋ ฌ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 31. 16:45

www.acmicpc.net/problem/1181

 

1181๋ฒˆ: ๋‹จ์–ด ์ •๋ ฌ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 20,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 50์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net


๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ N๊ฐœ์˜ ๋‹จ์–ด๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

   1. ๊ธธ์ด๊ฐ€ ์งง์€ ๊ฒƒ๋ถ€ํ„ฐ

   2. ๊ธธ์ด๊ฐ€ ๊ฐ™์œผ๋ฉด ์‚ฌ์ „ ์ˆœ์œผ๋กœ

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 20,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 50์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

 

์ถœ๋ ฅ

์กฐ๊ฑด์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜์—ฌ ๋‹จ์–ด๋“ค์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ, ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๋ฒˆ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ์—๋Š” ํ•œ ๋ฒˆ์”ฉ๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1

13

but

i

wont

hesitate

no

more

no

more

it

cannot

wait

im

yours

 

์˜ˆ์ œ ์ถœ๋ ฅ 1

i

im

it

no

but

more

wait

wont

yours

cannot

hesitate


ํ’€์ด

์ด์ „ ๋ฌธ์ œ๋“ค๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ compare() ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•˜์—ฌ ์ •๋ ฌ

1. ๊ฐ String์˜ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ •๋ ฌ

2. ๊ธธ์ด๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ

 

import java.io.*;
import java.util.*;

public class B1181 {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int n = Integer.parseInt(br.readLine());
		String[] word = new String[n];

		for(int i = 0; i < n; i++) {
			word[i] = br.readLine();
		}
		
		Arrays.sort(word, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				if(o1.length() < o2.length())
					return -1;
				else if(o1.length() == o2.length())
					return o1.compareTo(o2);
				else
					return 1;
			}
		});
		
		for(int i = 0; i < n; i++) {
			if(i != 0 && word[i].equals(word[i-1]))
				continue;
			System.out.println(word[i]);
		}
	}
}