[C++] ์Šคํƒ(Stack)

2020. 4. 30. 18:38ยท๐Ÿ“ Computer Science/โœ Algorithm

์Šคํƒ(Stack)

์Šคํƒ

 

๋ฌธ์ œ

 

https://www.acmicpc.net/problem/4949

 

4949๋ฒˆ: ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

๋ฌธ์ œ ์„ธ๊ณ„๋Š” ๊ท ํ˜•์ด ์ž˜ ์žกํ˜€์žˆ์–ด์•ผ ํ•œ๋‹ค. ์–‘๊ณผ ์Œ, ๋น›๊ณผ ์–ด๋‘  ๊ทธ๋ฆฌ๊ณ  ์™ผ์ชฝ ๊ด„ํ˜ธ์™€ ์˜ค๋ฅธ์ชฝ ๊ด„ํ˜ธ์ฒ˜๋Ÿผ ๋ง์ด๋‹ค. ์ •๋ฏผ์ด์˜ ์ž„๋ฌด๋Š” ์–ด๋–ค ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ด„ํ˜ธ๋“ค์˜ ๊ท ํ˜•์ด ์ž˜ ๋งž์ถฐ์ ธ ์žˆ๋Š”์ง€ ํŒ๋‹จํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์งœ๋Š” ๊ฒƒ์ด๋‹ค. ๋ฌธ์ž์—ด์— ํฌํ•จ๋˜๋Š” ๊ด„ํ˜ธ๋Š” ์†Œ๊ด„ํ˜ธ("()") ์™€ ๋Œ€๊ด„ํ˜ธ("[]")๋กœ 2์ข…๋ฅ˜์ด๊ณ , ๋ฌธ์ž์—ด์ด ๊ท ํ˜•์„ ์ด๋ฃจ๋Š” ์กฐ๊ฑด์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๋ชจ๋“  ์™ผ์ชฝ ์†Œ๊ด„ํ˜ธ("(")๋Š” ์˜ค๋ฅธ์ชฝ ์†Œ๊ด„ํ˜ธ(")")์™€๋งŒ ์ง์„ ์ด๋ค„์•ผ ํ•œ๋‹ค. ๋ชจ๋“  ์™ผ์ชฝ ๋Œ€๊ด„ํ˜ธ("[")๋Š” ์˜ค๋ฅธ์ชฝ ๋Œ€๊ด„

www.acmicpc.net

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

int main(void)
{
	while (1) {
		string s;
		getline(cin, s);
		if (s.length() == 1 && s[0] == '.')
			break;

		stack<char> st;
		bool flag = true;
		for (int i = 0; i < s.length(); i++) {
			if (s[i] == '(' || s[i] == '{' || s[i] == '[')
				st.push(s[i]);
			else if (s[i] == ']' || s[i] == '}' || s[i] == ')') {
				if (!st.empty() && (st.top() == '[') && (s[i] == ']'))
					st.pop();
				else if (!st.empty() && (st.top() == '{') && (s[i] == '}'))
					st.pop();
				else if (!st.empty() && (st.top() == '(') && (s[i] == ')'))
					st.pop();
				else {
					flag = false;
					break;
				}
			}
		}

		if (flag && st.empty())
			cout << "yes\n";
		else
			cout << "no\n";
	}
	return 0;
}

 

https://www.acmicpc.net/problem/1874

 

1874๋ฒˆ: ์Šคํƒ ์ˆ˜์—ด

1๋ถ€ํ„ฐ n๊นŒ์ง€์— ์ˆ˜์— ๋Œ€ํ•ด ์ฐจ๋ก€๋กœ [push, push, push, push, pop, pop, push, push, pop, push, push, pop, pop, pop, pop, pop] ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ์ˆ˜์—ด [4, 3, 6, 8, 7, 5, 2, 1]์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

www.acmicpc.net

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

int main(void)
{
	int n;
	cin >> n;

	stack<int> st;
	string answer = "";
	int num = 0;

	while (n--) {
		int temp;
		cin >> temp;

		while (st.empty() || st.top() < temp) {
			st.push(++num);
			answer += "+\n";
		}

		if (st.top() == temp) {
			st.pop();
			answer += "-\n";
		}
		else {
			cout << "NO\n";
			return 0;
		}
	}

	cout << answer;
	return 0;
}

 

https://www.acmicpc.net/problem/1406

 

1406๋ฒˆ: ์—๋””ํ„ฐ

๋ฌธ์ œ ํ•œ ์ค„๋กœ ๋œ ๊ฐ„๋‹จํ•œ ์—๋””ํ„ฐ๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํŽธ์ง‘๊ธฐ๋Š” ์˜์–ด ์†Œ๋ฌธ์ž๋งŒ์„ ๊ธฐ๋กํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ์ง‘๊ธฐ๋กœ, ์ตœ๋Œ€ 600,000๊ธ€์ž๊นŒ์ง€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ํŽธ์ง‘๊ธฐ์—๋Š” '์ปค์„œ'๋ผ๋Š” ๊ฒƒ์ด ์žˆ๋Š”๋ฐ, ์ปค์„œ๋Š”

www.acmicpc.net

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

int main()
{
	cin.tie(NULL);
	cout.tie(NULL);
	ios::sync_with_stdio(false);

	string s;
	cin >> s;

	stack<char> StackL, StackR;
	for (int i = 0; i < s.length(); i++)
		StackL.push(s[i]);

	int M;
	cin >> M;
	while (M--) {
		cin >> s;
		if (s == "L") {
			if (!StackL.empty()) {
				StackR.push(StackL.top());
				StackL.pop();
			}
		}
		else if (s == "D") {
			if (!StackR.empty()) {
				StackL.push(StackR.top());
				StackR.pop();
			}
		}
		else if (s == "B") {
			if (!StackL.empty())
				StackL.pop();
		}
		else if (s == "P") {
			char tmp;
			cin >> tmp;
			StackL.push(tmp);
		}
	}

	while (!StackL.empty()) {
		StackR.push(StackL.top());
		StackL.pop();
	}

	while (!StackR.empty()) {
		cout << StackR.top();
		StackR.pop();
	}
}

 

https://www.acmicpc.net/problem/6549

 

6549๋ฒˆ: ํžˆ์Šคํ† ๊ทธ๋žจ์—์„œ ๊ฐ€์žฅ ํฐ ์ง์‚ฌ๊ฐํ˜•

์ž…๋ ฅ์€ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ์ง์‚ฌ๊ฐํ˜•์˜ ์ˆ˜ n์ด ๊ฐ€์žฅ ์ฒ˜์Œ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ n ≤ 100,000) ๊ทธ ๋‹ค์Œ n๊ฐœ์˜ ์ •์ˆ˜ h1, ..., hn (0 ≤ hi ≤

www.acmicpc.net

#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	while (cin >> n && n) {
		vector<int> height(n + 1);
		for (int i = 0; i < n; i++)
			cin >> height[i];

		stack<int> st;
		long long answer = 0;
		for (int i = 0; i <= n; i++) {
			while (!st.empty() && height[i] < height[st.top()]) {
				int t = st.top();
				st.pop();

				int width = i - (st.empty() ? 0 : st.top() + 1);
				answer = max(answer, (long long)height[t] * width);
			}
			st.push(i);
		}
		cout << answer << "\n";
	}
}

 

https://leetcode.com/problems/maximal-rectangle/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int maximalRectangle(vector<vector<char>> &matrix)
    {
        if (matrix.empty())
            return 0;

        int answer = 0;
        int c = matrix[0].size();
        vector<int> h(c + 1, 0);

        for (auto &v : matrix)
        {
            for (int i = 0; i < c; ++i)
                h[i] = v[i] == '0' ? 0 : h[i] + 1;

            stack<int> st;
            for (int i = 0; i <= c;)
            {
                if (!st.empty() && h[i] < h[st.top()])
                {
                    int top = st.top();
                    st.pop();

				    int width = i - (st.empty() ? 0 : st.top() + 1);
                    answer = max(answer, h[top] * width);
                }
                else
                    st.push(i++);
            }
        }

        return answer;
    }
};

 

https://leetcode.com/problems/trapping-rain-water/

 

Trapping Rain Water - LeetCode

Trapping Rain Water - Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.   Example 1: [https://assets.leetcode.com/uploads/2018/10/22/rainwatertrap.png] Input: he

leetcode.com

#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

class Solution
{
public:
    int trap(vector<int> &height)
    {
        int answer = 0;
        stack<int> st;

        for (int i = 0; i < height.size(); ++i)
        {
            while (!st.empty() && height[i] > height[st.top()])
            {
                int top = st.top();
                st.pop();

                if (st.empty())
                    break;

                int heightWater = min(height[i], height[st.top()]) - height[top];
                int width = i - (st.top() + 1);
                answer += heightWater * width;
            }

            st.push(i);
        }

        return answer;
    }
};

 

https://leetcode.com/problems/min-stack/description/

#include <stack>
using namespace std;

class MinStack {
public:
    stack<int> s;
    stack<int> minS;

    MinStack() {
    }

    void push(int val) {
        minS.push((s.empty() || minS.top() > val) ? val : minS.top());
        s.push(val);
    }

    void pop() {
        s.pop();
        minS.pop();
    }

    int top() {
        return s.top();
    }

    int getMin() {
        return minS.top();
    }
};

 

https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/

#include <stack>
using namespace std;

class Solution {
public:
    int kthSmallest(TreeNode* root, int k) {
        stack<TreeNode*> s;
        while (root != nullptr || s.empty() == false) {
            while (root != nullptr) {
                s.push(root);
                root = root->left;
            }

            root = s.top();
            s.pop();

            k--;
            if (k == 0)
                return root->val;
            
            root = root->right;
        }

        return -1;
    }
};
์ €์ž‘์žํ‘œ์‹œ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ“ Computer Science/โœ Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [C++] ๋ฑ(Deque)
  • [C++] ํ(Queue), ์šฐ์„ ์ˆœ์œ„ ํ(Priority Queue)
  • [C] ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ(BST)
  • [C++] binary_search, lower_bound, upper_bound
Blxxming
Blxxming
CS ์ง€์‹๊ณผ ๊ณต๋ถ€ํ•˜๋‹ค ๋ฐฐ์šด ๊ฒƒ, ๊ฒฝํ—˜ํ•œ ๊ฒƒ ๋“ฑ์„ ๊ธฐ๋กํ•˜๋Š” ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
  • Blxxming
    ๐Ÿ’ก๋ฒˆ๋œฉ๐Ÿ’ก
    Blxxming
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
  • ๊ณต์ง€์‚ฌํ•ญ

    • Tech Interview
    • ๐Ÿ“š Tech (246)
      • ๐Ÿ“ Computer Science (96)
        • โœ OS (12)
        • โœ Network & Web (10)
        • โœ Database (11)
        • โœ Data Structure (6)
        • โœ Algorithm (40)
        • โœ Design Pattern (9)
        • โœ Cloud Computing (3)
        • โœ (5)
      • ๐Ÿ“ Language (73)
        • โœ Language (6)
        • โœ C & C++ (11)
        • โœ C# (19)
        • โœ JAVA (37)
      • ๐Ÿ“ Game (43)
        • โœ Computer Graphics (2)
        • โœ Unity (14)
        • โœ Unreal (26)
        • โœ (1)
      • ๐Ÿ“ Book (34)
        • โœ Effective (3)
        • โœ Game Server (16)
        • โœ Clean Code (14)
        • โœ (1)
  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.0
Blxxming
[C++] ์Šคํƒ(Stack)
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”