반응형
문제
정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램을 작성하시오.
명령은 총 여섯 가지이다.
push X: 정수 X를 큐에 넣는 연산이다.
pop: 큐에서 가장 앞에 있는 정수를 빼고, 그 수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
size: 큐에 들어있는 정수의 개수를 출력한다.
empty: 큐가 비어있으면 1, 아니면 0을 출력한다.
front: 큐의 가장 앞에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
back: 큐의 가장 뒤에 있는 정수를 출력한다. 만약 큐에 들어있는 정수가 없는 경우에는 -1을 출력한다.
입력
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 2,000,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
출력
출력해야하는 명령이 주어질 때마다, 한 줄에 하나씩 출력한다.
내 풀이 )
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// 문제
// 1. 큐를 이용하여 입력을 처리하는 프로그램을 만드시오.
// 입력
// 1. 명령어의 수를 입력한다.
// 2. 명령어를 입력한다.
// 처리
// 1. 명령어에 맞게 큐를 작성한다.
// 출력
std::ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
queue<int> que;
int N = 0;
cin >> N;
for (int i = 0; i < N; i++)
{
string str = "";
cin >> str;
if (str == "push")
{
int num = 0;
cin >> num;
que.push(num);
}
else if (str == "pop")
{
if (que.empty() == false)
{
cout << que.front() << "\n";
que.pop();
}
else
{
cout << -1 << "\n";
}
}
else if (str == "size")
{
cout << que.size() << "\n";
}
else if (str == "empty")
{
if (que.empty())
{
cout << 1 << "\n";
}
else
{
cout << 0 << "\n";
}
}
else if (str == "front")
{
if (que.empty())
{
cout << -1 << "\n";
}
else
{
cout << que.front() << "\n";
}
}
else if (str == "back")
{
if (que.empty())
{
cout << -1 << "\n";
}
else
{
cout << que.back() << "\n";
}
}
}
return 0;
}
다른 풀이)
#include <cstdio>
char c[24000011];
char q[16000011];
long long readPointer = 0;
long long writePointer = 0;
long long qEnd = 0;
long long qFront = 0;
char tmp[10];
long long cPointer;
int main() {
long long n;
scanf("%lld", &n);
fread(c, 1, 24000010, stdin);
while (c[readPointer] < 'a') {
readPointer++;
}
readPointer++;
while (n--) {
char cc = c[readPointer];
if (cc <= 'm') {
if (cc == 'a') {
readPointer += 5;
if (qEnd == qFront) {
c[writePointer++] = '-';
c[writePointer++] = '1';
c[writePointer++] = '\n';
} else {
cPointer = qEnd - 8;
while (q[cPointer] != '\0') {
c[writePointer++] = q[cPointer++];
}
}
} else if (cc == 'i') {
readPointer += 5;
long long s = (qEnd - qFront) >> 3;
if (s == 0) {
c[writePointer++] = '0';
c[writePointer++] = '\n';
} else {
long long a = 0;
while (s > 0) {
tmp[a++] = (s % 10) + '0';
s /= 10;
}
while (a > 0) {
a--;
c[writePointer++] = tmp[a];
}
c[writePointer++] = '\n';
}
} else { // if cc == 'm'
readPointer += 6;
if (qEnd == qFront) {
c[writePointer++] = '1';
c[writePointer++] = '\n';
} else {
c[writePointer++] = '0';
c[writePointer++] = '\n';
}
}
} else {
if (cc == 'o') {
readPointer += 4;
if (qEnd == qFront) {
c[writePointer++] = '-';
c[writePointer++] = '1';
c[writePointer++] = '\n';
} else {
while (q[qFront] != '\0') {
c[writePointer++] = q[qFront++];
}
if ((qFront & 0b111) != 0) {
qFront = (qFront & ~0b111) + 0b1000;
}
}
} else if (cc == 'r') {
readPointer += 6;
if (qEnd == qFront) {
c[writePointer++] = '-';
c[writePointer++] = '1';
c[writePointer++] = '\n';
} else {
cPointer = qFront;
while (q[cPointer] != '\0') {
c[writePointer++] = q[cPointer++];
}
}
} else { // cc == 'u'
readPointer += 4;
while (c[readPointer] != '\n') {
q[qEnd++] = c[readPointer++];
}
q[qEnd++] = '\n';
q[qEnd++] = '\0';
if ((qEnd & 0b111) != 0) {
qEnd = (qEnd & ~0b111) + 0b1000;
}
readPointer += 2;
}
}
}
fwrite(c, 1, writePointer, stdout);
}
반응형
'알고리즘' 카테고리의 다른 글
[백준 알고리즘 c++] 문제 26.요세푸스 문제 0 11866 (0) | 2022.09.02 |
---|---|
[백준 알고리즘 c++] 문제 25.카드2 2164 (0) | 2022.09.02 |
[백준 알고리즘 c++] 문제 23.균형잡힌 세상 4949 (0) | 2022.09.02 |
[백준 알고리즘 c++] 문제 22.괄호 9012 (0) | 2022.09.02 |
[백준 알고리즘 c++] 문제 21.제로 10773 (0) | 2022.09.02 |