728x90
반응형
2024.09.02기준 - 실버3
728x90
백준, BEAKJOON, BOJ, JAVA, 자바
풀이
이 문제는 주어진 조건에 맞는 염색채인지 아닌지 확인하여 출력하는 문제입니다.
문제 설명
더보기
1. A, B, C, D, E, F 중 0개 또는 1개가 나와야 하기 때문에 ABCDEF를 제외한 단어가 나오면 잘못된 염색체입니다.
2. AFC에서 연속된 AFC가 아닌 경우(ANFC, AFAFC) 조건 AFC전 까지 시작하는 단어로 해당되어 1번 조건에 맞지 않게 됩니다.
3. AFC 뒤에 단어는 무조건 1개이거나 없어야됩니다. 단 단어가 1개가 있다면 ABCDEF 중 1개만 나와야합니다.
1. 우선 풀이에 필요한 변수들을 정리했습니다.
char[] arr; // 문자열을 저장할 배열
char be, now; // 문자열을 비교할 때 전 문자와 현재 문자를 저장할 변수
String str; // 중복을 제거한 문자열을 저장할 변수
boolean check; // 조건에 맞는지 안맞는지 저장하는 변수
List<Integer> list; // 중복을 제거를 하지만 중복의 개수를 저장할 리스트
int index; // 리스트에 값을 추가, 변경 하기 위한 변수
2. 입력받은 문자열을 중복을 제거 해주면서 중복 개수를 저장을 해줍니다.
arr = br.readLine().toCharArray(); // 입력받은 문자열
str = "";
list = new LinkedList<>();
be = arr[0];
str += arr[0];
list.add(1);
index = 0;
// 중복을 제거 해주면서 중복 개수를 저장하는 반복문
for (int i = 1; i < arr.length; i++) {
now = arr[i];
if (be != now) { // 전과 다른 문자라면
str += arr[i]; // 문자를 추가
be = now; // 전 문자를 현재 문자로 변경
index++; // 다음 문자열로 중복 개수를 추가하기 위한 index +1
list.add(1); // 현재 문자 중복 개수
} else { // 전과 중복되는 문자열이라면
list.set(index, list.get(index) + 1);
}
}
3. AFC를 입력받을 수 없는 특수한 문자로 변경하여 AFC가 있는지 확인합니다.
// AFC를 특수한 문자로 변경
str = str.replaceAll("AFC", "!");
index = APC(str); // 문자열에 AFC가 존재하는지 확인.
// AFC가 문자열에 없다면
if (index == -1) {
sb.append("Good\n");
continue;
}
4. AFC를 !로 치환하면서 str 길이가 달라지므로 list 삭제해줍니다.
// AFC를 !로 치환하면서 str 길이가 달라지므로 list 삭제
list.remove(index + 2);
list.remove(index + 1);
5. AFC 앞에 있는 문자가 0개 또는 1개인지 확인 후 1개라면 조건에 맞는지 확인합니다.
// AFC 앞에 문자가 0 또는 1개만 있는지
check = index == 0 || index == 1 ? true : false;
if (!check) { // AFC 앞에 문자가 더 많다면
sb.append("Good\n");
continue;
}
// AFC 앞에 문자는 하나지만 ABCDEF가 아니거나 개수가 1개 보다 많다면
if (index == 1) {
if (!ABCDEF(str.charAt(0)) || list.get(0) > 1) {
sb.append("Good\n");
continue;
}
}
6. AFC 뒤 문자열도 동일하게 확인 후 출력합니다.
// AFC 뒤에 문자열이 0개 또는 1개라면 true 아니라면 false
check = index == str.length() - 1 || index == str.length() - 2 ? true : false;
if (!check) { // AFC 뒤에 문자열이 더 많다면
sb.append("Good\n");
continue;
}
// AFC 뒤에 문자가 하나만 있는데 ABCDEF가 아니거나 개수가 1개 보다 많다면
if (index == str.length() - 2) {
if (!ABCDEF(str.charAt(str.length() - 1)) || list.get(str.length() - 1) > 1) {
sb.append("Good\n");
continue;
}
}
// 규칙을 만족하는 염색체라면
sb.append("Infected!\n");
코드
package Main;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine()); // 테스트 케이스 개수
char[] arr; // 문자열을 저장할 배열
char be, now; // 문자열을 비교할 때 전 문자와 현재 문자를 저장할 변수
String str; // 중복을 제거한 문자열을 저장할 변수
boolean check; // 조건에 맞는지 안맞는지 저장하는 변수
List<Integer> list; // 중복을 제거를 하지만 중복의 개수를 저장할 리스트
int index; // 리스트에 값을 추가, 변경 하기 위한 변수
while (t-- > 0) {
arr = br.readLine().toCharArray(); // 입력받은 문자열
str = "";
list = new LinkedList<>();
be = arr[0];
str += arr[0];
list.add(1);
index = 0;
// 중복을 제거 해주면서 중복 개수를 저장하는 반복문
for (int i = 1; i < arr.length; i++) {
now = arr[i];
if (be != now) { // 전과 다른 문자라면
str += arr[i]; // 문자를 추가
be = now; // 전 문자를 현재 문자로 변경
index++; // 다음 문자열로 중복 개수를 추가하기 위한 index +1
list.add(1); // 현재 문자 중복 개수
} else { // 전과 중복되는 문자열이라면
list.set(index, list.get(index) + 1);
}
}
// AFC를 특수한 문자로 변경
str = str.replaceAll("AFC", "!");
index = APC(str); // 문자열에 AFC가 존재하는지 확인.
// AFC가 문자열에 없다면
if (index == -1) {
sb.append("Good\n");
continue;
}
// AFC를 !로 치환하면서 str 길이가 달라지므로 list 삭제
list.remove(index + 2);
list.remove(index + 1);
// AFC 앞에 문자가 0 또는 1개만 있는지
check = index == 0 || index == 1 ? true : false;
if (!check) { // AFC 앞에 문자가 더 많다면
sb.append("Good\n");
continue;
}
// AFC 앞에 문자는 하나지만 ABCDEF가 아니거나 개수가 1개 보다 많다면
if (index == 1) {
if (!ABCDEF(str.charAt(0)) || list.get(0) > 1) {
sb.append("Good\n");
continue;
}
}
// AFC 뒤에 문자열이 0개 또는 1개라면 true 아니라면 false
check = index == str.length() - 1 || index == str.length() - 2 ? true : false;
if (!check) { // AFC 뒤에 문자열이 더 많다면
sb.append("Good\n");
continue;
}
// AFC 뒤에 문자가 하나만 있는데 ABCDEF가 아니거나 개수가 1개 보다 많다면
if (index == str.length() - 2) {
if (!ABCDEF(str.charAt(str.length() - 1)) || list.get(str.length() - 1) > 1) {
sb.append("Good\n");
continue;
}
}
// 규칙을 만족하는 염색체라면
sb.append("Infected!\n");
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
// ABCDEF에 속하는지 확인하는 함수.
private static boolean ABCDEF(char c) {
return c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F';
}
// AFC가 있는지 확인하는 함수.
private static int APC(String s) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '!') { // AFC가 존재한다면
return i;
}
}
return -1;
}
}
728x90
반응형
'코딩테스트 일기 (BAEKJOON)' 카테고리의 다른 글
BEAKJOON / 백준 - JAVA 1497번 기타콘서트 (3) | 2024.09.04 |
---|---|
BEAKJOON / 백준 - JAVA 30620번 서로소 싫어 (0) | 2024.09.03 |
BEAKJOON / 백준 - JAVA 17484번 진우의 달 여행 (Small) (6) | 2024.09.01 |
BEAKJOON / 백준 - JAVA 28463번 Toe Jumps (0) | 2024.08.31 |
BEAKJOON / 백준 - JAVA 14494번 다이나믹이 뭐예요? (0) | 2024.08.30 |