๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 4649_Tanning Salon

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

< Tanning Salon >

 

๋ฌธ์ œ ํ’€์ด 

 

HashSet์„ ์‚ฌ์šฉํ•˜์—ฌ ์–ผ๋งˆ๋‚˜ ๋งŽ์€ ์†๋‹˜์ด ํƒœ๋‹์„ ํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋Œ์•„๊ฐ”๋Š”์ง€ ๊ตฌํ•œ๋‹ค.

 

* ํƒœ๋‹์„ ๋ชป ํ•˜๊ณ  ๋– ๋‚œ ๊ณ ๊ฐ์ด๋ผ๋„, ๋ฌธ์ž์—ด์— ๋“ฑ์žฅํ•˜๋Š” ๋‘ ๋ฒˆ์งธ ๊ฐ™์€ ๊ธ€์ž๋Š” ํ‡ด์‹ค๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.

 

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.HashSet;
import java.util.StringTokenizer;

public class _4649_ { // Tanning Salon

	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;

		String str = "";

		while (!(str = bf.readLine()).equals("0")) {
			st = new StringTokenizer(str);

			int bed = Integer.parseInt(st.nextToken());
			String customer = st.nextToken();

			HashSet<Character> set = new HashSet<>();

			int result = 0;
			for (int i = 0; i < customer.length(); i++) {
				if (set.contains(customer.charAt(i))) {
					set.remove(customer.charAt(i));
					continue;
				} 
				if (set.size() >= bed) {
					result += 1;
				}
				set.add(customer.charAt(i));
			}
			if (result == 0) {
				bw.write("All customers tanned successfully.\n");
			} else {
				bw.write(result + " customer(s) walked away.\n");
			}
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
bed, customer : ํƒœ๋‹ ๋ฒ ๋“œ์˜ ๊ฐœ์ˆ˜, ๊ณ ๊ฐ ์ •๋ณด
set : ํƒœ๋‹ ๋ฒ ๋“œ ํ˜„ํ™ฉ
result : ๋Œ์•„๊ฐ„ ๊ณ ๊ฐ ์ˆ˜ 

 

0์ด ์ž…๋ ฅ๋˜๊ธฐ ์ „๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) ํƒœ๋‹ ๋ฒ ๋“œ์˜ ๊ฐœ์ˆ˜์™€ ๊ณ ๊ฐ ์ •๋ณด ์ž…๋ ฅ

2) ๊ณ ๊ฐ ์ •๋ณด๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ HashSet์— ์กด์žฌํ•œ๋‹ค๋ฉด ์ œ๊ฑฐ/ ์กด์žฌํ•˜์ง€ ์•Š๊ณ  ํ˜„์žฌ ํƒœ๋‹ ๋ฒ ๋“œ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„ ํƒœ๋‹ ๋ฒ ๋“œ ์ˆ˜ ์ด์ƒ์ด๋ผ๋ฉด result+1/ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด HashSet์— ์ถ”๊ฐ€

3) result ๊ฐ’์ด 0์ด๋ฉด 'All customers tanned successfully.'๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  0์ด ์•„๋‹ˆ๋ผ๋ฉด 'result + customer(s) walked away.'๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 



 

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

[Baekjoon] 10654_Cow Jog  (0) 2025.12.04
[Baekjoon] 13984_Contest Score  (0) 2025.12.02
[Baekjoon] 5006_Horror List  (0) 2025.11.19
[Baekjoon] 26111_Parentheses Tree  (0) 2025.11.18
[Baekjoon] 11254_Hydraulic Arm  (0) 2025.11.17