๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 16652_Email Destruction

๋ฟŒ์•ผ._. 2025. 12. 9. 10:20
๋ฌธ์ œ(์ถœ์ฒ˜: 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