-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1178.cpp
More file actions
60 lines (51 loc) · 1.66 KB
/
1178.cpp
File metadata and controls
60 lines (51 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//leetcode
class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
{
unordered_map<int, int> frequency;
for (const string& word: words)
{
int mask = 0;
for (char ch: word) {
mask |= (1 << (ch - 'a'));
}
if (__builtin_popcount(mask) <= 7) {
++frequency[mask];
}
}
vector<int> ans;
for (const string& puzzle: puzzles)
{
int total = 0;
// 枚举子集方法一
// for (int choose = 0; choose < (1 << 6); ++choose) {
// int mask = 0;
// for (int i = 0; i < 6; ++i) {
// if (choose & (1 << i)) {
// mask |= (1 << (puzzle[i + 1] - 'a'));
// }
// }
// mask |= (1 << (puzzle[0] - 'a'));
// if (frequency.count(mask)) {
// total += frequency[mask];
// }
// }
// 枚举子集方法二
int mask = 0;
for (int i = 1; i < 7; ++i) {
mask |= (1 << (puzzle[i] - 'a'));
}
int subset = mask;
do {
int s = subset | (1 << (puzzle[0] - 'a'));
if (frequency.count(s)) {
total += frequency[s];
}
subset = (subset - 1) & mask;
} while (subset != mask);
ans.push_back(total);
}
return ans;
}
};