[프로그래머스] 불량 사용자
풀이 1
#include <string>
#include <vector>
#include <set>
#include <iostream>
using namespace std;
set<int> ans;
vector<string> user_id_g;
vector<string> banned_id_g;
int bitCount(int x) {
if (x == 0) return 0;
return (x % 2) + bitCount(x / 2);
}
bool match(string a, string b) {
if (a.length() != b.length()) return false;
for (int i = 0; i < a.length(); ++i) {
if (a[i] == '*' || b[i] == '*') continue;
if (a[i] != b[i]) return false;
}
return true;
}
void dp(int banned_index, int bitmask) {
if (banned_id_g.size() == banned_index) {
if (bitCount(bitmask) == banned_id_g.size()) {
ans.insert(bitmask);
}
return;
}
for (int i = 0; i < user_id_g.size(); ++i) {
if (match(user_id_g[i], banned_id_g[banned_index])) {
if ((bitmask & (1 << i)) == 0) {
dp(banned_index + 1, bitmask | (1 << i));
}
}
}
}
int solution(vector<string> user_id, vector<string> banned_id) {
user_id_g = user_id;
banned_id_g = banned_id;
int answer = 0;
dp(0, 0);
answer = ans.size();
return answer;
}
풀이 2
#include <string>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool isPossible(string u_id, string b_id){
if(u_id.length() != b_id.length()) return false;
for(int i = 0; i<b_id.length(); ++i){
if(b_id[i] == '*') continue;
if(b_id[i] != u_id[i]) return false;
}
return true;
}
int solution(vector<string> user_id, vector<string> banned_id) {
int n = user_id.size();
int m = banned_id.size();
vector<vector<string>> possible_banned_id(m);
for(int i = 0; i<m; ++i){
string b_id = banned_id[i];
for(int j = 0; j<n; ++j){
string u_id = user_id[j];
if(isPossible(u_id, b_id)) possible_banned_id[i].push_back(u_id);
}
}
for(int i = 0; i<m; ++i){
if(possible_banned_id[i].empty()) return 0;
}
set<set<string>> answerSet;
set<string> possible_answer;
for(int a = 0; a< possible_banned_id[0].size(); ++a){
string stra = possible_banned_id[0][a];
possible_answer.insert(stra);
if(m == 1){
answerSet.insert(possible_answer);
possible_answer.erase(stra);
continue;
}
for(int b = 0; b < possible_banned_id[1].size(); ++b){
string strb = possible_banned_id[1][b];
if(possible_answer.find(strb) != possible_answer.end()) continue;
possible_answer.insert(strb);
if(m == 2){
answerSet.insert(possible_answer);
possible_answer.erase(strb);
continue;
}
for(int c = 0; c < possible_banned_id[2].size(); ++c){
string strc = possible_banned_id[2][c];
if(possible_answer.find(strc) != possible_answer.end()) continue;
possible_answer.insert(strc);
if(m == 3){
answerSet.insert(possible_answer);
possible_answer.erase(strc);
continue;
}
for(int d = 0; d < possible_banned_id[3].size(); ++d){
string strd = possible_banned_id[3][d];
if(possible_answer.find(strd) != possible_answer.end()) continue;
possible_answer.insert(strd);
if(m == 4){
answerSet.insert(possible_answer);
possible_answer.erase(strd);
continue;
}
for(int e = 0; e < possible_banned_id[4].size(); ++e){
string stre = possible_banned_id[4][e];
if(possible_answer.find(stre) != possible_answer.end()) continue;
possible_answer.insert(stre);
if(m == 5){
answerSet.insert(possible_answer);
possible_answer.erase(stre);
continue;
}
for(int f = 0; f < possible_banned_id[5].size(); ++f){
string strf = possible_banned_id[5][f];
if(possible_answer.find(strf) != possible_answer.end()) continue;
possible_answer.insert(strf);
if(m == 6){
answerSet.insert(possible_answer);
possible_answer.erase(strf);
continue;
}
for(int g = 0; g < possible_banned_id[6].size(); ++g){
string strg = possible_banned_id[6][g];
if(possible_answer.find(strg) != possible_answer.end()) continue;
possible_answer.insert(strg);
if(m == 7){
answerSet.insert(possible_answer);
possible_answer.erase(strg);
continue;
}
for(int h = 0; h < possible_banned_id[7].size(); ++h){
string strh = possible_banned_id[7][h];
if(possible_answer.find(strh) != possible_answer.end()) continue;
possible_answer.insert(strh);
answerSet.insert(possible_answer);
possible_answer.erase(strh);
}
possible_answer.erase(strg);
}
possible_answer.erase(strf);
}
possible_answer.erase(stre);
}
possible_answer.erase(strd);
}
possible_answer.erase(strc);
}
possible_answer.erase(strb);
}
possible_answer.erase(stra);
}
for(auto it = answerSet.begin(); it != answerSet.end(); ++it){
if(it->size() != m) answerSet.erase(it);
}
int answer = answerSet.size();
return answer;
}