https://www.acmicpc.net/problem/9996
문제
풀이
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));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine(), "*");
String head = st.nextToken();
String tail = st.nextToken();
String[] arr= new String[n];
for(int i = 0; i < n; i++){
arr[i] = br.readLine();
}
for (int i = 0; i < n; i++) {
String test = arr[i];
if(arr[i].length() >= head.length()+tail.length()){
if (test.substring(0, head.length()).equals(head)) {
if (test.substring(test.length() - tail.length(), test.length()).equals(tail)) {
System.out.println("DA");
} else {
System.out.println("NE");
}
} else {
System.out.println("NE");
}
}else{
System.out.println("NE");
}
}
}
}
문제 풀이에 있어서 간과한 부분은 주어진 테스트 문자열이 패턴의 head, tail의 길이의 합보다 작은 경우이다.
예제 출력에도 있었지만 이 부분을 생각하지 못해 런타임 에러가 났었다. 더욱 주의해야겠다.
'문제 풀이 > 백준' 카테고리의 다른 글
[백준]1620번. 나는야 포켓몬 마스터 이다솜(JAVA) (0) | 2024.04.04 |
---|---|
[백준]22233번. 제출(JAVA) (0) | 2024.04.03 |
[백준]20922번. 겹치는 건 싫어 (JAVA) (0) | 2024.03.30 |
[백준] 2559번. 수열 (JAVA) (0) | 2024.03.30 |
[백준] 21921번. 블로그 (JAVA) (0) | 2024.03.30 |