본문 바로가기
Develop/백준 (Cpp)

[백준] 7600번 : 문자가 몇갤까 (C++)

by Tarra 2023. 2. 13.

7600번 : 문자가 몇갤까


문제 )

"The quick brown fox jumped over the lazy dogs."

이 문장은 모든 알파벳이 적어도 한 번은 나오는 문장으로 유명하다. 즉 26개의 서로 다른 문자를 갖고 있는 것이다.

각 케이스마다 문장에서 공백, 숫자, 특수 문자를 제외하고 얼마나 다양한 알파벳이 나왔는지를 구하면 된다. 대소문자는 하나의 문자로 처리한다. ex) 'A' == 'a'

 

 

입력 :

입력은 250자를 넘지 않는 문장이 주어진다.

각 문장은 적어도 하나의 공백이 아닌 문자를 포함한다. (알파벳이 아닐 수 있다)

마지막 줄에는 '#'이 주어진다.

 

 

 

출력 :

각 줄마다 출몰한 알파벳의 개수를 출력하면 된다.

 

 

 

 

풀이)

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
41
42
43
44
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
 
using namespace std;
 
int main()
{
    string sentence;
    
    while (1) {
        // 문장 입력
        getline(cin, sentence);
        if (sentence == "#") {
            break;
        }
        int alpha[26]{ 0 };
        // 해당 문장을 모두 소문자로 변환
        transform(sentence.begin(), sentence.end(), sentence.begin(), ::tolower);
 
        // 연산 줄이기
        int len = sentence.length();
        for (int i = 0; i < len; i++) {
            int number = sentence[i] - 'a';
 
            if (number >= 0 && number <= 26) {
                alpha[sentence[i] - 'a']++;
            }
        }
 
        int answer = 0;
        for (auto& ele : alpha) {
            if (ele > 0) {
                answer++;
            }
        }
 
        cout << answer << "\n";
    }
 
    return 0;
}
 
cs

출처 : https://www.acmicpc.net/problem/7600 

 

7600번: 문자가 몇갤까

각 줄마다 출몰한 알파벳의 개수를 출력하면 된다.

www.acmicpc.net