๋ฌธ์ (์ถ์ฒ: 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 ์ถ๋ ฅ, ์ค๋ณต๋ ์นด๋๊ฐ ์๋ค๋ฉด ๊ฐ ์นด๋ ์ ์ถ๋ ฅ
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1972_๋๋ผ์ด ๋ฌธ์์ด (0) | 2023.10.12 |
---|---|
[Baekjoon] 5568_์นด๋ ๋๊ธฐ (0) | 2023.10.11 |
[Baekjoon] 25192_์ธ์ฌ์ฑ ๋ฐ์ ๊ณฐ๊ณฐ์ด (0) | 2023.10.09 |
[Baekjoon] 16943_์ซ์ ์ฌ๋ฐฐ์น (0) | 2023.10.06 |
[Baekjoon] 2992_ํฌ๋ฉด์ ์์ ์ (1) | 2023.10.05 |