๊ธฐ๋ก๋ฐฉ

BOJ_1235 : ํ•™์ƒ ๋ฒˆํ˜ธ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1235 : ํ•™์ƒ ๋ฒˆํ˜ธ

Soom_1n 2023. 11. 2. 22:22

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

1235๋ฒˆ: ํ•™์ƒ ๋ฒˆํ˜ธ

์ฒซ์งธ ์ค„์—๋Š” ํ•™์ƒ์˜ ์ˆ˜ N(2≤N≤1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ๊ฐ ํ•™์ƒ์˜ ํ•™์ƒ ๋ฒˆํ˜ธ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. ๋ชจ๋“  ํ•™์ƒ๋“ค์˜ ํ•™์ƒ ๋ฒˆํ˜ธ๋Š” ์„œ๋กœ ๋‹ค๋ฅด์ง€๋งŒ ๊ทธ ๊ธธ์ด๋Š” ๋ชจ๋‘ ๊ฐ™์œผ๋ฉฐ, 0๋ถ€

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ํ•™์ƒ๋ฒˆํ˜ธ๋ฅผ ์ค‘๋ณต๋˜์ง€ ์•Š๋„๋ก ๊ฐ€์žฅ ์งง๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • ๋ฒˆํ˜ธ๋ฅผ ์ค„์ผ๋•Œ๋Š” ์™ผ์ชฝ์ˆ˜ ๋ถ€ํ„ฐ ๋ฒ„๋ฆฐ๋‹ค.

๐Ÿ”ธ ๋ฌธ์ œ ํ’€์ด ๐Ÿ”ธ

  • ํ•™์ƒ ๋ฒˆํ˜ธ ๋ฌธ์ž์—ด์˜ ๋ ๋ถ€๋ถ„์—์„œ ํ•˜๋‚˜์”ฉ ๊ธธ์ด๋ฅผ ๋Š˜๋ ค๊ฐ€๋ฉฐ ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผํ•œ๋‹ค.
    • ๋ ๋ถ€๋ถ„์— ์žˆ์œผ๋ฉด ์ ‘๊ทผ์ด ์–ด๋ ค์šธ ๊ฒƒ ๊ฐ™์•„ ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด์„œ ์ €์žฅ ํ›„ ์ค‘๋ณต์„ ํ™•์ธํ–ˆ๋‹ค.
    • ์ค‘๋ณตํ™•์ธ์„ ์œ„ํ•ด ๋ฌธ์ž์—ด์„ 1๋ถ€ํ„ฐ N-1๊นŒ์ง€ ์ž˜๋ผ๊ฐ€๋ฉฐ Set์— ๋„ฃ๊ณ  ํ™•์ธํ–ˆ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;

public class Main {
	public static void main(String[] args) throws IOException {
		// Input
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int N = Integer.parseInt(br.readLine());

		String[] students = new String[N];

		for (int i = 0; i < N; i++) {
			StringBuilder sb = new StringBuilder(br.readLine());
			students[i] = sb.reverse().toString();
		}

		// Process
		int i = 0;
		int length = students[0].length() - 1;

		for (i = 0; i < length; i++) {
			boolean flag = true;
			Set<String> set = new HashSet<>();
			for (int j = 0; j < N; j++) {
				String s = students[j].substring(0, i + 1);
				if (set.contains(s)) {
					flag = false;
					break;
				}
				set.add(s);
			}
			if (flag)
				break;
		}

		// Output
		bw.write(Integer.toString(i + 1));
		bw.flush();
	}
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • StringBuilder์˜ reverse()๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ๋’ค์ง‘์–ด ์ €์žฅํ–ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์„ 1๋ถ€ํ„ฐ N-1 ํฌ๊ธฐ๊นŒ์ง€ ์ž˜๋ผ์„œ Set์— ๋„ฃ์–ด๋ณด๊ณ  ์ค‘๋ณต์„ ํ™•์ธํ•œ๋‹ค.
    • ๋ฌธ์ œ์—์„œ N๊ธธ์ด๋Š” ์ด๋ฏธ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฐ€์ •์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— N-1๊นŒ์ง€๋งŒ ํ™•์ธํ•œ๋‹ค. 
    • ์ค‘๋ณต๋˜๋ฉด ๋‹ค์Œ ๊ธธ์ด๋กœ ๋‹ค์‹œ ํ™•์ธํ•œ๋‹ค.
    • ์ „๋ถ€ ์ค‘๋ณต๋˜์ง€ ์•Š์œผ๋ฉด ํ˜„์žฌ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฌธ์ž์—ด๋“ค์„ ์ •๋ ฌํ•ด์„œ ๊ฐ™์€ ์ธ๋ฑ์Šค๋งŒ ํ™•์ธํ•˜๋ฉด ๋˜์ง€ ์•Š์„๊นŒ ์ƒ๊ฐํ–ˆ์ง€๋งŒ, ์ฒซ ๋ฒˆ์งธ ์ธ๋ฑ์Šค๋งŒ ํ•ด๋‹น๋˜๊ณ  ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋˜ ๊ณ„์‚ฐ์ด์—ˆ๋‹ค.
  • ๊ฒฐ๊ตญ substring๊ณผ Set์„ ํ†ตํ•ด ์ž˜๋ผ๊ฐ€๋ฉฐ ์ค‘๋ณตํ™•์ธ์œผ๋กœ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

728x90

'CodingTest > Java' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Lv.2 : ๋‰ด์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ง  (0) 2023.11.07
Lv.2 : ํ”„๋กœ์„ธ์Šค  (0) 2023.11.03
Lv.2 : ๊ธฐ๋Šฅ๊ฐœ๋ฐœ  (0) 2023.11.02
BOJ_1418 : K-์„ธ์ค€์ˆ˜  (0) 2023.11.01
Lv.2 : [1์ฐจ] ์บ์‹œ  (0) 2023.11.01