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

PublicMinsu·2022년 11월 25일
0

문제

접근 방법

규칙이 있는 거 같은데 복잡하니 중복을 제거하는 set에 집어넣고 몇 번째 순서인지 확인하는 방법을 하였다.

코드

#include <string>
#include <vector>
#include <set>
#include <queue>
using namespace std;

struct wordObj
{
    int count;
    string word;
};
int solution(string word)
{
    char aeiou[] = {'A', 'E', 'I', 'O', 'U'};
    int answer = 0;
    queue<wordObj> q;
    set<string> dic;
    q.push({0, ""});
    while (!q.empty())
    {
        wordObj obj = q.front();
        q.pop();
        if (obj.count == 5)
        {
            dic.insert(obj.word);
        }
        else
        {
            ++obj.count;
            for (int i = 0; i < 5; ++i)
            {
                q.push({obj.count, obj.word + aeiou[i]});
            }
            q.push({obj.count, obj.word});
        }
    }
    for (string w : dic)
    {
        answer++;
        if (w == word)
            break;
    }
    return answer - 1;
}

풀이

비효율적이긴 해도 문제를 풀 때마다 set에 만들어질 수 있는 모든 단어를 집어넣고 순서를 세주는 방식이다.
-1을 해주는 이유는 비어있는 문자열도 집어넣었기 때문이다.

profile
연락 : publicminsu@naver.com

0개의 댓글