Make Unreal REAL.
article thumbnail
Level 2. 모음사전

 

 

#include <iostream>

using namespace std;

bool rDFS(string s, string& word, int& answer)
{
    const static string alphabets[] = {"A", "E", "I", "O", "U"};

    ++answer;

    if (s == word)
        return true;
    else if (s.length() == 5)
        return false;

    for (const string& alpha : alphabets)
        if (rDFS(s + alpha, word, answer))
            return true;

    return false;
}

int solution(string word)
{
    int answer = 0;
    const string alphabets[] = {"A", "E", "I", "O", "U"};

    for (const string& alpha : alphabets)
        if (rDFS(alpha, word, answer))
            return answer;
}

 

뒤에서부터 개수를 직접 세어 더하는 방법인 것 같다.

 

#include <iostream>

using namespace std;

int solution(string word)
{
    string s("AEIOU");
    int n = word.size(), answer(n);

    for (int i = 0, b = 1; i < n; ++i, b *= 5)
        answer += s.rfind(word[i]) * 781 / b;

    return answer;
}
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그