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. redis __详解 (转载自作者:孤独烟 出处: http://rjzheng.cnblogs.com/)

    https://www.cnblogs.com/rjzheng/p/9096228.html [原创]分布式之redis复习精讲 引言 为什么写这篇文章? 博主的<分布式之消息队列复习精讲> ...

  2. 【Day3】3.提取商城分类结构

    import re with open('index.html','r',encoding='utf-8') as f: html = re.sub('\n','',f.read()) section ...

  3. Delphi 编写线程函数

  4. mmap:内存映射文件

    介绍 建立一个文件的内存映射将使用操作系统虚拟内存来直接访问文件系统上的数据,而不是使用常规的I/O函数访问数据. 内存映射通常可以提高I/O性能,因为使用内存映射时,不需要对每一个访问都建立一个单独 ...

  5. git 账号密码

    由于git迁移服务地址,而导致无法登陆 首先  git config --system --unset credential.helper  然后执行 git config --global cred ...

  6. anaconda环境---ubuntu下重装

    anaconda环境---ubuntu下重装 @wp20190312 为何重装? 配置一个环境,意外发现conda命令不好用了,提示“找不到conda模块”,整个conda虚拟环境中的工程项目无法使用 ...

  7. Django form表单修改数据

    form: #!/usr/bin/env python #coding:utf8 from django.forms import Form,ModelForm import models class ...

  8. 设置IDEA中各种线条颜色

    在IDEA之中, 如同TearLine, 行号, 搜索结果高亮, Caret Line, 方法分隔线颜色的设置的操作方法如下: 操作路径: 1.Ctrl+Alt+S, 打开设置:   settings ...

  9. MySQL--------SQL优化审核工具实战

    1. 背景 SQLAdvisor是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具.它基于MySQL原生态词法解析,结合分析SQL中的where条件.聚合条件. ...

  10. Elasticsearch:运用scroll接口对大量数据实现更好的分页

    在Elasticsearch中,我们可以通过size和from来对我们的结果来进行分页.但是对于数据量很大的索引,这是有效的吗?Scroll API可用于从单个搜索请求中检索大量结果(甚至所有结果), ...