๋ฌธ์ (์ถ์ฒ: 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 |