[프로그래머스 level 2] 모음 사전 - 84512 (C++)

yeonjuLee·2024년 11월 4일

코딩테스트 대비

목록 보기
10/32
post-thumbnail

(작성중 ~ing)

오늘의 학습 키워드

  • 완전탐색 - 자료형 선택 중요
  • 진법 계산

[프로그래머스] 모음 사전 - 84512

문제해설

진법계산

접근법: 완전탐색

자료형 선택

#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>

using namespace std;

string alpha = "AEIOU";
int base[5]; // = {781, 156, 31, 6, 1};
unordered_map<char, int> book;

void setBase(){
    int factor = 1;
    base[4] = 1;
    for (int i = 4; i >= 0; i--){
        factor *= 5;
        base[i - 1] = base[i] + factor;
    }
}

void setMap(string alpha){
    int idx = 0;
    for (char s : alpha){
        book.insert({s, idx++});
    }
}

int solution(string word) {
    // 사전 준비
    setBase(); // base 구하기 (781 156 31 6 1)
    setMap("AEIOU"); // book['A'] = 0, book['B'] = 1, ...

    int answer = 0, idx = 0;
    for (char s : word){
        answer += (book[s] * base[idx++] + 1);
    }
    return answer;
}

오늘의 회고

0개의 댓글