Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9841d4c

Browse files
committed
Lv5_후보키
1 parent 8ef075e commit 9841d4c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

‎Programmers/Lv5/Lv5_후보키.cpp‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
#include <set>
5+
#include <algorithm>
6+
using namespace std;
7+
8+
bool hkey_able(vector<int> &answer, int now) {
9+
for (int i = 0; i < answer.size(); i++) {
10+
if ((answer[i] & now) == answer[i])
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
bool cmp_hkey(pair<int, int> &p1, pair<int, int> &p2) {
17+
if (p1.second > p2.second)
18+
return true;
19+
else if (p1.second == p2.second) {
20+
if (p1.first < p2.first)
21+
return true;
22+
else
23+
return false;
24+
}
25+
else
26+
return false;
27+
}
28+
29+
int solution142(vector<vector<string>> relation) {
30+
vector<int> answer;
31+
int asize = relation.front().size(); // 속성의 갯수
32+
int rsize = relation.size(); // row 갯수
33+
34+
vector<pair<int, int>> hkey; // { 속성번호, 유니크 값 갯수 }
35+
36+
for (int i = 0; i < asize; ++i) {
37+
set<string> s;
38+
for (int j = 0; j < rsize; ++j)
39+
s.insert(relation[j][i]);
40+
hkey.push_back({ i, s.size() });
41+
}
42+
sort(hkey.begin(), hkey.end(), cmp_hkey); // 유니크 값 크기순 정렬
43+
44+
for (int i = 1; i < (1 << asize); ++i) {
45+
int ukey = 1;
46+
47+
for (int k = 0; k < asize; k++) {
48+
if (i&(1 << k))
49+
ukey *= hkey[k].second;
50+
}
51+
52+
if (!hkey_able(answer, i) || ukey < rsize) // 계산할 필요없다면
53+
continue;
54+
else { // 정답이 될 수 있는지 확인
55+
set<string> ss;
56+
for (int j = 0; j < rsize; ++j) {
57+
string t = "";
58+
for (int k = 0; k < asize; k++) {
59+
if (i&(1 << k))
60+
t += relation[j][hkey[k].first];
61+
}
62+
ss.insert(t);
63+
}
64+
if (ss.size() == rsize)
65+
answer.push_back(i);
66+
}
67+
}
68+
69+
return answer.size();
70+
}

‎Programmers/Programmers.vcxproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@
161161
<ClCompile Include="Lv5\Lv5_캐시.cpp" />
162162
<ClCompile Include="Lv5\Lv5_파일명정렬.cpp" />
163163
<ClCompile Include="Lv5\Lv5_프렌즈4블록.cpp" />
164+
<ClCompile Include="Lv5\Lv5_후보키.cpp" />
164165
<ClCompile Include="라인코딩테스트\Line_1번.cpp" />
165166
<ClCompile Include="라인코딩테스트\Line_2번.cpp" />
166167
<ClCompile Include="라인코딩테스트\Line_3번.cpp" />

‎Programmers/Programmers.vcxproj.filters‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,5 +465,8 @@
465465
<ClCompile Include="Lv5\Lv5_무지의먹방라이브.cpp">
466466
<Filter>소스 파일</Filter>
467467
</ClCompile>
468+
<ClCompile Include="Lv5\Lv5_후보키.cpp">
469+
<Filter>소스 파일</Filter>
470+
</ClCompile>
468471
</ItemGroup>
469472
</Project>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /