题目:
分析:
可以采用前缀树思路解决
代码:
class Solution {
private:
Solution* next[26] = { nullptr };
int k=0;//不是结尾位0 是结尾为1.
public:
Solution() {}
void insert(const string& word,int &c)//插入单词
{
Solution* root = this;
//尾部符号个数的计算
int d=0;
int dd=0;
if(this->next[word[0] - 'a'] == nullptr) d=1;
for (const auto& w : word) {
if (root->next[w - 'a'] == nullptr)
{
d++;
c++;
if(root->k==1) {
d=-(1<<30);root->k=0;
}
root->next[w - 'a'] = new Solution();
}
else dd++;
root = root->next[w - 'a'];
}
cout<<d<<" "<<dd<<" "<<endl;
for(int i=0;i<26;i++)
{
if(root->next[i]!=nullptr) return;
}
root->k=1;
if(d>0) {
c++;
c=c+dd;
}
}
int minimumLengthEncoding(vector<string>& words) {
//["time", "atime", "btime"]
/*vector<string> words;
words.push_back("btime");
words.push_back("atime");
words.push_back("time");*/
int c=0;
for(auto& word : words)
{
reverse(word.begin(),word.end());
insert(word,c);
}
return c;
}
};