파게로그

[백준 9996번] 한국이 그리울 땐 서버에 접속하지 본문

콤퓨타 왕왕기초/PS

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

파게 2020. 11. 21. 07:24

문제 링크: 9996번 제목

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

 

파이썬은 어지간해서는 Exception을 발생시키지 않으니 가끔 엣지 케이스를 잡아내기 힘들다는 단점도 있다. 이 문제의 경우, 아래와 같은 코드를 사용할 경우, 길이 체크 부분의 코드가 없으면 "a*a" 패턴에 대해서 "a"라는 파일 이름에 대해서도 결과값을 DA로 내버린다는 함정이 있었다.

 

def compare_string(a, b): ​​​​if len(a) != len(b): ​​​​​​​​return False ​​​​for i in range(len(a)): ​​​​​​​​if a[i] != b[i]: ​​​​​​​​​​​​return False ​​​​return True n = int(input()) p = input() star = 0 for i in range(len(p)): ​​​​if p[i]=='*': ​​​​​​​​star = i left = p[:star] right = p[star+1:] answer = "" for _ in range(n): ​​​​case = input() ​​​​ ​​​​# 길이 체크 ​​​​if len(case) < len(left) + len(right): ​​​​​​​​answer += "NE\n" ​​​​​​​​continue ​​​​ ​​​​check_left = compare_string(left, case[:len(left)]) ​​​​if not check_left: ​​​​​​​​answer += "NE\n" ​​​​​​​​continue ​​​​ ​​​​check_right = compare_string(right, case[len(case)-len(right):]) ​​​​if not check_right: ​​​​​​​​answer += "NE\n" ​​​​​​​​continue ​​​​ ​​​​answer += "DA\n" print(answer)

 

Comments