분류 전체보기 (51) 썸네일형 리스트형 백준 오큰수(17298) C++ 풀이 알고리즘 분류 자료 구조 스택 문제 해결 아이디어 stack을 이용하여 empty()가 아닌 동안 자기보다 큰 값이 나올 때까지 pop()하고 empty()가 되면 -1를 넣으면 된다. 코드 #include #include using namespace std; int n; int arr[1000001]; int result[1000001]; stack st; int main() { cin >> n; for (int i = 0; i > arr[i]; } for (int i = n - 1; i >= 0; i--) { while (!st.empty()) { if (st.top() > arr[i]) { result[i] = st.top(); break; } else st.pop().. 백준 옥상 정원 꾸미기(6198) C++ 풀이 알고리즘 분류 자료 구조 스택 문제 해결 아이디어 뒤에 인덱스부터 탐색을 시작하여 Stack을 이용하여 현재 있는 곳에서 볼 수 있는 곳을 pop하면서 누적하여 저장하면 된다. 코드 #include #include using namespace std; struct Building { long long height; long long cnt; }; int n; long long arr[80001]; stack st; long long result = 0; int main() { cin >> n; for (int i = 0; i > arr[i]; } for (int i = n - 1; i >= 0; i--) { long long count = 0; while (!st.empty.. 백준 탑(2493) C++ 풀이 알고리즘 분류 자료 구조 스택 문제 해결 아이디어 스택을 이용하여 전에 있던 탑 중 어떤 크기의 탑(입력한 값보다 크면 됨)이랑 충돌되는지 출력하면 된다. 코드 #include #include using namespace std; struct Top { int height; int location; }; stack st; int n; int num; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i > num; while (!st.empty()) { if (st.top().height >= num) { cout 백준 스택 수열(1874) C++ 풀이 알고리즘 분류 자료 구조 스택 문제 해결 아이디어 stack을 이용하여 1부터 n까지 반복하여 찾으려는 숫자랑 stack의 Top()과 일치하면 pop()을 하고 다음 찾을 숫자로 넘어가고 같지 않다면 push()를 하여 찾는 값이 올 때까지 대기하고 있으면 된다. 이 과정을 다 하고 stack이 empty이면 수열을 만든 것이고, 아니면 실패이다. 코드 #include #include #include using namespace std; int arr[100001]; int n; int idx = 0; stack st; vector v; int main() { cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (int i = 1; i 백준 제로(10773) C++ 풀이 알고리즘 분류 구현 자료 구조 스택 문제 해결 아이디어 Stack을 이용하여 입력받은 숫자를 넣고 0이 입력되면 pop()하여 가장 최근에 들어온 숫자를 지운다. 이 과정을 다 하면 stack에 남아있는 숫자를 더하면 된다. 코드 #include #include using namespace std; int main() { int k; stack s; cin >> k; int input; for (int i = 0; i > input; if (input != 0) s.push(input); else { if (!s.empty()) s.pop(); } } int cnt = 0; while (!s.empty()) { cnt += s.top(); s.pop(); } cout 백준 스택(10828) C++ 풀이 알고리즘 분류 자료 구조 스택 문제 해결 아이디어 Stack이 LIFO(마지막으로 들어온 것이 첫번째로 나간다)라는 것을 알고 문제에 제시한 대로 구현을 하면 됩니다. 코드 #include using namespace std; class Stack { private: int top; int* arr; public: Stack(int size) { arr = new int[size + 1]; top = -1; } void push(int num) { arr[++top] = num; } int pop() { if (empty()) return -1; return arr[top--]; } int size() { return top + 1; } int empty() { if (top == -1) return 1.. 백준 키로거(5397) C++ 풀이 알고리즘 분류 자료 구조 스택 연결 리스트 문제 해결 아이디어 list와 iterator를 이용하여 자신이 지금 어디를 순회하고 있고, 어디로 이동하려 하는지를 알면 해결 할 수 있는 문제이다. 코드 #include #include using namespace std; int t; string s; int main() { cin >> t; while (t--) { list l; list::iterator it = l.end(); cin >> s; for (char str : s) { if (str == '') { if (it != l.end()) it++; } else if (str == '-') { if (it != l.begin()) l.erase(prev(it)); } else l.insert(it.. 백준 에디터(1406) C++ 풀이 알고리즘 분류 자료 구조 스택 연결 리스트 문제 해결 아이디어 list와 iterator를 이용하여 list의 기능을 통해 문제를 해결하면 된다. list정리:https://khjtoy.tistory.com/17 초보자를 위한 STL list 정리 1)list란 무엇인가 1-1.list의 존재 이유 초보자를 위한 C++ STL forward_list 정리 1)forward_list 사용 이유 앞서 살펴본 STL array, vector같은 연속된 자료 구조에서는 데이터 중간에 자료를 추가하거나 삭제하 khjtoy.tistory.com iterator(반복자):https://khjtoy.tistory.com/16 초보자를 위한 c++ STL 반복자(iterator) 정리 1)반복자란 무엇인가 앞서 arra.. 이전 1 2 3 4 5 6 7 다음