๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/16652)
< Email Destruction >
๋ฌธ์ ํ์ด
์ ๋ ฅ๋ฐ์ ์ด๋ฉ์ผ ์ ๋ชฉ๋ง๋ค Re: ์ ๊ฐ์๋ฅผ ์ธ์ด ์ต๋๊ฐ์ ๊ตฌํ๋ค.
๋ง์ฝ Re: Re: Re: test ๋ผ๋ฉด Re:์ ๊ฐ์๊ฐ 3๊ฐ์ด๋ฏ๋ก ๊ณต๊ฒฉ ์ด์ ์ ์์ด์ผ ํ๋ ๋ฉ์ผ ์ ์๋ ๋ค์๊ณผ ๊ฐ์ด 4๊ฐ์ด๋ค.
test
Re: test
Re: Re: test
Re: Re: Re: test
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;
public class _16652_ { // Email Destruction
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
HashMap<String, Integer> map = new HashMap<>();
for (int i = 0; i < k; i++) {
st = new StringTokenizer(bf.readLine());
int cnt = 0;
while (st.hasMoreTokens()) {
String str = st.nextToken();
if (str.equals("Re:")) {
cnt += 1;
} else {
if (map.containsKey(str)) {
if (map.get(str) < cnt) {
map.replace(str, cnt);
}
} else {
map.put(str, cnt);
}
}
}
}
int result = 0;
for (String str : map.keySet()) {
result += (map.get(str) + 1);
}
if (result <= n) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
๋ณ์)
n, k : ๊ณต๊ฒฉ ์ด์ ์ถ์ธกํ ์ด๋ฉ์ผ ๊ฐ์, ํ์ฌ ์ด๋ฉ์ผ ๊ฐ์
map : HashMap <์ด๋ฉ์ผ ์ ๋ชฉ, Re ์ต๋ ๊ฐ์>
cnt : Re ๊ฐ์
result : ๊ณต๊ฒฉ ์ด์ ์์ด์ผ ํ๋ ๋ฉ์ผ์ ์
๊ณต๊ฒฉ ์ด์ ์ถ์ธกํ ์ด๋ฉ์ผ ๊ฐ์์ ํ์ฌ ์ด๋ฉ์ผ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ๋๋ค. ํ์ฌ ์ด๋ฉ์ผ ๊ฐ์๋งํผ ์ด๋ฉ์ผ ์ ๋ชฉ์ ์ ๋ ฅ๋ฐ์ HashMap์ Re ์ต๋๊ฐ์ ์ ์ฅํ๋ค. HashMap์ ํ์ํ๋ฉฐ result์ value+1์ ์ ์ฅํ๋ค.
์ต์ข result ๊ฐ์ด n์ดํ๋ผ๋ฉด "YES"๋ฅผ ์ถ๋ ฅํ๊ณ , n๋ณด๋ค ํฌ๋ค๋ฉด "NO"๋ฅผ ์ถ๋ ฅํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Baekjoon] 10331_Miscalculation (0) | 2025.12.10 |
|---|---|
| [Baekjoon] 7318_Parencodings (0) | 2025.12.08 |
| [Baekjoon] 33094_Diet Plan (0) | 2025.12.05 |
| [Baekjoon] 10654_Cow Jog (0) | 2025.12.04 |
| [Baekjoon] 13984_Contest Score (0) | 2025.12.02 |