๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 9996_ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

๋ฟŒ์•ผ._. 2024. 8. 28. 14:13
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/9996)

< ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ •๊ทœ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Pattern;

public class _9996_ { // ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int N = Integer.parseInt(bf.readLine());

		String str = bf.readLine();
		String pattern = "^";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) != '*') {
				pattern += str.charAt(i);
			} else {
				pattern += "[a-z]*";
			}
		}
		pattern += "$";

		for (int i = 0; i < N; i++) {
			String name = bf.readLine();
			if (Pattern.matches(pattern, name)) {
				bw.write("DA\n");
			} else {
				bw.write("NE\n");
			}
		}
		bw.flush();
	}
}

 

๋ณ€์ˆ˜)
N : ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜
str : ํŒจํ„ด
pattern : ํŒจํ„ด -> ์ •๊ทœ ํ‘œํ˜„์‹ ๋ณ€ํ™˜
name : ํŒŒ์ผ ์ด๋ฆ„

 

ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜ N์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ํŒจํ„ด์„ ์ž…๋ ฅ๋ฐ›์•„ ํŒจํ„ด์„ ์ •๊ทœ ํ‘œํ˜„์‹์œผ๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ๊ธฐ์กด ํŒจํ„ด์—์„œ *๋ฅผ [a-z]*๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค. ํŒŒ์ผ์˜ ๊ฐœ์ˆ˜๋งŒํผ ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ž…๋ ฅ๋ฐ›์•„ ์ •๊ทœ ํ‘œํ˜„์‹๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 2671_์ž ์ˆ˜ํ•จ์‹๋ณ„  (0) 2024.08.30
[Baekjoon] 1013_Contact  (0) 2024.08.29
[Baekjoon] 9342_์—ผ์ƒ‰์ฒด  (0) 2024.08.27
[Baekjoon] 11068_ํšŒ๋ฌธ์ธ ์ˆ˜  (0) 2024.08.26
[Baekjoon] 2238_๊ฒฝ๋งค  (0) 2024.08.09