ํฌ์ŠคํŠธ

๐ŸŒ“ 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 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.