65)最短的单词编码
class Solution {
public:
int minimumLengthEncoding(vector<string>& words) {
unordered_set<string> good(words.begin(), words.end());
for(auto word : words){
for(int k=1; k<word.size(); k++){
good.erase(word.substr(k));
}
}
int ans = 0;
for(auto str : good){
ans += str.size()+1;
}
return ans;
}
};
66)单词之和
class MapSum {
public:
unordered_map<string, int> map;
/** Initialize your data structure here. */
MapSum() {}
void insert(string key, int val) {
map[key] =val;
}
int sum(string prefix) {
int ans = 0;
for(auto [key, val] : map){
if(key.substr(0, prefix.size()) == prefix) ans += val;
}
return ans;
}
};
67)最大的异或
class Solution{
public:
int findMaximumXOR(vector<int>& nums){
int HIGHBIT = 30;
int x = 0;
for(int k=HIGHBIT; k>=0; k--){
unordered_set<int> seen;
for(auto num : nums){
seen.insert(num>>k);
}
int x_next = x*2 + 1;
bool found = false;
for(auto& num :nums){
if(seen.count(x_next^(num>>k))) {
found = true;
break;
}
}
if(found) x = x_next;
else x = x_next-1;
}
return x;
}
};