본문 바로가기
Algorithm/BOJ

[BOJ]10808번: 알파벳 개수(c++)

by HBGB 2020. 4. 24.

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

 

10808번: 알파벳 개수

단어에 포함되어 있는 a의 개수, b의 개수, …, z의 개수를 공백으로 구분해서 출력한다.

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
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
 
    string s;
    cin >> s;
 
    vector<int> alpha(260);
 
    for (int i = 0; i < s.size(); i++)
    {
        alpha[s[i] - 'a']++;
    }
 
    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
#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 갯수 연산
        cout << count(s.begin(), s.end(), i) << ' ';
    }
    return 0;
}
Colored by Color Scripter

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

[BOJ]11655번: ROT13(c++)  (0) 2020.04.25
[BOJ]10820번: 문자열 분석(c++)  (0) 2020.04.25
[BOJ]10809번: 알파벳 찾기(c++)  (0) 2020.04.24
[BOJ]1918번: 후위 표기식(c++)  (0) 2020.04.24
[BOJ]1935번: 후위 표기식2(c++)  (0) 2020.04.24

댓글