cosmohoo 2021. 10. 7. 00:26
반응형

문제설명

 

 

입출력 예 

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

using namespace std;
vector<char> collection = {'A', 'E', 'I', 'O', 'U'}; //모음
vector<string> dic; //단어모음

void DFS(string word, int len)
{
    if (len == word.length()) 
    { 
        dic.push_back(word); 
        return;
    } 
    for (int i=0; i<collection.size(); i++) 
    { 
        DFS(word + collection[i], len);
    }
}

int solution(string word) {
    for(int len =1; len <= 5; len ++)
    {
        string word = ""; 
        DFS(word, len);
    }
    sort(dic.begin(), dic.end());
    
    for(int i=0; i< dic.size(); i++)
    {
    if(word == dic[i])//찾았을 경우 
        {
        return i+1;
         }
    }
   
}

 

 

=> DFS를 이용해 만들 수 있는 모든 단어를 dic이라는 벡터에 집어넣었다. 

 

=> DFS를 통해 다 만들어놓은 dic 벡터를 sort 함수를 사용해 정렬한다. 

 

=> 처음부터 탐색하며 word를 찾았을 경우 return한다. 

 

 

https://codingham.tistory.com/168

 

DFS, BFS code

=>BFS 와 DFS code를 사용하기 위해 미리 정리해두었습니다. //int dist[100001]={0}; //bool check[100001];//갔다온지 확인하는 행렬 bool arr[MAX][MAX]; //인접행렬 vector list[MAX]; //인접리스트 vector >e..

codingham.tistory.com

 DFS 코드와 BFS 코드는 미리 정리해두었다. 

반응형