Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 북리뷰
- 브루트포스
- 개발
- vector
- OS
- 구현
- 오에스
- Computer science
- 알고리즘
- 너비우선탐색
- cs
- 정석학술정보관
- 코테
- 컴공
- 컴퓨터공학과
- 그래프
- 오퍼레이팅시스템
- 백준
- 문제풀이
- Operating System
- DP
- c++
- 자료구조
- 코딩
- coding
- 스택
- bfs
- 컴공과
- Stack
- 정석
Archives
- Today
- Total
Little Jay
[C++] 백준 4889번 - 안정적인 문자 본문
'{'가 들어오면 push하고
'}'가 들어올 때는 먼저 stack이 비었는지 확인하고
비어있으면 '{'로 바꿔주고 바꾼 횟수를 늘려준다
만약 empty가 아니라면 pop해주면 된다
스택이 비었으면 return change
스택이 차 있으면 return change + st.size() / 2
#include <iostream>
#include <stack>
using namespace std;
int main() {
string s;
int test = 1;
while (true) {
cin >> s;
int change = 0;
if (s[0] == '-')
break;
stack<char> st;
for (int i = 0; i < s.length(); i++) {
if (s[i] == '{')
st.push(s[i]);
else {
if (st.empty()) {
st.push('}');
change++;
}
else
st.pop();
}
}
if (st.empty()) {
cout << test << ". " << change << "\n";
}
else {
cout << test << ". " << change + (st.size() / 2) << "\n";
}
test++;
}
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 1931번 - 회의실 배정 (0) | 2021.08.18 |
---|---|
[C++] 백준 1406번 - 에디터 (0) | 2021.08.18 |
[C++] 백준 7568번 - 덩치 (0) | 2021.08.17 |
[C++] 백준 1764번 - 듣보잡 (0) | 2021.08.17 |
[C++] 백준 1620 - 나는야 포켓몬 마스터 이다솜 (0) | 2021.08.17 |
Comments