๋ฌธ์
ํ์ด
#include <string>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
map<string,int> uniqueness_table;
vector<string> uniqueness_vec;
set<string> candidate;
bool cmp(string s1, string s2){
if(s1.length()!=s2.size()) return s1.length()<s2.length();
return s1<s2;
}
void combination(int index, vector<vector<string>> relation, vector<int> pick){
map<string,int>temp;
for(int i=0; i<relation.size(); i++){
string inp = "";
for(int k=0; k<pick.size(); k++) inp += relation[i][pick[k]];
temp[inp]++;
}
if(temp.size() == relation.size()){
string inp = "";
for(int num:pick) inp += to_string(num);
uniqueness_table[inp] = 1;
return;
}
for(int i=index+1; i<relation[0].size(); i++){
pick.push_back(i);
combination(i, relation, pick);
pick.pop_back();
}
}
void minimality_check(){
candidate.insert(uniqueness_vec[0]);
for(int i=1; i<uniqueness_vec.size(); i++){
bool flag = true;
for(auto it = candidate.begin(); it!=candidate.end(); it++){
int cnt = 0;
for(int k=0; k<it->length(); k++){
if(uniqueness_vec[i].find(it->data()[k])!=string::npos) cnt++;
}
if(cnt == it->length()) flag = false;
}
if(flag) candidate.insert(uniqueness_vec[i]);
}
}
int solution(vector<vector<string>> relation) {
for(int i=0; i<relation[0].size(); i++) combination(i, relation, {i});
for(auto it = uniqueness_table.begin(); it!= uniqueness_table.end(); it++) uniqueness_vec.push_back(it->first);
sort(uniqueness_vec.begin(),uniqueness_vec.end(),cmp);
minimality_check();
return candidate.size();
}
ํ์ด
๊น๋ค๋ก์ ์ต๋๋ค.. ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ฌธ์์ด๋ก ๋ฝ๋ ๊ฒ๋ ๊น๋ค๋ก์ ๊ณ , ํนํ ํฌ์์ฑ์ ๊ตฌํ๋ ๋ถ๋ถ์ด ๊ต์ฅํ ๊น๋ค๋ก์ ์ต๋๋ค. ํ๊ธด ํ๋๋ฐ ๋งค์ฐ ์ฐ์ฐํฉ๋๋ค.
๋ค๋ฅธ ์ฌ๋๋ค์ ํ์ด๋ฅผ ๋ณด๋ฉด ๋นํธ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ ํ์๋๋ฐ ์๊ฐ์ด ๋๋ฉด ํ๋ฒ ๋ค์ ๋ณต๊ธฐ๋ฅผ ํด๋ด์ผ๊ฒ ์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฐ ๋ฌธ์ ๋ฅผ ํ๊ณ ๋๋ฉด ํญ์ ํ์ด์ฌ์ผ๋ก ํผ ์ฝ๋๋ฅผ ๋ณด๋๋ฐ ๊ฐ๊ฒฐํจ์ ๋ณด๋ฉด ๊ฐ์ํ์ผ ํ๋ ์ถ์ต๋๋ค.
'Algorithm ๐ง๐ปโ๐ป > ํ๋ก๊ทธ๋๋จธ์ค(Programmers)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ฌ๋ฐ๋ฅธ ๊ดํธ(Level 2) (0) | 2022.04.18 |
---|---|
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ ๋ฐ๋จน๊ธฐ(Level 2) (2) | 2022.04.18 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์ต์๊ฐ ๋ง๋ค๊ธฐ(Level 2) (0) | 2022.04.18 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ์นดํซ(Level 2) (0) | 2022.04.14 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - 124 ๋๋ผ์ ์ซ์(Level 2) (0) | 2022.04.14 |
[c++] ํ๋ก๊ทธ๋๋จธ์ค - ๋ฉ๋ด ๋ฆฌ๋ด์ผ(Level 2) (2) | 2022.04.13 |
๋๊ธ