2023. 6. 4. 19:34ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐จโ๐ปํ์ด ๊ณผ์
๋ฐฑํธ๋ํน(Backtracking)์ ์ด์ฉํด ์ฌ์ ์์๋๋ก ๋จ์ด๋ฅผ ์กฐํฉํ๋ ๋ฐฉ์์ผ๋ก ํ์์ต๋๋ค. 1)์ํ๋ฒณ์ ํ๋์ฉ ๋ถ์ด๊ณ ๋ผ๋ ๊ณผ์ ์์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ์ฆ๊ฐ์ํค๊ณ , 2)์ํ๋ ๋จ์ด๋ฅผ ์ฐพ์ผ๋ฉด ํ์์ ์ค์งํ๋๋ก ๊ตฌํํ์์ต๋๋ค.
์ฌ๊ท(Recursion)์ ๋ฐฑํธ๋ํน(Backtracking) ๊ฐ๋ ์ด ์ฒ์์๋ ์ต์ํ์ง ์์, ์ฝ๋๋ก ๊ตฌํํ๊ธฐ ์ด๋ ค์ ๋๋ฐ ์ ์ ์ฐ์ตํ๋ค ๋ณด๋ ์ต์ํด ์ง๋ ๊ฒ ๊ฐ์ต๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <string>
#include <vector>
using namespace std;
const int VOWEL_NUM = 5;
const char vowel[VOWEL_NUM] = { 'A', 'E', 'I', 'O', 'U' };
bool isFindingWord = false;
void CreateCombinationWords(const string& word, string targetWord, int& index)
{
if (targetWord.length() == VOWEL_NUM)
return;
for (int i = 0; i < VOWEL_NUM; i++)
{
targetWord.push_back(vowel[i]);
index++;
if (targetWord.compare(word) == 0)
{
isFindingWord = true;
break;
}
CreateCombinationWords(word, targetWord, index);
if (isFindingWord)
break;
targetWord.pop_back();
}
}
int GetWordIndex(const string& word)
{
int index = 0;
string targetWord = "";
CreateCombinationWords(word, targetWord, index);
return index;
}
int solution(string word) {
return GetWordIndex(word);
}

'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋ง๋ฒ์ ์๋ฆฌ๋ฒ ์ดํฐ | C++ (0) | 2023.06.05 |
---|---|
[Programmers] Lv2. ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ | C++ (0) | 2023.06.04 |
[Programmers] Lv2. ํธํ ๋์ค (0) | 2023.06.03 |
[Programmers] Lv2. ๋ฏธ๋ก ํ์ถ | C++ (1) | 2023.06.03 |
[Programmers] Lv2. ๋ฌด์ธ๋ ์ฌํ | C++ (0) | 2023.06.03 |