[Programmers] Lv2. λͺ¨μ μ¬μ | C++
2023. 6. 4. 19:34γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
λ°±νΈλνΉ(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);
}
728x90
λ°μν
'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 |