알고리즘/programmers
[Programmers 위클리 챌린지 5주차] 모음 사전
꾸준함.
2021. 9. 4. 20:28
문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/84512
코딩테스트 연습 - 5주차
사전에 알파벳 모음 'A', 'E', 'I', 'O', 'U'만을 사용하여 만들 수 있는, 길이 5 이하의 모든 단어가 수록되어 있습니다. 사전에서 첫 번째 단어는 "A"이고, 그다음은 "AA"이며, 마지막 단어는 "UUUUU"입니
programmers.co.kr
word의 길이가 비교적 짧으므로 모든 경우의 수를 구한 뒤 순서를 출력해주면 되는 문제였습니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
vector<char> vowels = { 'A', 'E', 'I', 'O', 'U' }; | |
vector<string> dictionary; | |
void func(string word, int wordLength) | |
{ | |
if (wordLength == word.length()) | |
{ | |
dictionary.push_back(word); | |
return; | |
} | |
for (char vowel : vowels) | |
{ | |
func(word + vowel, wordLength); | |
} | |
} | |
int solution(string word) { | |
for (int len = 1; len <= 5; len++) | |
{ | |
string word = ""; | |
func(word, len); | |
} | |
sort(dictionary.begin(), dictionary.end()); | |
for (int i = 0; i < dictionary.size(); i++) | |
{ | |
if (word == dictionary[i]) | |
{ | |
return i + 1; | |
} | |
} | |
} | |
int main(void) | |
{ | |
cout << solution("I") << "\n"; | |
return 0; | |
} |

개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형