๐ Baekjoon 4949 - '๊ท ํ์กํ ์ธ์'
๐ซ ๋ฌธ์
๐ซง Example Input/Output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// IN
So when I die (the [first] I will see in (heaven) is a score list).
[ first in ] ( first out ).
Half Moon tonight (At least it is better than no Moon at all].
A rope may form )( a trail in a maze.
Help( I[m being held prisoner in a fortune cookie factory)].
([ (([( [ ] ) ( ) (( ))] )) ]).
. // ์ํ๋ฒณ, ๊ณต๋ฐฑ, ์๊ดํธ, ๋๊ดํธ, ๋ง์ง๋ง ์จ์ (1 <= ๊ธธ์ด <= 100)
. // ์
๋ ฅ ๋
// OUT
yes // ๋ฌธ์์ด ๋ด ๊ดํธ๊ฐ ๊ท ํ์กํ๋๊ฐ?
yes
no
no
no
yes
yes
๐ซ C++ ํ์ด
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
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
while (true)
{
string s;
getline(cin, s);
if (s == ".")
break;
stack<char> st;
bool isBalanced = true;
for (auto c : s)
{
if ((c == '(') || (c == '['))
{
st.push(c);
}
else if ((c == ')') || (c == ']'))
{
if (st.empty() || (abs(c - st.top()) > 2))
{
isBalanced = false;
break;
}
st.pop();
}
}
if (st.empty() == false)
isBalanced = false;
cout << (isBalanced ? "yes" : "no") << '\n';
}
}
๐ซ Memo
)
์]
์ ๋ ฅ๋ฐ๊ณ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ ๋ชจ์์ด ๋น์ทํด์ ์ด๋ป๊ฒ ํฉ์น ์ ์์๊น ๊ณ ๋ฏผ- ์์คํค์ฝ๋ ํ๋ฅผ ๋ณด๊ณ
(
์)
,[
์]
์ ์ฐจ์ด๊ฐ ๊ฐ๊ฐ 1, 2์ธ ๊ฒ์ ๋ณด๊ณ ,diff <= 2
๋ก ๊ดํธ์ ์ฌ๋ถ์ ํ๋จ
- ์์คํค์ฝ๋ ํ๋ฅผ ๋ณด๊ณ
์ด ๊ธฐ์ฌ๋ ์ ์๊ถ์์ CC BY 4.0 ๋ผ์ด์ผ์ค๋ฅผ ๋ฐ๋ฆ
๋๋ค.