https://www.acmicpc.net/problem/4949
#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
string line;
stack<char> brackets;
vector<string> lines;
bool answer = true;
while (true)
{
getline(cin, line);
if (line == ".") break;
while (!brackets.empty()) brackets.pop();
answer = true;
for (char ch : line)
{
if (ch == '(' || ch == '[')
{
brackets.push(ch);
}
else if (ch == ')' || ch == ']')
{
if (brackets.empty())
{
answer = false;
break;
}
if (( brackets.top() == '(' && ch == ')' ) || ( brackets.top() == '[' && ch == ']' ))
{
brackets.pop();
}
else
{
answer = false;
}
}
}
if (answer && brackets.empty())
{
lines.emplace_back("yes");
}
else
{
lines.emplace_back("no");
}
}
for (string answer : lines)
{
cout << answer << endl;
}
}
'CS > 코딩테스트' 카테고리의 다른 글
백준 1141번: 접두사 - C++ (0) | 2025.03.25 |
---|---|
백준 2785번: 체인 - C++ 탐욕(Greedy)알고리즘 (0) | 2025.03.24 |
나머지 연산(%)의 분배법칙 (정리 필요) (0) | 2024.10.25 |