力扣第820题 单词的压缩编码
class TrieNode
{
public:
map<char, TrieNode*> children;
};
class Solution {
public:
void GetNum(TrieNode * node, int num, int& count)
{
if (node == NULL || node->children.size() == 0)
{
count += num + 1;
return;
}
for (map<char, TrieNode*>::iterator itor = node->children.begin(); itor != node->children.end(); itor++)
{
GetNum(itor->second, 1 + num, count );
}
}
int minimumLengthEncoding(vector<string>& words)
{
TrieNode* root = new TrieNode();
TrieNode* temp;
for (string s : words)
{
temp = root;
for (int i = s.size() - 1; i >= 0; i--)
{
if (temp->children[s[i]] == NULL)
{
temp->children[s[i]] = new TrieNode();
}
temp = temp->children[s[i]];
}
}
int count = 0;
GetNum(root, 0, count);
return count;
}
};