标签:lse longest image set solution long bsp ict prefix
思路:Brute force + pruning
用不用set来存储输入的words都可以。
class Solution { public: string longestWord(vector<string>& words) { string best; //存储当前最优解 //unordered_set<string> dict(words.begin(), words.end()); for(const string& word: words){ //pruning if(word.length()<best.length() || (word.length()==best.length()&& word>best)) continue; string prefix; bool valid = true; for(int i=0; i<word.length()-1 && valid; ++i){ prefix += word[i]; if(find(words.begin(), words.end(), prefix)==words.end()) //if(!dict.count(prefix)) valid = false; } if(valid) best = word; } return best; } };
leetcode 720 - Longest Word in Dictionary
标签:lse longest image set solution long bsp ict prefix
原文地址:https://www.cnblogs.com/Bella2017/p/11149677.html