๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 27060_Vertical Histogram

๋ฟŒ์•ผ._. 2025. 7. 11. 10:40
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/27060)

< Vertical Histogram >

 

๋ฌธ์ œ ํ’€์ด 

 

1) ๊ฐ ๋ฌธ์ž์˜ ๋“ฑ์žฅ ํšŸ์ˆ˜ ๊ตฌํ•˜๊ธฐ

2) ํ–‰์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅ ํšŸ์ˆ˜ ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

3) ๋ถˆํ•„์š”ํ•œ ๋นˆ์นธ์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ ํ–‰์˜ ์—ด ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

 

my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class _270606_ { // Vertical Histogram

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

		int arr[] = new int[26];

		for (int i = 0; i < 4; i++) {
			String str = bf.readLine();

			for (int j = 0; j < str.length(); j++) {
				if (Character.isAlphabetic(str.charAt(j))) {
					arr[str.charAt(j) - 'A'] += 1;
				}
			}
		}

		int max = 0;
		for (int i = 0; i < 26; i++) {
			max = Math.max(max, arr[i]);
		}

		int position[] = new int[max];
		for (int i = 0; i < position.length; i++) {
			for (int j = 0; j < 26; j++) {
				if (arr[j] >= max) {
					position[i] = j;
				}
			}
			max -= 1;
		}

		max = position.length;
		for (int i = 0; i < position.length; i++) {
			for (int j = 0; j <= position[i]; j++) {
				if (arr[j] >= max) {
					bw.write("*");
					if(j != position[i]) {
						bw.write(" ");
					}
				} else {
					bw.write("  ");
				}
			}
			max-=1;
			bw.write("\n");
		}
		for (int i = 0; i < 26; i++) {
			bw.write((char) ('A' + i));
			if(i!=25) {
				bw.write(" ");
			}
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
arr : ์•ŒํŒŒ๋ฒณ ๋“ฑ์žฅ ํšŸ์ˆ˜
str : ๋ฌธ์ž์—ด
max : ์•ŒํŒŒ๋ฒณ ๋“ฑ์žฅ ํšŸ์ˆ˜ ์ค‘ ์ตœ๋Œ“๊ฐ’
position : ๊ฐ ํ–‰์˜ ์—ด ์ตœ๋Œ“๊ฐ’

 

4์ค„์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ์•ŒํŒŒ๋ฒณ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๋ฐฐ์—ด arr์— ์ €์žฅํ•œ๋‹ค. ํ–‰ ๊ฐ’์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด arr ๊ฐ’ ์ค‘์—์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค. ๋ถˆํ•„์š”ํ•œ ๋นˆ์นธ์„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ ํ–‰์˜ ์—ด ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•œ๋‹ค. 

 

ํ–‰ ๊ฐ’๋งŒํผ ๋ฐ˜๋ณตํ•˜์—ฌ ์—ด ์ตœ๋Œ“๊ฐ’๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋ฉฐ ํ•ด๋‹น๋˜๋Š” ์นธ์— "*"์„ ์ถœ๋ ฅํ•˜๊ณ  ํ•ด๋‹น๋˜์ง€ ์•Š๋Š” ์นธ์—๋Š” ๊ณต๋ฐฑ์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋งˆ์ง€๋ง‰ ์ค„์—๋Š” ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•œ๋‹ค. 



 

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

[Baekjoon] 9523_Arithmetic with Morse  (1) 2025.07.15
[Baekjoon] 14210_Kartomat  (0) 2025.07.14
[Baekjoon] 32076_Easy as ABC  (2) 2025.07.10
[Baekjoon] 3518_๊ณต๋ฐฑ์™• ๋นˆ-์นธ  (1) 2025.07.09
[Baekjoon] 16300_H to O  (2) 2025.07.08