Given a list of words, we may encode it by writing a reference string S and a list of indexes A.

For example, if the list of words is ["time", "me", "bell"], we can write it as S = "time#bell#" and indexes = [0, 2, 5].

Then for each index, we will recover the word by reading from the reference string from that index until we reach a "#" character.

What is the length of the shortest reference string S possible that encodes the given words?

Example:

Input: words = ["time", "me", "bell"]
Output: 10
Explanation: S = "time#bell#" and indexes = [0, 2, 5].

Note:

  1. 1 <= words.length <= 2000.
  2. 1 <= words[i].length <= 7.
  3. Each word has only lowercase letters.

Runtime: 72 ms, faster than 65.22% of C++ online submissions for Short Encoding of Words.

#include <vector>
#include <string>
#include <algorithm>
using namespace std; struct TrieNode {
string word;
TrieNode* children[];
TrieNode(){
for(int i=; i<; i++) {
children[i] = nullptr;
}
}
}; class Trie {
public:
TrieNode* root;
void buildTrie(vector<string> words){
root = new TrieNode();
for(auto w : words) {
TrieNode* tmproot = root;
for(int i=w.size()-; i>=; i--) {
int idx = w[i] - 'a';
if(!tmproot->children[idx]) {
tmproot->children[idx] = new TrieNode();
}
tmproot = tmproot->children[idx];
}
tmproot->word = w;
}
}
}; class Solution {
public:
int minimumLengthEncoding(vector<string>& words) {
Trie trie = Trie();
trie.buildTrie(words);
TrieNode* root = trie.root;
vector<string> ret;
getroot2leaflength(root, ret);
int val = ;
for(int i=; i<ret.size(); i++) {
val += ret[i].size() + ;
}
return val;
}
void getroot2leaflength(TrieNode* root, vector<string>& ret) {
bool leaf = true;
for(int i=; i<; i++) {
if(root->children[i]) {
leaf = false;
getroot2leaflength(root->children[i], ret);
}
}
if(leaf) ret.push_back(root->word);
}
};

LC 820. Short Encoding of Words的更多相关文章

  1. 【leetcode】820. Short Encoding of Words

    题目如下: 解题思路:本题考查就是找出一个单词是不是另外一个单词的后缀,如果是的话,就可以Short Encode.所以,我们可以把words中每个单词倒置后排序,然后遍历数组,每个元素只要和其后面相 ...

  2. 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...

  3. [Swift]LeetCode820. 单词的压缩编码 | Short Encoding of Words

    Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...

  4. [LeetCode] Short Encoding of Words 单词集的短编码

    Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...

  5. Short Encoding of Words

    2018-07-02 09:48:48 问题描述: 问题求解: 方法一.问题给了规模n = 2000,也就是说用BF在O(n^2)的时间复杂度可以过,因此,第一个方法就是BF,但是需要注意的是这里已经 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  8. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  9. Get RSA public key ASN.1 encode from a certificate in DER format

    RSA public key ASN.1 encode is defined in PKCS#1 as follows: RSAPublicKey :: = SEQUENCE  {     modul ...

随机推荐

  1. 【转】在 Delphi 中创建 Linux 守护程序(服务进程)

    转自波哥的译文,必须转过来,太有价值了!原文地址在这里.以下为原文内容: 本文译自 原文链接,语言上做了精炼和排版的变更,以便更简洁明了. Delphi 开始支持 Linux 平台为 Delphi 开 ...

  2. JavaSpring【七、AspectJ】

    AspectJ 概念 @AspectJ类似纯Java注解的普通Java类 Spring可以使用AspectJ来作为切入点 AOP在运行时仍是纯SpringAOP,对AspectJ无依赖 配置: 对@A ...

  3. Maven新建项目出现 Could not calculate build plan:plugin 错误解决办法

    删除本地.m2仓库中 org.apache.maven.plugins:maven-resources-plugin所在目录. 然后右击项目 Maven->Update Project-> ...

  4. linux在配置菜单中添加选项

  5. 初级文件IO——若干种文件共享操作 如何影响 文件文件描述符表

    同一进程共享操作相同的文件 在同一个进程中多次open打开同一文件时,文件描述符可能会相同吗? 答:不可能.在同一进程里面,一旦某个文件描述符被用了,在close释放之前,别人不可能使用,所以指向同一 ...

  6. 十六, k8s集群资源需求和限制, 以及pod驱逐策略。

    目录 容器的资源需求和资源限制 QoS Classes分类 Guaranteed Burstable Best-Effort kubernetes之node资源紧缺时pod驱逐机制 Qos Class ...

  7. Hadoop_27_MapReduce_运营商原始日志增强(自定义OutputFormat)

    1.需求: 现有一些原始日志需要做增强解析处理,流程: 1. 从原始日志文件中读取数据(日志文件:https://pan.baidu.com/s/12hbDvP7jMu9yE-oLZXvM_g) 2. ...

  8. 【Linux常用命令】Linux kill, killall, kill -9,

    参考链接 https://blog.csdn.net/zong596568821xp/article/details/77899454 kill + PID kill -9 + PID  加上-9 是 ...

  9. D. Connected Components Croc Champ 2013 - Round 1 (并查集+技巧)

    292D - Connected Components D. Connected Components time limit per test 2 seconds memory limit per t ...

  10. 如何将html页面导出word格式?

    近期做的项目也是奇葩,页面上需要导出excel,pdf,还有导出图片等...,刚把前几个怂好,还有下载成word文件,如何处理? function getOutword (id, fileName) ...