๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 15323_ZigZag

๋ฟŒ์•ผ._. 2025. 6. 27. 17:11
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/15323)

< ZigZag >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฐ ์•ŒํŒŒ๋ฒณ์— ํ•ด๋‹นํ•˜๋Š” ์šฐ์„ ์ˆ˜์œ„ ํ๋ฅผ ๋งŒ๋“ค์–ด ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค. ๊ฐ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‹จ์–ด๋“ค ์ค‘์—์„œ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ์™€ ์„ ํƒ ํšŸ์ˆ˜๋ฅผ ํŒ๋ณ„ํ•˜์—ฌ ์„ ํƒํ•œ๋‹ค. 

 

+ ๊ฐ ๋‹จ์–ด์˜ ์‹œ์ž‘ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์— ๊ตฌ๋ถ„ํ•˜์—ฌ ๋”ฐ๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ  ํ•œ ๋ฒˆ์— ์ €์žฅํ•  ๊ฒฝ์šฐ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class _15323_ { // ZigZag
	static class Word implements Comparable<Word> {
		private String w;
		private int count;

		public Word(String w, int count) {
			this.w = w;
			this.count = count;
		}

		@Override
		public int compareTo(Word o) {
			if (this.count == o.count) {
				return this.w.compareTo(o.w);
			}
			return this.count - o.count;
		}
	}

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int k = Integer.parseInt(st.nextToken());
		int n = Integer.parseInt(st.nextToken());

		HashMap<Character, PriorityQueue<Word>> map = new HashMap<>();
		for (int i = 0; i < 26; i++) {
			map.put((char) ('a' + i), new PriorityQueue<>());
		}

		for (int i = 0; i < k; i++) {
			String str = bf.readLine();
			map.get(str.charAt(0)).add(new Word(str, 0));
		}

		for (int i = 0; i < n; i++) {

			char x = bf.readLine().charAt(0);

			Word word = map.get(x).poll();
			bw.write(word.w + "\n");
			word.count += 1;
			map.get(x).add(word);
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
k, n : ๋‹จ์–ด, ๋ฌธ์ž ๊ฐœ์ˆ˜
map : HashMap <Character, PriorityQueue <Word>>
x : ๋ฌธ์ž

 

Word

๋‹จ์–ด์™€ ์„ ํƒ ํšŸ์ˆ˜๋ฅผ ๋ณ€์ˆ˜๋กœ ๊ฐ€์ง€๋ฉฐ ์„ ํƒ ํšŸ์ˆ˜๊ฐ€ ์ž‘์„์ˆ˜๋ก, ์„ ํƒ ํšŸ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.

 

Main

๋‹จ์–ด์™€ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๊ฐ ์•ŒํŒŒ๋ฒณ์— ๋”ฐ๋ผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด HashMap์— ์•ŒํŒŒ๋ฒณ์„ key ๊ฐ’์œผ๋กœ, PriorityQueue๋ฅผ value๋กœ ์ €์žฅํ•˜์—ฌ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๋‹จ์–ด ์ˆ˜๋งŒํผ ๋‹จ์–ด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์‹œ์ž‘ํ•˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์ €์žฅํ•œ๋‹ค. ๋ฌธ์ž ๊ฐœ์ˆ˜๋งŒํผ ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ map์—์„œ key๊ฐ’์œผ๋กœ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๋Š” ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ฐพ์•„ ์ฒซ ๋ฒˆ์งธ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค. ๊ทธ ํ›„ ์„ ํƒ ํšŸ์ˆ˜๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ ๋‹ค์‹œ ์šฐ์„ ์ˆœ์œ„ ํ์— ์ €์žฅํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 16300_H to O  (2) 2025.07.08
[Baekjoon] 26043_์‹๋‹น ๋ฉ”๋‰ด  (3) 2025.07.07
[Baekjoon] 13732_Falling Apples  (2) 2025.06.26
[Baekjoon] 4881_์ž๋ฆฌ์ˆ˜์˜ ์ œ๊ณฑ  (0) 2025.06.25
[Baekjoon] 19605_Cyclic Shifts  (1) 2025.06.24