본문 바로가기
Coding Test/Programmers

[Programmers] (완전탐색) Lv 2. 모음사전

by song.ift 2023. 7. 14.

https://school.programmers.co.kr/learn/courses/30/lessons/84512

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int solution(string word) {
    int answer = 0;
    vector<char> v = { 'A', 'E', 'I', 'O', 'U' };
    
    // 각 자리는 이전 자리들의 가중치 [ 5^4, 5^3, 5^2, 5^1, 5^0 ] 를 가짐.
    // 따라서 AAE 에서 'E' 는 이전 자리수 52 + 51 + 50 = 31 의 가중치를 가지는데
    // E 는 [ 'A', 'E', 'I', 'O', 'U' ] 에서 두번째(인덱스로는 1) 을 이기 때문에
    // [ 31 * index + 1 ] 의 정보를 가진다.
    // 여기서 +1 을 하는 이유는 단어의 누적, A가 항상 +1 정보를 누적하는 것에서 영감
    // "I"(1563) 를 한번 공식으로 풀어보면,
    // (5^4 + 5^3 + 5^2 + 5^1 + 5^0 ) * 2 + 1 = ( 625 + 125 + 25 + 5 + 1 ) * 2 + 1 = 781 * 2 + 1 = 1563

    for (int i = 0; i < word.length(); ++i)
    {
        int cnt = 0, sum = 0;
        while (cnt < 5 - i)
            sum += pow(5, cnt++);

        auto idx = find(v.begin(), v.end(), word[i]) - v.begin();
        answer += sum * idx + 1;
    }

    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/commit/c606641307782eb6825535e51acd98f327fb2d03

 

[level 2] Title: 모음 사전, Time: 0.03 ms, Memory: 4.03 MB -BaekjoonHub · developeSHG/Algorithm-Baekjoon_Programmers@c60664

developeSHG committed Jul 13, 2023

github.com

 

댓글