https://www.acmicpc.net/problem/9996

 

9996번: 한국이 그리울 땐 서버에 접속하지

총 N개의 줄에 걸쳐서, 입력으로 주어진 i번째 파일 이름이 패턴과 일치하면 "DA", 일치하지 않으면 "NE"를 출력한다. 참고로, "DA"는 크로아티어어로 "YES"를, "NE"는 "NO"를 의미한다.

www.acmicpc.net

코드설명

문자열 문제입니다.

 

문제로직입니다.

"*" 로 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");
    		}
    	}
    }

}

+ Recent posts