-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
31 lines (30 loc) · 947 Bytes
/
program.cpp
File metadata and controls
31 lines (30 loc) · 947 Bytes
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
#include "../include/pre.h"
#include <bitset>
int maxProduct(vector<string>& words)
{
typedef std::bitset<26> alphabet;
typedef std::pair<int, alphabet> wordlist;
auto N = words.size();
vector<wordlist> words_;
for (auto& s : words) {
alphabet a;
for (auto c : s) a[c - 'a'] = true;
words_.emplace_back(s.size(), a);
}
int max = 0;
std::sort(words_.begin(), words_.end(), [](wordlist& l, wordlist& r){return l.first > r.first;});
for (size_t i = 0; i < N; i++) {
for (size_t j = i + 1; j < N; j++) {
if (max > words_[i].first * words_[j].first) break;
if ((words_[i].second & words_[j].second).any()) continue;
max = std::max(max, words_[i].first * words_[j].first);
}
}
return max;
}
int main()
{
vector<string> test = {"abcw", "baz", "foo", "bar", "xtfn", "abcdef"};
cout << maxProduct(test) << endl;
return 0;
}