반응형
set에 넣어서 자동 정렬되게끔 사전을 만들어준뒤에
정렬된 사전에서 찾으면 된다.
#include <string>
#include <set>
#include <vector>
using namespace std;
char word[5] = {'A', 'E', 'I', 'O', 'U'};
set<string> words;
void dfs(string s) {
if(s.length() >= 5)
return;
for(int i = 0; i < 5; i++) {
dfs(s + word[i]);
words.insert(s + word[i]);
}
}
int solution(string word) {
int answer = 0;
dfs("");
for(auto x : words) {
answer++;
if(word == x)
break;
}
return answer;
}
다시 풀었다. DFS 활용 감을 익히는중..!
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
unordered_map<string, int> umap;
int cnt = 0;
vector<char> v = {'A', 'E', 'I', 'O', 'U'};
void dfs(int count, string str, string word) {
if(count == 6) {
return;
}
if(!str.empty()) {
cnt++;
umap[str] = cnt;
}
for(int i = 0; i < 5; i++) {
dfs(count + 1, str + v[i], word);
}
}
int solution(string word) {
int answer = 0;
dfs(0, "", word);
return umap[word];
}
반응형
'[알고리즘 + 자료구조] > [프로그래머스]' 카테고리의 다른 글
프로그래머스 쿼드 압축 후 세기 (0) | 2023.08.10 |
---|---|
프로그래머스 2개 이하로 다른 비트 (0) | 2023.08.10 |
프로그래머스 택배 상자 C++ (0) | 2023.08.10 |
프로그래머스 땅따먹기 (0) | 2023.08.09 |
프로그래머스 예상 대진표 (0) | 2023.08.09 |