728x90
반응형
덱은 양방향에서 입력과 출력이 모두 가능한 자료구조입니다. 양방향의 입출력으로 인해 환형의 형태를 가진다는 점을 주의하며 이전의 큐 문제와 비슷하게 코드를 작성를 하여야 합니다.
2021.07.08 - [알고리즘/문제풀이] - [BOJ 18528번]큐 2
[BOJ 18528번]큐 2
큐 2 문제는 자료구조 큐를 활용하여 해결할 수 있는 문제이다. 자료구조 큐에 대한 학습을 하였다면 어려움없이 무난하게 풀 수 있는 문제이다. 큐에 대한 내용은 다음 링크에 확인할 수 있다. 2
ark-hive.tistory.com
작성한 코드는 다음과 같습니다.
#include<stdio.h>
#define size1 20020
int N;
int temp;
int x;
int deque[size1];
int front;
int back=1;
void push_front(int x) {
deque[front] = x;
front = front-1+size1;
front %= size1;
}
void push_back(int x) {
deque[back] = x;
back += 1;
back %= size1;
}
void pop_front() {
if (((front + 1)%size1) == (back%size1)) printf("%d\n", -1);
else {
front += 1;
front %= size1;
printf("%d\n", deque[front]);
}
}
void pop_back() {
if (((front + 1)%size1) == (back%size1)) printf("%d\n", -1);
else {
back = back-1+size1;
back %= size1;
printf("%d\n", deque[back]);
}
}
void size() {
printf("%d\n", (back - front - 1 + size1) % size1);
}
void empty() {
if (((front + 1) % size1) == (back % size1))
printf("%d\n", 1);
else
printf("%d\n", 0);
}
void front_f() {
if (((front + 1) % size1) == (back % size1)) printf("%d\n", -1);
else printf("%d\n", deque[(front+1)%size1]);
}
void back_f() {
if (((front + 1) % size1) == (back % size1)) printf("%d\n", -1);
else printf("%d\n", deque[(back-1+size1)%size1]);
}
int main(void) {
scanf("%d%*c", &N);
for (int i = 0; i < N; i++) {
x = 0;
temp = getchar();
if (temp == 'p') {
temp = getchar();
if (temp == 'u') {
for (int i = 0; i < 4; i++)
temp = getchar();
if (temp == 'f') {
for (int i = 0; i < 5; i++)
temp = getchar();
while ((temp = getchar()) != '\n' && temp != EOF) {
x *= 10;
x += temp - '0';
}
push_front(x);
}
else {
for (int i = 0; i < 4; i++)
temp = getchar();
while ((temp = getchar()) != '\n' && temp != EOF) {
x *= 10;
x += temp - '0';
}
push_back(x);
}
}
else {
for (int i = 0; i < 3; i++)
temp = getchar();
if (temp == 'f') {
pop_front();
}
else {
pop_back();
}
while ((temp = getchar()) != '\n' && temp != EOF);
}
}
else if (temp == 's') {
size();
while ((temp = getchar()) != '\n' && temp != EOF);
}
else if (temp == 'e') {
empty();
while ((temp = getchar()) != '\n' && temp != EOF);
}
else if (temp == 'f') {
front_f();
while ((temp = getchar()) != '\n' && temp != EOF);
}
else if (temp == 'b') {
back_f();
while ((temp = getchar()) != '\n' && temp != EOF);
}
}
}
문제의 지문은 다음 링크에서 확인할 수 있습니다.
728x90
반응형
'전공 > Problem Solving' 카테고리의 다른 글
[알고리즘/문제풀이/BOJ 5430번] AC (0) | 2021.07.09 |
---|---|
[알고리즘/문제풀이/BOJ 1021번] 회전하는 큐 (0) | 2021.07.08 |
[알고리즘/문제풀이/BOJ 1966번] 프린터 큐 (0) | 2021.07.08 |
[알고리즘/문제풀이/BOJ 11866번] 요세푸스 문제 0 (0) | 2021.07.08 |
[알고리즘/문제풀이/BOJ 2164번] 카드 2 (0) | 2021.07.08 |