๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 16300_H to O

๋ฟŒ์•ผ._. 2025. 7. 8. 12:15
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/16300)

< H to O >

 

๋ฌธ์ œ ํ’€์ด 

 

HashMap์— ๊ฐ ์›์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ์ €์žฅํ•œ๋‹ค. ๋งŒ์•ฝ ์ž…๋ ฅ์ด C2H6 10์ด๋ผ๋ฉด HashMap์— C: 20, H: 60์œผ๋กœ ์ €์žฅํ•œ๋‹ค. ๊ทธ ํ›„ ๊ตฌํ•˜๋ ค๋Š” ๋ถ„์ž๋„ ๊ฐ ์›์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ์ €์žฅํ•œ๋‹ค. ๋งŒ์•ฝ C3H8 ์ด๋ผ๋ฉด HashMap์— C: 3, H: 8๋กœ ์ €์žฅํ•œ๋‹ค. 

 

C: 20, H:60์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” C: 3, H: 8 ๊ฐœ์ˆ˜๋Š” ์ด 6๊ฐœ๊ฐ€ ๋œ๋‹ค.

 

 

my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

public class _16300_ { // H to O

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

		String molecule = st.nextToken();
		int k = Integer.parseInt(st.nextToken());

		HashMap<Character, Integer> map = new HashMap<>();

		findMolecule(molecule, map, k);

		molecule = bf.readLine();
		HashMap<Character, Integer> map2 = new HashMap<>();
		findMolecule(molecule, map2, 1);

		int result = Integer.MAX_VALUE;
		ArrayList<Character> list = new ArrayList<>(map2.keySet());

		for (int i = 0; i < list.size(); i++) {
			if (!map.containsKey(list.get(i))) {
				result = 0;
				break;
			} else {
				if (map.get(list.get(i)) / map2.get(list.get(i)) == 0) {
					result = 0;
					break;
				}
				result = Math.min(result, map.get(list.get(i)) / map2.get(list.get(i)));
			}
		}

		System.out.println(result);
	}

	private static void findMolecule(String molecule, HashMap<Character, Integer> map, int k) {
		char temp = ' ';
		String num = "";
		for (int i = 0; i < molecule.length(); i++) {
			if (Character.isAlphabetic(molecule.charAt(i))) {
				if (temp != ' ') {
					if (num.equals("")) {
						num = "1";
					}
					if (map.containsKey(temp)) {
						map.replace(temp, map.get(temp) + (Integer.parseInt(num) * k));
					} else {
						map.put(temp, Integer.parseInt(num) * k);
					}
				}
				temp = molecule.charAt(i);
				num = "";
			} else {
				num += molecule.charAt(i);
			}
		}
		if (num.equals("")) {
			num = "1";
		}
		if (map.containsKey(temp)) {
			map.replace(temp, map.get(temp) + (Integer.parseInt(num) * k));
		} else {
			map.put(temp, Integer.parseInt(num) * k);
		}
	}
}
๋ณ€์ˆ˜)
molecule : ์ž…๋ ฅ๋ฐ›์€ ๋ถ„์ž
k : ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ถ„์ž ๊ฐœ์ˆ˜
map, map2 : ๊ฐ ๋ถ„์ž๋ฅผ ์ด๋ฃจ๋Š” ์›์ž ๊ฐœ์ˆ˜
result : ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐœ์ˆ˜
list : ๋งŒ๋“ค๋ ค๋Š” ๋ถ„์ž์˜ ์›์ž

 

Main

๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ถ„์ž์™€ ๋ถ„์ž ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. findMolecule์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ถ„์ž์˜ ์›์ž ๊ฐœ์ˆ˜๋ฅผ HashMap์— ์ €์žฅํ•œ๋‹ค. ๋งŒ๋“ค๋ ค๋Š” ๋ถ„์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ๋˜‘๊ฐ™์ด findMolecule์„ ํ˜ธ์ถœํ•˜์—ฌ ๋ถ„์ž์˜ ์›์ž ๊ฐœ์ˆ˜๋ฅผ HashMap์— ์ €์žฅํ•œ๋‹ค. ๊ฐ ์›์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

findMolecule

๋ถ„์ž๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ์•ŒํŒŒ๋ฒณ์ผ ๋•Œ์™€ ์ˆซ์ž์ผ ๋•Œ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ HashMap์— ๊ฐ ์›์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•œ๋‹ค.



 

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

[Baekjoon] 32076_Easy as ABC  (2) 2025.07.10
[Baekjoon] 3518_๊ณต๋ฐฑ์™• ๋นˆ-์นธ  (1) 2025.07.09
[Baekjoon] 26043_์‹๋‹น ๋ฉ”๋‰ด  (3) 2025.07.07
[Baekjoon] 15323_ZigZag  (1) 2025.06.27
[Baekjoon] 13732_Falling Apples  (2) 2025.06.26