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

 

10809번: 알파벳 찾기

각각의 알파벳에 대해서, a가 처음 등장하는 위치, b가 처음 등장하는 위치, ... z가 처음 등장하는 위치를 공백으로 구분해서 출력한다. 만약, 어떤 알파벳이 단어에 포함되어 있지 않다면 -1을 출력한다. 단어의 첫 번째 글자는 0번째 위치이고, 두 번째 글자는 1번째 위치이다.

www.acmicpc.net

 

아스키코드로 문제를 보면

소문자는 'a' 가 기본값이 되고

소문자 - 'a' 를 하면 0이 됨을 이용하여 문제를 해결한다.

 

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <cstdio>
#include <string.h>
#pragma warning(disable:4996)
 
char str[100];
int arr[27];
 
int main() {
    scanf("%s", str);
    for (int i = 1; i <= 26; i++) arr[i] = -1;
    for (int i = 0; i < strlen(str); i++) {
        if (arr[str[i] - 'a' + 1== -1) arr[str[i] - 'a' + 1= i;
    }
    for (int i = 1; i <= 26; i++printf("%d ", arr[i]);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

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

BOJ 11655번 ROT13  (0) 2020.01.12
BOJ 10820번 문자열 분석  (0) 2020.01.12
BOJ 10845 큐  (0) 2020.01.11
BOJ 10799번 쇠막대기  (0) 2020.01.11
BOJ 9012번 괄호  (0) 2020.01.11

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

+ Recent posts