티스토리 뷰
반응형
백준 4949번 균형잡힌 세상
https://www.acmicpc.net/problem/4949
4949번: 균형잡힌 세상
각 문자열은 마지막 글자를 제외하고 영문 알파벳, 공백, 소괄호("( )"), 대괄호("[ ]")로 이루어져 있으며, 온점(".")으로 끝나고, 길이는 100글자보다 작거나 같다. 입력의 종료조건으로 맨 마지막에
www.acmicpc.net



정답 코드
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int find(string a) {
string finish = " .";
if (a == finish)
return 1;
stack<char>s;
for (int x = 0; x < a.length(); x++) {
if (a[x] == '(' || a[x] == '[')
s.push(a[x]);
else if (a[x] == ')') {
if (s.empty() == 1) {
return 0;
}
else if (s.top() == '(')
s.pop();
else if (s.top() != '(') {
return 0;
}
}
else if (a[x] == ']') {
if (s.empty() == 1) {
return 0;
}
else if (s.top() == '[')
s.pop();
else if (s.top() != '[') {
return 0;
}
}
}
if (s.empty() == 1)
return 1;
else
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string finish = " .";
while (true) {
string a;
getline(cin, a);
if (a == ".")
break;
int ab = find(a);
if (ab == 0)
cout << "no" << endl;
else
cout << "yes" << endl;
}
}
문제 풀이
자료구조 스택과 string을 이용해서 풀었습니다.
스택은 FILO구조인데 이 문제에서는 나중에 입력된 문자열이 먼저 빠져나가야지 앞에 입력된 문장을 뺄 수 있습니다. 그리고 getline(cin,a)를 사용하였는데 띄어쓰기를 포함한 문장을 입력받기 위해서 사용하였습니다.
그 이외에는 스택의 기본 내장 함수를 이용하였습니다.
반응형
'Algorithm > BOJ' 카테고리의 다른 글
백준 5014번 C++ (0) | 2023.08.20 |
---|---|
백준 4963번 C++ (0) | 2023.08.20 |
백준 4948번 C++ (0) | 2023.08.18 |
백준 4673번 C++ (0) | 2023.08.17 |
백준 4344번 C++ (0) | 2023.08.17 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DFS
- 스택
- Do it!
- 알고리즘
- 유니온 파인드
- C++ Stack
- js
- 이분 매칭
- 카운팅 정렬
- 세그먼트 트리
- 에라토스테네스의 체
- DP
- 자바스크립트
- CSS
- 백준
- C++
- java
- 백준 풀이
- BFS
- 자료구조
- html
- 자바
- 유클리드 호제법
- 알고리즘 공부
- HTML5
- 스프링 부트 crud 게시판 구현
- c++ string
- 우선순위 큐
- 반복문
- 투 포인터
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함