-
[C++] ํ๋ก๊ทธ๋๋จธ์ค 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด์ญ - ๋ถ๋ ์ฌ์ฉ์์ฝ๋ฉํ ์คํธ/์ฝ๋ฉํ ์คํธ ์ฐ์ต 2020. 10. 27. 18:23728x90
(1์๊ฐ ๋ฐ) DFS ์ฐ๋ฉด ๋น์ฐํ ์๊ฐ์ด๊ณผ ๋ ์ค ์์๋๋ฐ ํต๊ณผ. ๊ฒฝ์ฐ์ ์๋ง ๊ตฌํ๋ ๋ฐฉ๋ฒ์ด ์์ ์ค ์์๋ค..
๊ฐ์ ์กฐํฉ์ธ์ง ํ์ธํ ๋ ค๊ณ ์ ๋ ฌ ํ ํ๋์ string์ผ๋ก ๋ฌถ์ด์ ์ ์ฅํ๋๋ฐ ๋ค๋ฅธ ์ฌ๋ ํ์ด๋ฅผ ๋ณด๋๊น user_id ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ 8๊น์ง์ธ๊ฑธ ์ด์ฉํ๋ฉด ํจ์ฌ ์ฝ๊ฒ ํ ์ ์๋๊ฑธ ์์๋ค.
#include <string> #include <vector> #include <iostream> #include <set> #include <algorithm> using namespace std; vector<vector<string>> mapping; set<string> answer; void combineID(int idx, vector<string> combine) { if (idx == mapping.size()) { sort(combine.begin(), combine.end()); string tmp = ""; for (int i = 0; i < combine.size(); i++) { tmp += combine[i]; } answer.insert(tmp); return; } for (int i = 0; i < mapping[idx].size(); i++) { bool check = true; for (int j = 0; j < combine.size(); j++) { if (mapping[idx][i] == combine[j]) { check = false; break; } } if (check) { combine.push_back(mapping[idx][i]); combineID(idx + 1, combine); combine.pop_back(); } } } int solution(vector<string> user_id, vector<string> banned_id) { for (int i = 0; i < banned_id.size(); i++) { vector<string> tmp; for (int j = 0; j < user_id.size(); j++) { if (banned_id[i].size() != user_id[j].size()) continue; bool check = true; for (int k = 0; k < banned_id[i].size(); k++) { if (banned_id[i][k] != user_id[j][k]) { if (banned_id[i][k] != '*') { check = false; break; } } } if (check) { tmp.push_back(user_id[j]); } } mapping.push_back(tmp); } vector<string> combine; combineID(0, combine); return answer.size(); }
728x90'์ฝ๋ฉํ ์คํธ > ์ฝ๋ฉํ ์คํธ ์ฐ์ต' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ