9012번 괄호문제를 풀고 넘어왔다면 수월한 문제.
각 괄호마다 처리해주면 쉽게 문제를 풀 수 있다.
여기서
string str;
getline(cin, str); 로 getline을 사용하는데, 한줄씩 받는다는 의미이다.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
#include <iostream>
#include <algorithm>
#include <stack>
#include <string>
using namespace std;
int main(void) {
cout.tie(NULL);
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int T;
stack <char> s;
while (1) {
string str;
getline(cin, str);
if (str == ".") break;
bool check = true;
for (int i = 0; i < str.length(); i++) {
char tmp = str[i];
if (tmp == '(' || tmp == '[') { s.push(tmp); }
else if (tmp == ']' || tmp == ')') {
if (!s.empty()) {
if (s.top() == '(' && tmp == ')') {
s.pop();
}
else if (s.top() == '[' && tmp == ']') {
s.pop();
}
else {
check = false;
break;
}
}
else { check = false; break; }
}
}
if (s.empty() && check) cout << "yes\n";
else cout << "no\n";
while (!s.empty()) s.pop();
}
return 0;
}
|
cs |