class Solution { class Trie{ struct TrieNode{ char val; TrieNode *children[26] = {0};//这里不初始化会报错 TrieNode(char val){ this->val = val; } }; private: TrieNode *root; public: Trie(){ root = new TrieNode(' '); } /** Inserts a word into the trie. */ int insert(string word) { TrieNode *cur = root; bool isNew = false; for(int i = word.length() - 1;i >= 0 ;--i){ char c = word[i]; if(cur->children[c - 'a'] == nullptr){ cur->children[c - 'a'] = new TrieNode(c); isNew = true; } cur = cur->children[c - 'a']; } if(isNew)return word.length() + 1; return 0; } };public: static bool compare(string s1, string s2){ return s1.length() > s2.length(); } int minimumLengthEncoding(vector<string>& words) { if(words.empty())return 0; Trie *trie = new Trie(); sort(words.begin(), words.end(), compare); int len = 0; for(string word : words){ len += trie->insert(word); } return len; }};