반응형

10828번 문제 : 스택

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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <stack>
#include <string>
using namespace std;
 
int main() {
    int num;
    cin >> num;
    stack<int> s;
 
    while (num > 0)
    {
        string inp;
        cin >> inp;
 
        if (inp == "push")
        {
            int inp2;
            cin >> inp2;
            s.push(inp2);
        }
        else if (inp == "pop")
        {
            if (!s.empty()) {
                cout << s.top() << endl;
                s.pop();
            }
            else{
                cout << "-1" << endl;
            }
        }
        else if (inp == "size")
        {
            cout << s.size() << endl;
        }
        else if (inp == "empty")
        {
            if (!s.empty()) {
                cout << "0" << endl;
            }
            else{
                cout << "1" << endl;
            }
        }
        else if (inp == "top")
        {
            if (!s.empty()) {
                cout << s.top() << endl;
            }
            else{
                cout << "-1" << endl;
            }
        }
        else
        {
            cout <<"command error: "<< inp << endl;
        }
 
        num--;
    }
 
    return 0;
}
cs

메모
-c++에서는 string을 switch문에 활용할 수 없음 int만 가능 (c#은 가능)

1874번 문제 : 스택 수열

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;
 
int main()
{
    int num;
    cin >> num;
    vector<int> v;
    vector<string> result;
 
    while (num > 0)
    {
        int inp;
        cin >> inp;
        v.push_back(inp);
        num--;
    }
 
    stack<int> s;
    int value = 1;
    int index = 0;
    int popCounter = 0;
    while (true)
    {
        if (s.empty())
        {
            if (popCounter != v.size())
            {
                s.push(value);
                result.push_back("+");
                value++;
            }
            else
                break;
        }
        else
        {
            if (s.top() == v.at(index))
            {
                s.pop();
                popCounter++;
                result.push_back("-");
 
                if (v.size() > index)
                    index++;
                else
                {
                    result.clear();
                    result.push_back("NO");
                    break;
                }
            }
            else
            {
                if (value > v.size())
                {
                    result.clear();
                    result.push_back("NO");
                    break;
                }
 
                s.push(value);
                result.push_back("+");
                value++;
            }
        }
    }
 
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << "\n";
 
    return 0;
}
 
cs

메모:
-for(auto i: result) 사용하면 시간초과 발생

9012번 문제 : 괄호

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
 
string CheckVPS(string& str)
{
    stack<int> s;
    for (int i = 0; i < str.size(); i++)
    {
        if (s.empty())
        {
            if (str[i] == ')')
                return "NO";
            else if (str[i] == '(')
                s.push(1);
        }
        else
        {
            if (str[i] == ')')
                s.pop();
            else if (str[i] == '(')
                s.push(1);
        }
    }
 
    if (s.empty())
        return "YES";
    else
        return "NO";
}
 
int main() {
    
    int num;
    cin >> num;
    vector<string> result;
    
    while (num > 0)
    {
        string inp;
        cin >> inp;
 
        result.push_back(CheckVPS(inp));
        
        num--;
    }
 
    for (auto i : result)
        cout << i << endl;
 
    return 0;
}
cs


반응형

'C++ Algorithm > 백준 알고리즘 문제 풀이' 카테고리의 다른 글

피보나치 수  (0) 2019.03.11
큐 사용하기  (0) 2019.02.26
소수 구하기  (0) 2019.02.13
정렬해보기  (0) 2019.02.10
규칙 찾기  (0) 2019.02.05

+ Recent posts