4949๋ฒ: ๊ท ํ์กํ ์ธ์
ํ๋ ๋๋ ์ฌ๋ฌ์ค์ ๊ฑธ์ณ์ ๋ฌธ์์ด์ด ์ฃผ์ด์ง๋ค. ๊ฐ ๋ฌธ์์ด์ ์๋ฌธ ์ํ๋ฒณ, ๊ณต๋ฐฑ, ์๊ดํธ("( )") ๋๊ดํธ("[ ]")๋ฑ์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ, ๊ธธ์ด๋ 100๊ธ์๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค. ์ ๋ ฅ์ ์ข ๋ฃ์กฐ๊ฑด์ผ๋ก ๋งจ ๋ง๏ฟฝ๏ฟฝ
www.acmicpc.net
โจ ๋ด ์์ค ์ฝ๋
#include<iostream>
#include <stack>
#include <string>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
stack <char> st1;
while (1) {
getline(cin, s);
if (s == ".") {
break;
}
for (int i = 0; i < s.length(); i++) {
if (s[i] == '[') {
st1.push(s[i]);
}
else if (s[i] == '(') {
st1.push(s[i]);
}
else if (s[i] == ']') {
if (st1.empty()) {
cout << "no\n";
break;
}
else if(st1.top() == '[') {
st1.pop();
}
else {
cout << "no\n";
break;
}
}
else if (s[i] == ')') {
if (st1.empty()) {
cout << "no\n";
break;
}
else if(st1.top() == '(') {
st1.pop();
}
else {
cout << "no\n";
break;
}
}
//๋ฌธ์์ด ๋ง์ง๋ง๊น์ง ํ์์ ํ๋๋ฐ ์คํ์ด ๋น์ด์๋ ๊ฒฝ์ฐ
if ( i ==(s.length() -1) && st1.empty()) {
cout << "yes\n";
}
//๋ฌธ์์ด ๋ง์ง๋ง๊น์ง ํ์์ ํ๋๋ฐ ์คํ์ด ๋น์ด์์ง ์์ ๊ฒฝ์ฐ
else if (i == (s.length() - 1) && !st1.empty()) {
cout << "no\n";
}
}
while (!st1.empty()) st1.pop();
}
return 0;
}
๐ ๋ฌธ์ ํ์ด
๋ฌธ์์ด์ ํ๋์ฉ ํ์ํ๋ค๊ฐ '[' ๋๋ '(' ๊ฐ ๋์ค๋ฉด stack์ ์ง์ด ๋ฃ๋๋ค
๊ทธ ํ ')' ๋๋ ']' ์ ๋ฐ๊ฒฌํ๋ฉด stack์์ pop์ ํด๋ณธ๋ค
pop์ ํด์ ๋์จ ๊ฐ์ด ๋ฐ๊ฒฌํ ๋ซ๋ ๊ดํธ์ ์์ด ๋ง์ง ์์ผ๋ฉด 'no', stack์ด ๋น์ด์์ด๋ 'no'๋ฅผ ์ถ๋ ฅํ๋ค
๋ํ ๋ฌธ์์ด์ ๋ง์ง๋ง๊น์ง ํ์์ ํ๋๋ฐ stack์ด ๋น์ด์์ง ์์ ๊ฒฝ์ฐ๋ 'no'๋ฅผ ์ถ๋ ฅํ๋ค
๋ฌธ์์ด์ ๋ง์ง๋ง๊น์ง ํ์ํ๋๋ฐ stack์ด ๋น์ด์์ผ๋ฉด 'yes'๋ฅผ ์ถ๋ ฅํ๋ค
โญ ๋ฌธ์ ํ์ด ๊ฒฐ๊ณผ
