[문제]
Parenthesis String (PS) consists of two parenthesis symbols ‘(’ and ‘)’ only. In parenthesis strings, some strings are called a valid PS (shortly, VPS). Let us give the formal definition of VPS. A single “( )” is a member of VPS, called the base VPS. Let x and y be a member of VPS. Then “(x)”, a VPS which encloses a VPS x with a single pair of parenthesis, is also a member of VPS. And xy, the concatenation of two VPS x and y, is a member of VPS. For example, “(())()” and ((()))” are all VPS, but “(()(”, “(())()))” and “(()” are not VPS. You are given a set of PS. You should decide if the input string is VPS or not.
[입력]
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Then PS’s are given in the following T lines one by one. The length of each PS is between 2 and 50, inclusively.
[출력]
Your program is to write to standard output. Print the result in each line. If the input string is a VPS, then print “YES”. Otherwise print “NO”.
몇 번이나 틀린 끝에 결국엔 성공한 코드.
중요한 부분은 char parenthesis[]의 방 크기를 50이 아닌 51(null문자까지 생각을 해야한다)로 하는 것이 좋다는 것.
또한, for문으로 for(int j=0; j<strlen(bracket); j++) 로 할 수도 있겠지만,
while문으로 while(parenthesis[j]) 것이 더 좋을 수 있다는 것을 기억하자.
출처1: www.acmicpc.net/problem/9012
출처2: ICPC > Regionals > Asia Pacific > Korea > Nationwide Internet Competition > Daejeon Nationalwide Internet Competition 2012 G번
'코딩 문제풀이 및 연습 > [C언어]백준' 카테고리의 다른 글
[백준 6749번]Next in line (0) | 2021.03.17 |
---|---|
[백준 2675번]문자열 반복 (0) | 2021.03.16 |
[백준 2795번] Transactions (0) | 2020.09.05 |
[백준 2753번] 윤년 (0) | 2020.09.05 |
[백준 2752번] 세 수 정렬 (0) | 2020.09.05 |