본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 10384 : 팬그램

반응형

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

 

10384번: 팬그램

팬그램은 모든 알파벳을 적어도 한 번씩을 사용한 영어 문장을 말한다. 다음은 유명한 팬그램 중 하나이다. The quick brown fox jumps over a lazy dog 더블 팬그램은 모든 알파벳을 적어도 두 번씩은 사용

www.acmicpc.net

문자열을 이용한 구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

test case 수 t, 문자열 s를 선언한 후 입력받습니다.

📔 풀이과정

1. 특수기호와 대문자를 걸러내기 위한 함수 getFilteredStr를 수행합니다. 대문자가 있으면 소문자로 바꾸고 특수기호는 제거합니다.

2. getResultStr를 수행합니다. 알파벳이 나온 개수를 세준 후 vector alpha에 저장합니다. 이 원소들 중 최솟값이 pangram을 결정합니다.

📔 정답출력

조건에 따라 반환된 문자열을 형식에 맞게 출력합니다.


📕 Code

#include <bits/stdc++.h>
using namespace std;

string getFilteredStr(string s){
  string str;
  for(auto c : s){
    if('A' <= c && c <= 'Z') str += c - 'A' + 'a';
    else if('a' <= c && c <= 'z') str += c;
  }
  return str;
}

string getResultStr(string s){
  int pangramCnt = 10;
  vector <int> alpha(26,0);
  string str = getFilteredStr(s);
  for(auto c : str){
    alpha[c-'a']++;
  }

  for(int i = 0; i < 26; i++){
    pangramCnt = min(pangramCnt, alpha[i]);
  }

  if(!pangramCnt) return "Not a pangram\n";
  if(pangramCnt == 1) return "Pangram!\n";
  if(pangramCnt == 2) return "Double pangram!!\n";
  return "Triple pangram!!!\n";
}

int main(){
  int t;
  string s;

  cin >> t;
  cin.ignore();

  for(int testCase = 1; testCase <= t; testCase++){
    getline(cin,s);
    cout << "Case " << testCase << ": " << getResultStr(s);
  }
}