https://www.acmicpc.net/problem/9996
9996번: 한국이 그리울 땐 서버에 접속하지
총 N개의 줄에 걸쳐서, 입력으로 주어진 i번째 파일 이름이 패턴과 일치하면 "DA", 일치하지 않으면 "NE"를 출력한다. 참고로, "DA"는 크로아티어어로 "YES"를, "NE"는 "NO"를 의미한다.
www.acmicpc.net

백준 정답률이 낮을 때는 의심을 하고 풀었는데도 반례가 있었다..
역시나가 역시다.. 질문 게시판을 참고했다.
반례
1
abab*abab
ababab
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int cnt = Integer.parseInt(bf.readLine());
String standard = bf.readLine();
String first = "";
String second = "";
//기준점 * 앞뒤로 잘라주기!
for (int i = 0; i < cnt; i++) {
String str = bf.readLine();
for (int j = 0; j < standard.length(); j++) {
if (standard.charAt(j) == '*') {
first = standard.substring(0, j);
second = standard.substring(j + 1, standard.length());
}
}
boolean bFirstOk = false;
boolean bSecondOk = false;
for (int j = 0, k = str.length() - 1, h = second.length() - 1; j < str.length(); j++, k--, h--) {
if (j < first.length()) {
if (first.charAt(j) != str.charAt(j)) {
break;
}
} else {
bFirstOk = true;
}
if (j < second.length()) {
if (second.charAt(h) != str.charAt(k)) {
break;
}
} else {
bSecondOk = true;
}
}
if (bFirstOk && bSecondOk && !(first.length() + second.length() > str.length())) {
System.out.println("DA");
} else {
System.out.println("NE");
}
}
}
}
'Java' 카테고리의 다른 글
| [백준] 2993번 : 세 부분 (0) | 2023.03.07 |
|---|---|
| [백준] 11478번 : 서로 다른 부분 문자열의 개수 (0) | 2023.03.02 |
| [백준] 11899번 : 괄호 끼워넣기 (0) | 2023.02.28 |
| 추상클래스와 인터페이스 차이 (1) | 2023.02.15 |
| 자바 연습하기(일기) (0) | 2023.01.26 |