LC 820. 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 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 <= words.length <= 2000.1 <= words[i].length <= 7.- 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的更多相关文章
- 【leetcode】820. Short Encoding of Words
题目如下: 解题思路:本题考查就是找出一个单词是不是另外一个单词的后缀,如果是的话,就可以Short Encode.所以,我们可以把words中每个单词倒置后排序,然后遍历数组,每个元素只要和其后面相 ...
- 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...
- [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 ...
- [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 ...
- Short Encoding of Words
2018-07-02 09:48:48 问题描述: 问题求解: 方法一.问题给了规模n = 2000,也就是说用BF在O(n^2)的时间复杂度可以过,因此,第一个方法就是BF,但是需要注意的是这里已经 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- 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 ...
随机推荐
- 发送短信——java
闲来无事研究一下调用第三方接口发送短信的技术 这一次我们使用阿里的短信服务 一.进行平台相关服务的注册和设置 下面请参照阿里的短信服务文档进行设置,只要按照文档步骤来差不多30分钟就能搞定服务注册: ...
- linecache:高效的读取文本文件
介绍 可以很方便的读取文件 读取特定行 import linecache ''' 我们常用的序列的索引是从0开始的,但是linecache模块读取的文件行号是从1开始的 ''' # 表示读取C:\p ...
- winfrom 自动关闭 重新MessageBox.Show("Test");
复制代码 自动关闭 调用 AutoClosingMessageBox.Show("添加失败", "提示", 1000); #region alert p ...
- Django:常用字段、手动自动第三张表单、元信息
一.常用字段和非常用字段 二.手动,自动创建第三张表 三.元信息 四.defer和only 一.常用字段和非常用字段 -常用字段 AutoField int自增列,必须填入参数 primary_key ...
- Linux一些常用的命令
常见命令 cd命令 cd命令用来切换工作目录至dirname, 其中dirName表示法可为绝对路径或相对路径. pwd命令 pwd命令以绝对路径的方式显示用户当前工作目录. ls命令 ls命令用来显 ...
- 编码问题2 utf-8和Unicode的区别
utf-8和Unicode到底有什么区别?是存储方式不同?编码方式不同?它们看起来似乎很相似,但是实际上他们并不是同一个层次的概念 要想先讲清楚他们的区别,首先应该讲讲Unicode的来由. 众所周知 ...
- Luogu P4198 楼房重建 分块 or 线段树
思路:分块 提交:2次(第一次的求解有问题) 题解: 设块长为$T$,我们开$N/T$个单调栈,维护每一块的上升斜率. 修改时暴力重构整个块,$O(T)$ 求解时记录一个最大斜率$lst$,然后块内二 ...
- python中global的用法——再读python简明教程
今天看了知乎@萧井陌的编程入门指南,想重温一下 <python简明教程>,对global的用法一直不太熟练,在此熟练一下,并实践一下python中list.tuple.set作为参数的区别 ...
- 积性函数,线性筛入门 HDU - 2879
HDU - 2879HeHe 题意:He[N]为[0,N−1]范围内有多少个数满足式子x2≡x (mod N),求HeHe[N]=He[1]×……×He[N] 我是通过打表发现的he[x]=2k,k为 ...
- 北京清北 综合强化班 Day5
T1 思路: 输入数据,sort一下, 如果a[i]>sum+1(前缀和) 那么sum+1就一定不会被拼出来, 然后输出即可. 上代码: #include <iostream> #i ...