2018-07-02 09:48:48

问题描述:

问题求解:

方法一、问题给了规模n = 2000,也就是说用BF在O(n^2)的时间复杂度可以过,因此,第一个方法就是BF,但是需要注意的是这里已经非常擦边了,所以需要对常数项进行优化。

    public int minimumLengthEncoding(String[] words) {
boolean[] flag = new boolean[words.length];
int[] lens = new int[words.length];
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
});
int res = 0;
for (int i = 0; i < words.length; i++) {
lens[i] = words[i].length();
for (int j = i + 1; j < words.length; j++) {
int leni = lens[i];
if (lens[j] == 0) lens[j] = words[j].length();
int lenj = lens[j];
if (words[j].substring(lenj - leni).equals(words[i])) {
flag[i] = true;
break;
}
}
}
for (int i = 0; i < words.length; i++)
if (!flag[i]) res += words[i].length() + 1;
return res;
}

方法二、这个方法就比较巧妙了,首先使用Set进行去重,然后对Set中的每个元素将其后缀去除掉,那么最后剩下的就是答案了。

    public int minimumLengthEncoding(String[] words) {
Set<String> s = new HashSet<>(Arrays.asList(words));
for (String w : words)
for (int i = 1; i < w.length(); ++i)
s.remove(w.substring(i));
int res = 0;
for (String w : s) res += w.length() + 1;
return res;
}

Short Encoding of Words的更多相关文章

  1. 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 ...

  2. 【leetcode】820. Short Encoding of Words

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

  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. 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)

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

  6. 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 ...

  7. Swift LeetCode 目录 | Catalog

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

  8. All LeetCode Questions List 题目汇总

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

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

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

随机推荐

  1. c#中ref和out使用及区别

    在c#中,使用方法获得返回值时,只能获取一个返回值.当使用ref和out关键字后,可以获取多个返回值. MSDN对ref和out关键字的说明如下: ref 关键字: 使参数按引用传递.其效果是,当控制 ...

  2. mysql的Navicat查看数据库的ER图

    1.mysql数据库表间的关系图可以通过navicat查看.

  3. PAT乙级 1024. 科学计数法 (20)(未通过全部测试,得分18)

    1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...

  4. Object-C-NSArray

    NSArray *fruitArray=[[NSArray alloc] initWithObjects:@"apple",@"banana",@"p ...

  5. 深入理解 Java 内存模型(一)- 内存模型介绍

    深入理解 Java 内存模型(一)- 内存模型介绍 深入理解 Java 内存模型(二)- happens-before 规则 深入理解 Java 内存模型(三)- volatile 语义 深入理解 J ...

  6. 谷歌浏览器 URL无法访问

    使用谷歌浏览器老是会崩溃,或者访问的时候发现“URL无法访问”等失败的问题,连淘宝都没法访问,这个让人很恼火, 最后在扩展应用那里搜到个URL的redirect,问题解决了,~~发现没有再出现类似问题 ...

  7. linux常用命令:tar 命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  8. 为自己的网站添加Markdown功能 markedjs

    Markdown几个简单的标记可以实现轻量级的代替Word方案 不多说,引入开源库js https://github.com/chjj/marked使用方式简单,如下实例代码: <!DOCTYP ...

  9. Python Web学习笔记之CPU时间片

    时间片即CPU分配给各个程序的时间,每个线程被分配一个时间段,称作它的时间片,即该进程允许运行的时间,使各个程序从表面上看是同时进行的.如果在时 间片结束时进程还在运行,则CPU将被剥夺并分配给另一个 ...

  10. mamcached+magent构建memcached集群

    cat /etc/redhat-release CentOS release 6.7 (Final) 防火墙.selinux 关闭 192.168.12.30 安装libevent和memcached ...