일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Greedy
- 유니온파인드
- DP
- BOJ
- 백준
- 종만북
- stack
- 알고리즘문제해결전략
- 스택
- BFS
- 백트래킹
- 분할정복
- 그리디
- 분리집합
- 완전탐색
- union-find
- 너비우선탐색
- 동적계획법
- backtracking
- 문자열
- 알고스팟
- 재귀
- 세그먼트트리
- 누적합
- DFS
- 다이나믹프로그래밍
- Algospot
- priority_queue
- acm
- 이분탐색
- Today
- Total
DAMPER's 낙서장
[ALGOSPOT] Mismatched Brackets (BRACKETS2) 본문
출처 : algospot.com/judge/problem/read/BRACKETS2
algospot.com :: BRACKETS2
Mismatched Brackets 문제 정보 문제 Best White is a mathematics graduate student at T1 University. Recently, he finished writing a paper and he decided to polish it. As he started to read it from the beginning, he realized that some of the formulas ha
algospot.com
<문제>
Best White is a mathematics graduate student at T1 University. Recently, he finished writing a paper and he decided to polish it. As he started to read it from the beginning, he realized that some of the formulas have problems: some of the brackets are mismatched! Since there are so many formulas in his paper, he decided to check their validity with a computer program.
The following kinds of brackets are included in Best White’s paper.
- Round brackets are opened by a ( and closed by a ).
- Curly brackets are opened by a { and closed by a }.
- Square brackets are opened by a [ and closed by a ].
A formula is said well-matched when the following conditions are met:
- Every bracket has a corresponding pair. ( corresponds to ), [ corresponds to ], et cetera.
- Every bracket pair is opened first, and closed later.
- No two pairs "*cross*" each other. For example, [(]) is not well-matched.
Write a program to help Best White by checking if each of his formulas is well-matched. To make the problem easier, everything other than brackets are removed from the formulas.
<입력>
The first line of the input will contain the number of test cases C (1≤C≤100). Each test is given in a single line as a character string. The strings will only include characters in "[](){}" (quotes for clarity). The length of the string will not exceed 10,000.
<출력>
For each test case, print a single line "YES" when the formula is well-matched; print "NO" otherwise. (quotes for clarity)
<예제 입력>
3
()()
({[}])
({}[(){}])
<예제 출력>
YES
NO
YES
stack을 사용하는 전형적인 문제이다.
전체 string을 쭉 돌면서 여는 괄호들을 모두 stack에 넣고, 닫는 괄호들이 나올 때마다 stack의 top을 확인하면 되는 문제이다.
이러한 과정을 그림으로 그려 설명하면 이해가 빠르지만 나중에 추가하는 걸로..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#include <bits/stdc++.h>
using namespace std;
#define swap(a,b) (a)^=(b)^=(a)^=(b)
#define endl '\n'
typedef long long lld;
int main()
{
ios_base::sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
int tc;
cin>>tc;
while(tc--)
{
string s;
cin>>s;
stack<char> st;
bool flag = true;
int ans = 0;
for(size_t i=0;i<s.length();i++)
{
if(s[i]=='(' || s[i]=='[' || s[i]=='{') st.push(s[i]);
else if(st.empty()) flag = false;
else if(st.top() == '(' && s[i] == ')') st.pop();
else if(st.top() == '[' && s[i] == ']') st.pop();
else if(st.top() == '{' && s[i] == '}') st.pop();
else flag = false;
}
if(!st.empty()) flag = false;
if(!flag) cout<<"NO\n";
else cout<<"YES\n";
}
return 0;
}
|
cs |
'Problem Solving > 알고리즘 문제해결전략' 카테고리의 다른 글
[ALGOSPOT] 트리 순회 순서 변경 (TRAVERSAL) (0) | 2021.01.26 |
---|---|
[ALGOSPOT] 외계 신호 분석 (ITES) (0) | 2021.01.23 |
[ALGOSPOT] 조세푸스 문제 (JOSEPHUS) (0) | 2021.01.22 |
[ALGOSPOT] 두니발 박사의 탈옥 (NUMB3RS) (0) | 2021.01.22 |
[ALGOSPOT] 폴리오미노 (POLY) (0) | 2021.01.22 |