본문 바로가기
Algorithm/BOJ

[BOJ]10809번: 알파벳 찾기(c++)

by HBGB 2020. 4. 24.

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

 

10809번: 알파벳 찾기

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

www.acmicpc.net

 

 

방법 1: 라이브러리 사용X

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
#include <iostream>
#include <vector>
 
using namespace std;
 
int main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    vector<int> alpha(26-1);
    string s;
    cin >> s;
 
    for (int i = 0; i < s.size(); i++)
    {
        if (alpha[s[i] - 'a'>= 0)
        {
            continue;
        }
 
        alpha[s[i] - 'a'= i;
    }
 
    for (int i : alpha)
    {
        cout << i << ' ';
    }
 
    return 0;
}
Colored by Color Scripter

 

 

방법 2: 라이브러리 사용O

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
#include <iostream>
#include <algorithm>
 
using namespace std;
 
int main() 
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    string s;
    cin >> s;
 
    for (int i = 'a'; i <= 'z' ; i++)
    {
        // i가 위치한 반복자 찾기
        auto it = find(s.begin(), s.end(), i);
        
        // 못찾았을때
        if (it == s.end())
        {
            cout << -1 << ' ';
        }
        else
        {
            cout << (it - s.begin()) << ' ';
        }
    }
 
    return 0;
}
Colored by Color Scripter

 

 

속도 차이는 없다

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

[BOJ]10820번: 문자열 분석(c++)  (0) 2020.04.25
[BOJ]10808번: 알파벳 개수(c++)  (0) 2020.04.24
[BOJ]1918번: 후위 표기식(c++)  (0) 2020.04.24
[BOJ]1935번: 후위 표기식2(c++)  (0) 2020.04.24
[BOJ]2133번: 타일 채우기(c++)  (0) 2020.04.24

댓글