-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path318.cpp
59 lines (59 loc) · 1.87 KB
/
318.cpp
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
__________________________________________________________________________________________________
sample 28 ms submission
class Solution {
public:
int getId(string &word,int *ids){
int k=0;
for(auto c:word)
if((k&ids[c-'a'])==0)
k |= ids[c-'a'];
return k;
}
int maxProduct(vector<string>& words) {
std::ios::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
int ids[26];
for(int i=0;i<26;i++){
ids[i]=1<<i;
}
int n = words.size();
vector<int> id(n,0);
for(int i=0;i<n;i++){
id[i]=getId(words[i],ids);
}
int res = 0;
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
if((id[i]&id[j])==0){
int l=words[i].length()*words[j].length();
if(l>res)
res = l;
}
return res;
}
};
__________________________________________________________________________________________________
sample 13656 kb submission
class Solution {
public:
int maxProduct(vector<string>& words) {
if (words.empty()) {
return 0;
}
vector<int> mask(words.size(), 0);
for (int i = 0; i < words.size(); ++i) {
for (auto c : words[i]) {
mask[i] = mask[i] | (1 << static_cast<int>(c - 'a'));
}
}
int max_product = 0;
for (int i = words.size() - 1; i >= 0; --i) {
for (int j = i - 1; j >= 0; --j) {
if (!(mask[i] & mask[j])) {
max_product = max(max_product, static_cast<int>(words[i].size()) * static_cast<int>(words[j].size()));
}
}
}
return max_product;
}
};
__________________________________________________________________________________________________