๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“• STUDY/Algorithm

[C / C++] ๋ฐฑ์ค€ 10828๋ฒˆ - ์Šคํƒ(Stack)

๋ฐฑ์ค€ 10828๋ฒˆ : https://www.acmicpc.net/problem/10828

 

10828๋ฒˆ: ์Šคํƒ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง€๋Š” ๋ช…๋ น์˜ ์ˆ˜ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๋ช…๋ น์ด ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋ฌธ์ œ์— ๋‚˜์™€์žˆ์ง€

www.acmicpc.net

โœ… ๋ฌธ์ œ ์„ค๋ช…

๋ง๊ทธ๋Œ€๋กœ ์Šคํƒ์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

โšก ์Šคํƒ์ด๋ž€?

๊ฐ„๋‹จํ•˜๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ์ž…๊ตฌ์™€ ์ถœ๊ตฌ๊ฐ€ ๊ฐ™์€ ๊ตฌ์กฐ!

LIFO (Last In First Out) ๋กœ, 

์ ‘์‹œ๋ฅผ ์Œ“์œผ๋ฉด ๋งจ ์œ„์— ๋†“์ธ(๋งจ ๋งˆ์ง€๋ง‰์— ๋†“์€) ์ ‘์‹œ๋ถ€ํ„ฐ ๋นผ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ

๊ฐ€์žฅ ๋‚˜์ค‘์— ๋„ฃ์€ ์ •๋ณด๋ฅผ ๋จผ์ € ๊บผ๋‚ด๋Š” ๊ตฌ์กฐ์ด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด a -> b -> c ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์—ˆ๋‹ค๋ฉด, ๋บ„ ๋•Œ๋Š” c -> b -> a ์ˆœ์„œ๋กœ ๊บผ๋‚ด๊ฒŒ ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์—ฌ๊ธฐ์„œ ๋„ฃ๋Š” ๊ฒƒ์€ ‘push’, ๋นผ๋Š” ๊ฒƒ์€ ‘pop’์ด๋ผ๊ณ  ์นญํ•œ๋‹ค.

 

1. ์ดˆ๊ธฐ ์ƒํƒœ (์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ๊ณต๋ฐฑ ์ƒํƒœ)

2. push(a) -> push(b) -> push(c)

3. pop() -> pop() -> pop()

1. push X : ์ •์ˆ˜ X๋ฅผ ์Šคํƒ์— ๋„ฃ๋Š”๋‹ค.

2. pop : ์Šคํƒ์—์„œ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ •์ˆ˜(๋งจ ๋งˆ์ง€๋ง‰์— ๋„ฃ์€ ์ •์ˆ˜)๋ฅผ ๋นผ๊ณ  ๊ทธ ์ˆ˜๋ฅผ ์ถœ๋ ฅ, ๋งŒ์•ฝ ๋น„์–ด์žˆ์œผ๋ฉด –1 ์ถœ๋ ฅ

3. size : ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

4. empty : ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0 ์ถœ๋ ฅ

5. top : ์Šคํƒ ๊ฐ€์žฅ ์œ„์— ์žˆ๋Š” ์ •์ˆ˜ ์ถœ๋ ฅ, ๋น„์–ด์žˆ์œผ๋ฉด –1 ์ถœ๋ ฅ


โœ… ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๋ช…

1. ์ „์—ญ๋ณ€์ˆ˜ idx๋ฅผ ์‚ฌ์šฉ

-1) ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0์ด ์•„๋‹ˆ๋ผ –1์ด ๋น„์–ด์žˆ๋Š” ์ƒํƒœ์ž„

-2) ๋”ฐ๋ผ์„œ ์ดˆ๊ธฐ์ƒํƒœ๋Š” –1๋กœ ์‹œ์ž‘ํ•˜๋„๋ก ์„ค์ •

 

2. top()

-1) idx๊ฐ€ –1์ด๋ฉด ๊ณต๋ฐฑ์ด๋ฏ€๋กœ –1 ๋ฐ˜ํ™˜

-2) ์•„๋‹ˆ๋ฉด stack[idx] (๋งจ ์œ„์— ์žˆ๋Š” ๊ฐ’) ๋ฐ˜ํ™˜

 

3. push(int x)

-1) idx๊ฐ€ –1๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋จผ์ € idx++์„ ํ•˜์—ฌ 0์œผ๋กœ ๋งŒ๋“ค๊ณ ,

-2) stack[idx]์— x ์ €์žฅ

 

4. pop()

-1) idx๊ฐ€ –1์ด๋ฉด ๊ณต๋ฐฑ์ด๋ฏ€๋กœ –1 ๋ฐ˜ํ™˜

-2) pop()์„ ํ•˜๋ฉด ๋งจ ์œ„ ์ •์ˆ˜ ํ•˜๋‚˜๋ฅผ ์—†์• ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ idx--

-3) ์—†์• ๊ธฐ ์ „ ์ •์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ stack[idx+1] ๋ฐ˜ํ™˜

 

5. size()

-1) idx๋Š” –1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— +1ํ•œ ๊ฐ’์ด ํ˜„์žฌ ์Šคํƒ์— ๋“ค์–ด์žˆ๋Š” ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜

 

6. empty()

-1) idx == -1 ๊ณต๋ฐฑ

-2) idx > -1 ๊ณต๋ฐฑ X


โœ… ์ฝ”๋“œ [C์–ธ์–ด]

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int stack[10001];

/* ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๋ฐฑ์€ -1 */
int idx = -1;

/* top */
// idx๊ฐ€ -1์ด๋ฉด ๊ณต๋ฐฑ, ์•„๋‹ˆ๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋„ฃ์€ ๊ฐ’ ๋ฐ˜ํ™˜
int top() {
	if (idx == -1)return -1;
	return stack[idx];
}

/* push */
// ์ธ๋ฑ์Šค +1 ํ•œ ํ›„ stack ๋ฐฐ์—ด์— x ๋„ฃ๊ธฐ
void push(int x) {
	idx++;
	stack[idx] = x;
}

/* pop */
// ๋ฐฐ์—ด์ด ๊ณต๋ฐฑ ์ƒํƒœ๋ฉด -1 ๋ฐ˜ํ™˜, ์•„๋‹ˆ๋ฉด ์ธ๋ฑ์Šค -1 ํ•œ ํ›„ ๊ฐ’ ๋ฐ˜ํ™˜
int pop() {
	if (idx == -1) return -1;
	idx--;
	return stack[idx + 1]; // idx-- ํ•˜๊ธฐ ์ „ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ idx+1
}

/* size */
// ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— +1 ํ•œ ๊ฐ’ ๋ฐ˜ํ™˜
int size() {
	return (idx + 1);
}

/* empty */
// -1์ด๋ฉด ๊ณต๋ฐฑ, ์•„๋‹ˆ๋ฉด 0๋ฐ˜ํ™˜
int empty() {
	if (idx == -1) return 1;
	else return 0;
}

int main(void) {
	int n;
	scanf("%d", &n);

	for (int i = 0; i < n; i++) {
		char word[6];
		scanf("%s", word);
		if (strcmp(word, "push") == 0) {
			int x;
			scanf("%d", &x);
			push(x);
		}
		else if (strcmp(word, "pop") == 0)
			printf("%d\n", pop());

		else if (strcmp(word, "size") == 0)
			printf("%d\n", size());

		else if (strcmp(word, "empty") == 0)
			printf("%d\n", empty());

		else if (strcmp(word, "top") == 0)
			printf("%d\n", top());
	}
	return 0;
}

https://github.com/2hyunjinn/Baekjoon/blob/5de6d97106efeab2a84bb5180c268dac01d2115e/Baekjoon_10828.c

 

GitHub - 2hyunjinn/Baekjoon

Contribute to 2hyunjinn/Baekjoon development by creating an account on GitHub.

github.com

์œ„ ์ฝ”๋“œ๋Š” stack์˜ ์ •ํ™•ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ณต๋ถ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ’€์–ด์„œ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.

๋งŒ์•ฝ C++์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์ด๋ฏธ stack์„ ๊ตฌํ˜„ํ•ด๋†“์€ C++์˜ ๋‚ด์žฅํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.


โœ… ์ฝ”๋“œ [C++ ๋‚ด์žฅํ•จ์ˆ˜]

 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main(void)
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);

	int n;
	cin >> n;
	
	stack<int> stack;
	for (int i = 0; i < n; i++) {
		string word;
		cin >> word;
		if (word == "push") {
			int x;
			cin >> x;
			stack.push(x);
		}
		else if (word == "pop") {
			if (stack.empty()) cout << -1 << '\n';
			else {
				cout << stack.top() << '\n';
				stack.pop();
			}
		}
		else if (word == "size") {
			cout << stack.size() << '\n';
		}
		else if (word == "empty") {
			cout << stack.empty() << '\n';
		}
		else if (word == "top") {
			if (stack.empty()) cout << -1 << '\n';
			else cout << stack.top() << '\n';
		}
	}

	return 0;
}

 

https://github.com/2hyunjinn/Baekjoon/blob/7601643deb85a3f83e69779bf15ddef03a9508e7/Baekjoon_10828(2).cpp 

 

GitHub - 2hyunjinn/Baekjoon

Contribute to 2hyunjinn/Baekjoon development by creating an account on GitHub.

github.com