https://www.acmicpc.net/problem/9996
코드설명
문자열 문제입니다.
문제로직입니다.
"*" 로 split된 맨 앞과 끝자리의 숫자가 비교문자열의 맨앞과 끝에 위치한다면 성공하는 문제입니다.
해당 로직을 적용하면 됩니다.
처음에 문제를 접했을때는 아래와 같은 형태의 정규표현식을 사용하려했습니다만,
이 문제에서는 일정한 규칙을 찾는 문제는 아니므로 자바에서는 정규표현식을 사용하여 해결할 수 없었습니다.
String regex = "^(100+1+|01)+$";
boolean flag = Pattern.matches(regex, str);
코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int N, M;
public static String[] standard;
public static int answer = 0;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
standard = new String[N];
standard = st.nextToken().split("\\*");
for(int i=0;i<N;i++) {
st = new StringTokenizer(br.readLine());
String temp = st.nextToken();
if(standard[0].length() + standard[1].length() > temp.length() ) {
System.out.println("NE");
continue;
}
String compareFront = temp.substring(0, standard[0].length());
String compareBack = temp.substring(temp.length() - standard[1].length() ,temp.length());
if(compareFront.equals(standard[0]) && compareBack.equals(standard[1])) {
System.out.println("DA");
}else {
System.out.println("NE");
}
}
}
}
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 222-풀링 17829 - 분할정복 JAVA (0) | 2023.10.01 |
---|---|
[백준] 2630 색종이 만들기 - 분할정복 JAVA (0) | 2023.09.30 |
[백준] 6987 월드컵 - 브루트포스(BruteForce) + 조합(Combination) JAVA (0) | 2023.09.27 |
[백준] 1194 달이 차오른다, 가자. - BFS + 비트마스크 JAVA (0) | 2023.09.26 |
[백준] 16571 알파 틱택토 - 백트래킹 + 게임이론 JAVA (0) | 2023.09.24 |