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. explain查看sql执行计划

    http://www.cnblogs.com/wolf-sun/p/5291563.html 一该命令作用:该命令会向您展示查询是如何被执行的. 1.各个项的含义:https://blog.csdn. ...

  2. 原生http模块与使用express框架对比

    node的http创建服务与利用Express框架有何不同 原生http模块与使用express框架对比: const http = require("http"); let se ...

  3. java常用类总结

    0.jar包下载地点 http://mvnrepository.org/ 1.序列化反序列化Object代码 百度云:http://pan.baidu.com/disk/home#list/path= ...

  4. 527D Clique Problem 判断一维线段没有两辆相交的最大线段数量

    这题说的是给了n个位置 在x轴上 每个位置有一个权值为wi,然后将|xi - xj|>=wi+wj ,满足这个条件的点建一条边,计算着整张图中有多少多少个点构成的子图,使得这个子图的节点数尽量的 ...

  5. testng使用DataProvider+Excel实现DDT

    DDT,即数据驱动测试 Data Driver Test,我曾经记录了一篇关于python的DDT框架(Excel+DDT数据驱动实例),那么java中的DDT是怎么样的呢?在java中,可以用tes ...

  6. 数据仓库原理<3>:数据仓库与ODS

    1. 引言 本篇主要讲述操作数据存储(ODS)系统产生的背景.定义.特点,以及它与数据仓库的区别. 在前两篇,笔者介绍了什么是数据仓库?为什么需要数据仓库?数据仓库系统的体系结构是什么?因此可能在读者 ...

  7. 论文笔记之《Event Extraction via Dynamic Multi-Pooling Convolutional Neural Network》

    1. 文章内容概述 本人精读了事件抽取领域的经典论文<Event Extraction via Dynamic Multi-Pooling Convolutional Neural Networ ...

  8. MongoDB— 细说操作

    基本操作 由于是开篇,就大概的说下基本的“增删查改“,我们再开一个cmd,输入mongo命令打开shell,其实这个shell就是mongodb的客户端, 同时也是一个js的编译器,默认连接的是“te ...

  9. python如何序列化json数据

    使用json模块提供的loads方法和dumps方法,可以很方便的载入和读取json数据格式.而在具体实际应用中,我们使用python数据格式是 string.list 或dict等,这类格式如何直接 ...

  10. Python Web学习笔记之并发编程的孤儿进程与僵尸进程

    1.前言 之前在看<unix环境高级编程>第八章进程时候,提到孤儿进程和僵尸进程,一直对这两个概念比较模糊.今天被人问到什么是孤儿进程和僵尸进程,会带来什么问题,怎么解决,我只停留在概念上 ...