Q. https://www.acmicpc.net/problem/13022
13022번: 늑대와 올바른 단어
첫째 줄에 단어가 주어진다. 단어는 w, o, l, f로만 이루어져 있으며, 길이는 50을 넘지 않는다.
www.acmicpc.net
# 아이디어
1. HashSet을 활용하여 단어를 입력받으므로 중복되지 않고 'w', 'o', 'l', 'f' 만 존재해야 한다.
2. 단어 길이는 항상 4의 배수여야 한다.
3. 모든 단어의 개수는 동일해야 하며, 순서가 지켜져야 한다.
Sol1.
아이디어 3의 순서가 지켜져야 한다는 부분에 문제가 있는 것 같다...
하지만, 왜 틀린지 반례를 못 찾겠네..???
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.next();
char cp;
HashSet<Character> copy = new HashSet<>();
for (int i = 0; i < input.length(); i++) {
cp = input.charAt(i);
copy.add(cp);
}
boolean isWolf = false;
if (copy.size() == 4 && copy.contains('w') && copy.contains('o') && copy.contains('l') && copy.contains('f'))
isWolf = true;
if (input.length() % 4 != 0 || !isWolf) {
System.out.println(0);
System.exit(0);
}
int[] cnt = {0, 0, 0, 0};
char lastChar = '0';
int change = 1;
for (int i = 0; i < input.length(); i++) {
cp = input.charAt(i);
if(i==0 && cp!='w'){
System.out.println(0);
System.exit(0);
}
if (i >= 4 && change % 4 == 0) {
for (int j = 0; j < 3; j++) {
if (cnt[j] != cnt[j + 1]) {
System.out.println(0);
System.exit(0);
}
}
}
if (cp == 'w')
cnt[0]++;
else if (cp == 'o')
cnt[1]++;
else if (cp == 'l')
cnt[2]++;
else if (cp == 'f')
cnt[3]++;
if (i > 0 && cp != lastChar) {
change++;
}
lastChar = cp;
}
for (int i = 0; i < 3; i++) {
if (cnt[i] != cnt[i + 1]) {
System.out.println(0);
System.exit(0);
}
}
System.out.println(1);
sc.close();
}
}
# 블로그
1. 연속하여 w, o, l, f가 n개씩 나오는지 판별.
2. 올바른 단어가 여러개 반복되는지 판별.
반례) wolffwwoollf, wolfwwoollff, wwoollffwolf
'코테문제' 카테고리의 다른 글
[코테] 문자열 뒤집기 (0) | 2025.04.24 |
---|---|
[코테] 기본 CS 및 replace() 헷갈림 (0) | 2025.04.14 |
[백준] B1978 (0) | 2024.01.25 |
[백준] S25418 (0) | 2024.01.25 |
[백준] S2477 다시 풀기 (0) | 2024.01.05 |