문제 풀이/백준

[백준]9996번. 한국이 그리울 땐 서버에 접속하지(JAVA)

27200 2024. 4. 2. 12:05

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의 길이의 합보다 작은 경우이다.
예제 출력에도 있었지만 이 부분을 생각하지 못해 런타임 에러가 났었다. 더욱 주의해야겠다.