Algorithm/BOJ

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

HBGB 2020. 4. 24. 23:12

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