문제 링크입니다: https://programmers.co.kr/learn/courses/30/lessons/64064
코딩테스트 연습 - 불량 사용자
개발팀 내에서 이벤트 개발을 담당하고 있는 "무지"는 최근 진행된 카카오이모티콘 이벤트에 비정상적인 방법으로 당첨을 시도한 응모자들을 발견하였습니다. 이런 응모자들을 따로 모아 불량
programmers.co.kr
알고리즘은 아래와 같습니다.
1. 불량 사용자 아이디에 해당하는 유저 아이디의 인덱스들을 벡터 배열 v에 넣어줍니다.
2. func 재귀 함수를 통해 나올 수 있는 유저 아이디의 인덱스 조합들을 정렬한 상태로 set에 넣어줍니다.
3. 2번에서 구한 set의 크기를 반환해주면 끝
This file contains 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 <string> | |
#include <vector> | |
#include <set> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
const int MAX = 8; | |
int N; | |
set<string> result; | |
vector<int> v[MAX]; | |
bool isIdentical(string userId, string bannedId) | |
{ | |
if (userId.length() != bannedId.length()) | |
{ | |
return false; | |
} | |
for (int i = 0; i < userId.length(); i++) | |
{ | |
if (bannedId[i] != '*' && userId[i] != bannedId[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
void func(int idx, string s, set<int> visited) | |
{ | |
if (idx == N) | |
{ | |
sort(s.begin(), s.end()); | |
result.insert(s); | |
return; | |
} | |
for (int nameIdx : v[idx]) | |
{ | |
if (visited.find(nameIdx) == visited.end()) | |
{ | |
char c = nameIdx + '0'; | |
visited.insert(nameIdx); | |
func(idx + 1, s + c, visited); | |
visited.erase(nameIdx); | |
} | |
} | |
} | |
int solution(vector<string> user_id, vector<string> banned_id) { | |
N = banned_id.size(); | |
for (int i = 0; i < banned_id.size(); i++) | |
{ | |
for (int j = 0; j < user_id.size(); j++) | |
{ | |
if (isIdentical(user_id[j], banned_id[i])) | |
{ | |
v[i].push_back(j); | |
} | |
} | |
} | |
set<int> visited; | |
func(0, "", visited); | |
return result.size(); | |
} |

개발환경: Programmers IDE
지적, 조언, 질문 환영합니다! 질문 남겨주세요~
반응형
'알고리즘 > programmers' 카테고리의 다른 글
[Programmers] 최적의 행렬 곱셈 (0) | 2022.06.25 |
---|---|
[Programmers] 사라지는 발판 (0) | 2022.06.25 |
[Programmers] 선입 선출 스케줄링 (0) | 2022.06.21 |
[Programmers] 최고의 집합 (0) | 2022.06.21 |
[Programmers] 야근 지수 (0) | 2022.06.21 |