문제
https://www.acmicpc.net/problem/9996
풀이(5분)
import java.io.*;
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());
String pattern = br.readLine();
String[] s = pattern.split("\\*");
String head = s[0];
String tail = s[1];
for(int i = 0; i < n; i++){
String input = br.readLine();
if(input.length() < head.length() + tail.length()){ // 길이가 짧을 때
System.out.println("NE");
continue;
}
if(input.substring(0,head.length()).equals(head)){ // 앞의 문자가 head와 일치할 때
if(input.substring(input.length() - tail.length(),input.length()).equals(tail)){ // 뒤의 문자가 tail과 일치할 때
System.out.println("DA"); // 정답 출력
continue;
}
}
System.out.println("NE"); // 아닌 경우 전부 NE 출력
}
}
}
문제 풀이 전략
문장을 입력받은 후 *를 기준으로 head와 tail로 나눈다.
1. 문장의 길이가 먼저 만족시킬 수 있는지 검사한다.
2. head와 동일한지 본다.
3. tail과 동일한지 본다.
4. 맞다면 DA를 그렇지 않은 경우엔 전부 NE를 출력한다.
'문제 풀이 > 백준' 카테고리의 다른 글
[백준] 1092번. 배(JAVA) (0) | 2025.03.28 |
---|---|
[백준] 1405번. 미친 로봇(JAVA) (0) | 2025.03.27 |
[백준] 2195번. 문자열 복사(JAVA) (0) | 2025.03.26 |
[백준] 2229번. 조 짜기(JAVA) (0) | 2025.03.25 |
[백준] 1253번. 좋다(JAVA) (0) | 2025.03.24 |