불량사용자

김인회·2021년 5월 5일
0

알고리즘

목록 보기
31/53

코드


#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;
// 2^24
bool bitmask[16777217];

void set_bit(int &mask, int index) {
    int value = 7 << (index * 3);
    mask = mask | value;
}

int bitmasking(vector<int> nums) {
    int mask = 0;
    for (int i = 0; i < nums.size(); i++) {
        set_bit(mask, nums[i]);
    }
    return mask;
}

bool is_sameStr(string a, string b) {
    if (a.size() != b.size()) return false;
    for (int i = 0; i < a.size(); i++) {
        if (b[i] == '*') continue;
        if (a[i] != b[i]) return false;
    }
    return true;
}

vector<vector<int>> checking(vector<string> &user_id, vector<string> &banned_id) {
    vector<vector<int>> ret(banned_id.size());
    for (int i = 0; i < banned_id.size(); i++) {
        for (int j = 0; j < user_id.size(); j++) {
            if (is_sameStr(user_id[j], banned_id[i])) {
                ret[i].push_back(j);
            }
        }
    }
    return ret;
}

void switch_num(vector<vector<int>> &element, map<int, bool> &m, int &answer, int index ) {
    if (index == element.size()) {
        if (m.size() == element.size()) {
            vector<int> temp;
            for (auto iter = m.begin(); iter != m.end(); iter++) {
                temp.push_back(iter->first);
            }
            int bit = bitmasking(temp);
            if (bitmask[bit] == false) {
                bitmask[bit] = true;
                answer++;
            }
        }
        return;
    }

    for (int i = 0; i < element[index].size(); i++) {
        if (m.find(element[index][i]) == m.end()) {
            m[element[index][i]] = true;
            switch_num(element, m, answer, index + 1);
            m.erase(element[index][i]);
        }
    }

}

int solution(vector<string> user_id, vector<string> banned_id) {
    int answer = 0;
    vector<vector<int>> element = checking(user_id, banned_id);
    map<int, bool> m;
    switch_num(element, m, answer, 0);
    return answer;
}

int main() {
    vector<string> a = { "frodo", "fradi", "crodo", "abc123", "frodoc" };
    vector<string> b = { "fr*d*", "*rodo", "******", "******" };
 
    cout << solution(a, b);

}
profile
안녕하세요. 잘부탁드립니다.

0개의 댓글