๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11507_์นด๋“œ์…‹ํŠธ

๋ฟŒ์•ผ._. 2023. 10. 10. 09:18

Silver IV

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

< ์นด๋“œ์…‹ํŠธ >

 

๋ฌธ์ œ ํ’€์ด 

 

Set์„ ํ™œ์šฉํ•˜์—ฌ ์ค‘๋ณต๋œ ์นด๋“œ์ธ์ง€ ํŒ๋‹จํ•œ๋‹ค. ์ค‘๋ณต๋œ ์นด๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๊ทธ๋ฆผ์ด ์ผ์น˜ํ•˜๋Š” ์นด๋“œ ๊ฐœ์ˆ˜๋ฅผ ํ•˜๋‚˜ ์ฐจ๊ฐํ•œ๋‹ค.

 

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

public class _11507_ { // ์นด๋“œ์…‹ํŠธ

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

		String str = bf.readLine();

		int idx = 0;
		Set<String> set = new HashSet<>();
		int p = 13, k = 13, h = 13, t = 13;
		boolean flag = false;

		while (idx < str.length()) {
			String temp = str.substring(idx, idx + 3);
			if (set.contains(temp)) {
				flag = true;
				break;
			} else {
				set.add(temp);
				if (temp.charAt(0) == 'P')
					p -= 1;
				else if (temp.charAt(0) == 'K')
					k -= 1;
				else if (temp.charAt(0) == 'H')
					h -= 1;
				else if (temp.charAt(0) == 'T')
					t -= 1;
			}
			idx += 3;
		}
		if(flag) {
			System.out.println("GRESKA");
		}else {
			bw.write(p + " " + k + " " + h + " " + t);
			bw.flush();
		}
	}

 

Main

๋ณ€์ˆ˜)
str : ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด
idx : ๋ฌธ์ž์—ด ์ธ๋ฑ์Šค
set : ์ค‘๋ณต ์นด๋“œ ํŒ๋‹จํ•˜๊ธฐ ์œ„ํ•œ set 
p, k, h, t : ๊ฐ ์นด๋“œ ์ˆ˜
flag : ์ค‘๋ณต ์—ฌ๋ถ€

 

- ๋ฌธ์ž์—ด ์ž…๋ ฅ(str)

- 3์ž๋ฆฌ์”ฉ ๋Š์–ด ์ฝ์œผ๋ฉฐ set์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธ -> ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ์ค‘๋ณต์ด๋ฏ€๋กœ ์ข…๋ฃŒ/ ํฌํ•จ๋˜์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด set์— ์ถ”๊ฐ€ ๋ฐ ๊ทธ๋ฆผ์— ํ•ด๋‹นํ•˜๋Š” ์นด๋“œ ์ˆ˜ ํ•˜๋‚˜ ์ฐจ๊ฐ -> ๋‹ค์Œ ์นด๋“œ๋ฅผ ์ฝ๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค 3 ์ฆ๊ฐ€

- ์ค‘๋ณต๋œ ์นด๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด GRESKA  ์ถœ๋ ฅ, ์ค‘๋ณต๋œ ์นด๋“œ๊ฐ€ ์—†๋‹ค๋ฉด ๊ฐ ์นด๋“œ ์ˆ˜ ์ถœ๋ ฅ