820. 單詞的壓縮編碼

題目
類型:字典樹
難度:中等
題意:快速找出所有後綴。

class Solution {
public:
    int minimumLengthEncoding(vector<string>& words) {
        //32020.3.28 美學暴力 待字典樹優化
        unordered_set<string> good(words.begin(), words.end());
        for(auto word: words){
            for(int i = 1; i < word.size(); i++){
                good.erase(word.substr(i)); //從集合中刪除後綴
            }
        }
        int res = 0;
        for(auto w: good){
            res += w.size() + 1;
        }
        return res;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章