-
Notifications
You must be signed in to change notification settings - Fork 0
[스택, 큐, 덱] 9월 15일 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "\uC2A4\uD0DD,-\uD050,-\uB371"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| #include <iostream> | ||
| #include <queue> | ||
|
|
||
| using namespace std; | ||
| const int SIZE = 10001; | ||
|
|
||
| int front_pointer = 0, rear_pointer = 0; | ||
| vector<int> deque_vec(SIZE); | ||
|
|
||
| bool empty(){ | ||
| return front_pointer == rear_pointer; | ||
| } | ||
|
|
||
| void push_front(int k){ | ||
| front_pointer = (front_pointer+1) % SIZE; | ||
| deque_vec[front_pointer] = k; | ||
| } | ||
|
|
||
| void push_back(int k){ | ||
| rear_pointer = (rear_pointer+1) % SIZE; | ||
| deque_vec[rear_pointer] = k; | ||
| } | ||
|
|
||
| int pop_front(){ | ||
| front_pointer = (front_pointer + 1) % SIZE; | ||
| return deque_vec[front_pointer]; | ||
| } | ||
|
|
||
| int pop_back(){ | ||
| rear_pointer = (rear_pointer + 1) % SIZE; | ||
| return deque_vec[rear_pointer]; | ||
| } | ||
|
|
||
| int size(){ | ||
| int tmp = (rear_pointer - front_pointer); | ||
| if(tmp < 0){ | ||
| tmp += SIZE; | ||
| } | ||
| return tmp; | ||
| } | ||
|
|
||
| int front() { | ||
| int tmp = (front_pointer + 1) % SIZE; | ||
| return deque_vec[tmp]; | ||
| } | ||
|
|
||
| int back() { | ||
| return deque_vec[rear_pointer]; | ||
| } | ||
|
|
||
| int main(){ | ||
| int n, k; | ||
| string cmd; | ||
| deque<int> dq; | ||
|
|
||
| cin >> n ; | ||
| while(n--){ | ||
| cin >> cmd; | ||
| if(cmd == "push_front"){ | ||
| cin >> k; | ||
| dq.push_front(k); | ||
| continue; | ||
| } | ||
| if(cmd == "push_back"){ | ||
| cin >> k; | ||
| dq.push_back(k); | ||
| continue; | ||
| } | ||
| if(cmd == "pop_back"){ | ||
| if(dq.empty()) | ||
| cout << -1 << '\n'; | ||
| else{ | ||
| cout << dq.back() << '\n'; | ||
| dq.pop_back(); | ||
| } | ||
| continue; | ||
| } | ||
| if(cmd == "pop_front"){ | ||
| if(dq.empty()) | ||
| cout << -1 << '\n'; | ||
| else{ | ||
| cout << dq.front() << '\n'; | ||
| dq.pop_front(); | ||
| } | ||
| continue; | ||
| } | ||
| if(cmd == "size"){ | ||
| cout << dq.size() << '\n'; | ||
| continue; | ||
| } | ||
| if(cmd == "empty"){ | ||
| cout << dq.empty() << '\n'; | ||
| continue; | ||
| } | ||
| if(cmd == "front"){ | ||
| if(dq.empty()) | ||
| cout << -1 << '\n'; | ||
| else{ | ||
| cout << dq.front() << '\n'; | ||
| } | ||
| continue; | ||
| } | ||
| if(cmd == "back"){ | ||
| if(dq.empty()) | ||
| cout << -1 << '\n'; | ||
| else{ | ||
| cout << dq.back() << '\n'; | ||
| } | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include <map> | ||
| #include <stack> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int main() { | ||
| int n; | ||
| cin >> n; | ||
|
|
||
| string s; | ||
| cin >> s; | ||
|
|
||
| //피연산자의 값 입력 | ||
| double t; | ||
| map<char, double> value; | ||
| for (int i = 0; i < n; i++) { | ||
| cin >> t; | ||
| value['A' + i] = t; | ||
| } | ||
|
Comment on lines
+18
to
+22
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. map도 좋지만, 다른 방법도 있어요! 코드를 잘 살펴보시면 답이 나올 것 같아요~ |
||
|
|
||
| stack<double> st; | ||
|
|
||
| for(int i=0; i<s.length(); i++){ | ||
| double a, b; | ||
| char c = s[i]; | ||
| if(c >= 'A' && c <='Z'){ | ||
| st.push(value[c]); | ||
| } | ||
| else if(c == '*'){ | ||
| a = st.top(); | ||
| st.pop(); | ||
| b = st.top(); | ||
| st.pop(); | ||
| st.push(a*b); | ||
| } | ||
| else if(c == '+'){ | ||
| a = st.top(); | ||
| st.pop(); | ||
| b = st.top(); | ||
| st.pop(); | ||
| st.push(a+b); | ||
| } | ||
| else if(c == '/'){ | ||
| a = st.top(); | ||
| st.pop(); | ||
| b = st.top(); | ||
| st.pop(); | ||
| st.push(b/a); | ||
| } | ||
| else if(c == '-'){ | ||
| a = st.top(); | ||
| st.pop(); | ||
| b = st.top(); | ||
| st.pop(); | ||
| st.push(b-a); | ||
| } | ||
|
Comment on lines
+32
to
+59
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기 중복이 많네요! 사칙연산 함수를 만드는건 어떨까요? |
||
| } | ||
|
|
||
| cout << fixed; | ||
| cout.precision(2); | ||
| cout << st.top(); | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include <map> | ||
| #include <algorithm> | ||
|
|
||
| using namespace std; | ||
|
|
||
| bool cmp(const pair<string, int> &i1, const pair<string, int> &i2){ | ||
| if(i1.second != i2.second) | ||
| return i1.second > i2.second; | ||
| if(i1.first.size() != i2.first.size()) | ||
| return i1.first.size() > i2.first.size(); | ||
| return i1.first < i2.first; | ||
| } | ||
| int main(){ | ||
|
|
||
| int n, len; | ||
| cin >> n >> len; | ||
|
|
||
| //맵에 단어와 횟수를 입력 | ||
| map<string, int> word; | ||
| for(int i=0; i<n; i++){ | ||
| string s; | ||
| cin >> s; | ||
| if(s.length() >= len) | ||
| word[s]++; | ||
| } | ||
|
|
||
| //벡터에 맵 입력 | ||
| vector<pair<string, int>> answer(word.begin(), word.end()); | ||
|
Comment on lines
+30
to
+31
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| //정렬 | ||
| sort(answer.begin(), answer.end(), cmp); | ||
|
|
||
| //출력 | ||
| for(int i=0; i<answer.size(); i++){ | ||
| cout << answer[i].first << '\n'; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include <stack> | ||
|
|
||
| using namespace std; | ||
|
|
||
| int solution(vector<vector<int>> board, vector<int> moves) { | ||
| int answer = 0; | ||
| int n = board.size(); | ||
| vector<stack<int>> doll(n); | ||
|
|
||
| //스택에 인형 종류 입력 | ||
| for(int i=n-1; i>=0; i--){ | ||
| for(int j=0; j<n; j++){ | ||
| if(board[i][j] != 0) | ||
| doll[j].push(board[i][j]); | ||
| } | ||
| } | ||
|
Comment on lines
+13
to
+19
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 아주 잘하셨어요! |
||
|
|
||
| stack<int> moved; | ||
| int prev = 0; | ||
| //인형 옮기기 | ||
| for(int move : moves){ | ||
| if(doll[move-1].empty()) | ||
| continue; | ||
|
Comment on lines
+25
to
+26
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 잘 끊으셨습니다! |
||
| int k = doll[move-1].top(); | ||
| doll[move-1].pop(); | ||
| if(k==prev){ //연속해서 쌓였다면 | ||
| moved.pop(); | ||
| //바구니가 비지 않았다면 마지막 값 입력, 비었다면 0 입력 | ||
| if(!moved.empty()){ | ||
| prev = moved.top(); | ||
| } | ||
| else{ | ||
| prev = 0; | ||
| } | ||
| answer += 2; | ||
| } | ||
| else{ | ||
| moved.push(k); | ||
| prev = k; | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
|
|
||
| int main() { | ||
| vector<vector<int>> board = {{0, 0, 0, 0, 0}, | ||
| {0, 0, 1, 0, 3}, | ||
| {0, 2, 5, 0, 1}, | ||
| {4, 2, 4, 4, 2}, | ||
| {3, 5, 1, 3, 1}}; | ||
| vector<int> moves = {1, 5, 3, 5, 1, 2, 1, 4}; | ||
| cout << solution(board, moves); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
와 이걸 직접구현하셨네요! 정말 잘하셨어요!!
근데 사실 삭제&삽입 연산은 front와 back에서 아주 약간 다르게 작동해요!
아무튼 정말 멋져요...!