1. 스택
- 가장 먼저 넣은 값이 가장 마지막에 나오는 구조
10828 | 10773 |
import sys input = sys.stdin.readline n = int(input()) r = [] for i in range(n) : a = list(input().split()) if a[0] == 'push' : r.append(int(a[1])) elif a[0] == 'top' : if len(r) == 0 : print(-1) else : print(r[-1]) elif a[0] == 'pop' : if len(r) == 0 : print(-1) else : print(r[-1]) r.pop(len(r)-1) elif a[0] == 'size' : print(len(r)) elif a[0] == 'empty' : if len(r) == 0 : print(1) else : print(0) |
import sys input = sys.stdin.readline n = int(input()) r = [] for i in range(n) : a = int(input()) if a == 0 : r.pop(len(r)-1) else : r.append(a) print(sum(r)) |
**9012 (방법의 문제, (가 있으면 pop, 없으면 NO 바로 출력) | **17952 |
import sys input = sys.stdin.readline n = int(input()) for i in range(n) : a = input().rstrip() l = [] for x in a : if x == '(' : l.append(x) elif x == ')' : if len(l) == 0 : l.append(x) break else : l.pop(len(l)-1) if len(l) != 0 : print('NO') else : print('YES') |
import sys import collections input = sys.stdin.readline n = int(input()) l = collections.deque() sum = 0 for i in range(n) : a = list(map(int, input().split())) if a[0] == 0 : if len(l) == 0 : continue b, t = l.pop() else : b, t = a[1], a[2] if t == 1 : sum += b else : l.append([b, t-1]) print(sum) |
2. 큐
- 원하는 데이터를 선택해서 내보낼 수 있음
10845 | 11866 |
import sys input = sys.stdin.readline n = int(input()) r = [] for i in range(n) : a = list(input().split()) if a[0] == 'push' : r.append(int(a[1])) elif a[0] == 'front' : if len(r) == 0 : print(-1) else : print(r[0]) elif a[0] == 'back' : if len(r) == 0 : print(-1) else : print(r[-1]) elif a[0] == 'pop' : if len(r) == 0 : print(-1) else : print(r[0]) r.pop(0) elif a[0] == 'size' : print(len(r)) elif a[0] == 'empty' : if len(r) == 0 : print(1) else : print(0) |
import sys input = sys.stdin.readline a, b = map(int, input().split()) l = [] for i in range(a) : l.append(i+1) print('<', end = '') n = 0 j = 0 count = 0 while True : n += 1 if len(l) == 0 : break if j-count >= len(l) : j = 0 count = 0 if n % b == 0 : if len(l) == 1 : print(l[j-count], end='>') l.pop(j-count) count += 1 else : print(l[j-count], end=', ') l.pop(j-count) count += 1 j += 1 |
**2164 | |
import sys import collections input = sys.stdin.readline n = int(input()) a = collections.deque(range(1, n+1)) while True : if len(a) == 1: break for i in range(2) : if i == 0 : a.popleft() else : b = a.popleft() a.append(b) print(*a) |
3. C++ 풀이
- 스택, 큐에 관련된 문제들
10828 | 17952 |
#include <iostream> #include <stack> #include <string> using namespace std; int main() { ios_base::sync_wuth_studio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; stack<int> stk; string str; while(n--){ cin >> str; if(str=="push"){ int num; cin >> num; stk.push(num) } else if(str=="pop"){ if(stk.empty()) { cout << stk.top() << "\n" ; stk.pop(); } else { cout << -1 <<'\n'; } } else if(str == 'top'){ if(stk.empty()) { cout << stk.top() << "\n" ; } else { cout << -1 <<'\n'; } } } } |
#include <iostream> #include <stack> #include <string> using namespace std; int main() { ios_base::sync_wuth_studio(false); cin.tie(NULL); cout.tie(NULL); int n; int num, A, T; cin >> n; stack<<pair<int,int>> stk; int result; while(n--){ cin >> num; if(num==1){ cin >> a>>T ; stk.push({a, T}); } if(stk.empty()){ stk.top().second --; if (stk.top().second == 0){ result == stk.top().first cout << result ; } } } |
10845 | 11866 |
#include <iostream> #include <queue> #include <string> using namespace std; int main() { ios_base::sync_wuth_studio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; queue<int> q; string str; while(n--){ cin >> str; if(str=="push"){ int num; cin >> num; q.push(num) } else if(str=="pop"){ if(stk.empty()) { cout << q.top() << "\n" ; q.pop(); } else { cout << -1 <<'\n'; } } else if(str='size'){ cout << q.size() << '\n'; } else if(str = 'empty') { cout << q.empty() << "\n" ; } else if(str == 'front'){ if(q.empty()) { cout << q.front() << "\n" ; } else { cout << -1 <<'\n'; } } else if(str == 'back'){ if(q.empty()) { cout << q.back() << "\n" ; } else { cout << -1 <<'\n'; } } } } |
#include <iostream> #include <queue> #include <string> #include <vector> using namespace std; int main() { ios_base::sync_wuth_studio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; queue<int> q; for(int i =1;i<=n;i++){ q.push(i); } while(n--){ for(int i=0;i<k-1;i++){ int num = q.front(); q.pop(); q.push(num); } result.push_back(q.front()); q.pop(); } cout << "<"; for(int i=0;i<result.size();i++){ if(i != result.size()-1){ cout<<result[i] << ", "; } else{ cout<<result[i]<<'>' ; } |