BOJ : https://www.acmicpc.net/problem/10845

 

큐에 대한 기본 구현을 묻는 문제이다.

 

더보기
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
#include <iostream>
#include <string>
#include <queue>
using namespace std;
 
queue<int> q;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    string str;
    int N, num; cin >> N;
    while (N--) {
        cin >> str;
        if (str == "push") {
            cin >> num;
            q.push(num);
        }
        if (str == "pop") {
            if (q.empty()) cout << "-1\n";
            else cout << q.front() << '\n', q.pop();
        }
        if (str == "size") {
            cout << q.size() << '\n';
        }
        if (str == "empty") {
            cout << q.empty() << '\n';
        }
        if (str == "front") {
            if (q.empty()) cout << "-1\n";
            else cout << q.front() << '\n';
        }
        if (str == "back") {
            if (q.empty()) cout << "-1\n";
            else cout << q.back() << '\n';
        }
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10820번 문자열 분석  (0) 2020.01.12
BOJ 10809번 알파벳 찾기  (0) 2020.01.12
BOJ 10799번 쇠막대기  (0) 2020.01.11
BOJ 9012번 괄호  (0) 2020.01.11
BOJ 10828번 스택  (0) 2020.01.11

BOJ : https://www.acmicpc.net/problem/10799

 

쇠막대기 자르기 문제이다.

( ) 모양이 되면 막대를 자르면서 갯수를 추가해주면 된다.

( ), ( (, ) (, ) ) 이 4가지 모양을 보면 규칙을 찾을 수 있다.

 

더보기
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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
 
stack<char> s;
 
int main() {
    string str;
    cin >> str;
    for (int i = str.size() - 1; i >= 0; i--) {
        s.push(str[i]);
    }
 
    int total = 0;
    int open = 1;
    char pre = '(', now;
    s.pop();
    while (!s.empty()) {
        now = s.top();
        s.pop();
 
        if (now == '(') {
            open++;
        }
        if (now == ')') {
            if (pre == '(') {
                open--;
                total += open;
            }
            if (pre == ')') {
                total++, open--;
            }
        }
 
        pre = now;
    }
    cout << total;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10809번 알파벳 찾기  (0) 2020.01.12
BOJ 10845 큐  (0) 2020.01.11
BOJ 9012번 괄호  (0) 2020.01.11
BOJ 10828번 스택  (0) 2020.01.11
BOJ 11004번 K번째 수 (QuickSelection)  (0) 2020.01.11

BOJ : https://www.acmicpc.net/problem/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
#include <iostream>
#include <stack>
#include <string>
using namespace std;
 
stack<char> s;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int T;
    string str;
    cin >> T;
    while (T--) {
        cin >> str;
        for (int i = 0; i < str.size(); i++) s.push(str[i]);
 
        int temp = 0, test = 1;
        while (!s.empty()) {
            char ch = s.top();
            s.pop();
 
            if (ch == '(') {
                if (!temp) {
                    test = 0;
                    break;
                }
                temp--;
            }
            if (ch == ')') {
                temp++;
            }
        }
        if (temp || !test) cout << "NO\n";
        else cout << "YES\n";
 
        while (!s.empty())s.pop();
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10845 큐  (0) 2020.01.11
BOJ 10799번 쇠막대기  (0) 2020.01.11
BOJ 10828번 스택  (0) 2020.01.11
BOJ 11004번 K번째 수 (QuickSelection)  (0) 2020.01.11
BOJ 11652번 카드  (0) 2020.01.11

BOJ : 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
#include <iostream>
#include <string>
#include <stack>
using namespace std;
 
stack<int> st;
 
int main() {
    int N, num;
    cin >> N;
    string want;
    while (N--) {
        cin >> want;
        if (want == "push") {
            cin >> num;
            st.push(num);
        }
        if (want == "pop") {
            if (st.size()) cout << st.top() << '\n', st.pop();
            else cout << "-1\n";
        }
        if (want == "top") {
            if (st.size()) cout << st.top() << '\n';
            else cout << "-1\n";
        }
        if (want == "size") {
            cout << st.size() << '\n';
        }
        if (want == "empty") {
            cout << st.empty() << "\n";
        }
    }
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > BOJ' 카테고리의 다른 글

BOJ 10799번 쇠막대기  (0) 2020.01.11
BOJ 9012번 괄호  (0) 2020.01.11
BOJ 11004번 K번째 수 (QuickSelection)  (0) 2020.01.11
BOJ 11652번 카드  (0) 2020.01.11
BOJ 10989번 수 정렬하기 3  (0) 2020.01.10

+ Recent posts