728x90
반응형
https://www.acmicpc.net/problem/4949
4949번: 균형잡힌 세상
각 문자열은 마지막 글자를 제외하고 영문 알파벳, 공백, 소괄호("( )"), 대괄호("[ ]")로 이루어져 있으며, 온점(".")으로 끝나고, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마지막에
www.acmicpc.net
---------------------------------------------------핵심 알고리즘--------------------------------------------
stack
---------------------------------------------------풀이----------------------------------------------------
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
using namespace std;
int main(void){
FASTIO;
string input;
while(true){
getline(cin, input);
if(input == "."){
break;
}
stack<char> s;
bool yes_flag = true;
for(int i = 0; i < input.length(); i++){
if(input[i] == '(' || input[i] == '['){
s.push(input[i]);
}
else if(input[i] == ')'){
if(!s.empty() && s.top() == '('){
s.pop();
}
else{
yes_flag = false;
break;
}
}
else if(input[i] == ']'){
if(!s.empty() && s.top() == '['){
s.pop();
}
else{
yes_flag = false;
break;
}
}
}
if(!s.empty() || !yes_flag){
cout << "no\n";
}
else{
cout << "yes\n";
}
}
return 0;
}
---------------------------------------------------후기----------------------------------------------------
input 받는 형식때문에 기록한다.
cin >> str; 형태로 입력받으면 공백 기준으로 구분되어서 한 줄을 받을 수 없다.
따라서, getline()을 이용한다.
string str;
getline(cin, str);
이 방식으로 하면 공백을 포함해 한 줄을 입력받을 수 있다.
728x90
반응형
'알고리즘' 카테고리의 다른 글
[BOJ] 18111 마인크래프트 (0) | 2023.05.26 |
---|---|
[BOJ] 7568 덩치 (2) | 2023.05.26 |
[BOJ] 2108 통계학 (0) | 2023.05.25 |
[BOJ] 1966 프린터 큐 (0) | 2023.05.25 |
[BOJ] 1929 소수 구하기 (2) | 2023.05.25 |