코딩테스트 준비/프로그래머스
[프로그래머스 LV2] - 모음사전(트리) c++
SeoburiFaust
2024. 2. 21. 12:14
문제
https://school.programmers.co.kr/learn/courses/30/lessons/84512
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
접근방법
count를 전역변수로 설정하고,
방문횟수마다 +1을 해서
tree에 저장했다.
이렇게 하면,
각 노드의 자식 노드 개수를 구할 수 있다.
코드
#include <string>
#include <vector>
#include <iostream>
#include <map>
using namespace std;
vector<char> dict = {'A', 'E', 'I', 'O', 'U'};
map<string, int> tree;
int count = 1;
void make_tree(string word) {
for (int i=0;i<5;i++) {
string temp = word + dict[i];
if (temp.size() >= 6) return;
tree[temp] = count;
count++;
make_tree(temp);
}
return;
}
int solution(string word) {
int answer = 1;
make_tree("");
answer = tree[word];
return answer;
}
개선할 점