๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9324_์ง„์งœ ๋ฉ”์‹œ์ง€

๋ฟŒ์•ผ._. 2024. 5. 28. 22:47

Silver IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/9324)

< ์ง„์งœ ๋ฉ”์‹œ์ง€ >

 

๋ฌธ์ œ ํ’€์ด 

 

HashMap์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž๊ฐ€ ๋“ฑ์žฅํ•˜๋Š” ์ˆ˜๋ฅผ ํ™•์ธํ•œ๋‹ค.

 

 

 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.HashMap;

public class _9324_ { // ์ง„์งœ ๋ฉ”์‹œ์ง€

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int t = Integer.parseInt(bf.readLine());

		HashMap<Character, Integer> map = new HashMap<>();
		for (int i = 0; i < t; i++) {
			String str = bf.readLine();

			int idx = 0;
			boolean flag = false;
			while (idx < str.length()) {
				if (map.containsKey(str.charAt(idx))) {
					map.replace(str.charAt(idx), map.get(str.charAt(idx)) + 1);
					if (map.get(str.charAt(idx)) % 3 == 0) {
						if (idx + 1 >= str.length() || str.charAt(idx + 1) != str.charAt(idx)) {
							flag = true;
							break;
						}else {
							idx += 2;
						}
					} else {
						idx += 1;
					}
				} else {
					map.put(str.charAt(idx), 1);
					idx += 1;
				}
			}
			if (flag) {
				bw.write("FAKE\n");
			} else {
				bw.write("OK\n");
			}
			map.clear();
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
t : ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๊ฐœ์ˆ˜
map : ๊ฐ ๋ฌธ์ž ๊ฐœ์ˆ˜
str : ๋ฌธ์ž์—ด
idx : ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๋Š” ๋ฌธ์ž์—ด ์ธ๋ฑ์Šค
flag : ๋ฉ”์‹œ์ง€ ํŒ๋ณ„ ์—ฌ๋ถ€

 

ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ˆ˜๋งŒํผ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

2) ๋ฌธ์ž์—ด์„ ํƒ์ƒ‰

2-1) HashMap์— ์ด๋ฏธ ์žˆ๋Š” ๋ฌธ์ž๋ผ๋ฉด -> value+1

๋งŒ์•ฝ value ๊ฐ’์ด 3์˜ ๋ฐฐ์ˆ˜๋ผ๋ฉด ๋‹ค์Œ ๋ฌธ์ž๋„ ํ˜„์žฌ ๋ฌธ์ž์™€ ๊ฐ™์€ ๋ฌธ์ž์—ฌ์•ผ ํ•จ -> ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ์ข…๋ฃŒ, ๊ฐ™๋‹ค๋ฉด idx+2

๋งŒ์•ฝ value ๊ฐ’์ด 3์˜ ๋ฐฐ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด idx+1

2-2) HashMap์— ์žˆ๋Š” ๋ฌธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด put

3) flag ๊ฐ’์— ๋”ฐ๋ผ ์ •๋‹ต ์ถœ๋ ฅ