作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/concatenated-words/

题目描述

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.
A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

Example:

Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]

Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]

Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".

Note:

  1. The number of elements of the given array will not exceed 10,000
  2. The length sum of elements in the given array will not exceed 600,000.
  3. All the input string will only include lower case letters.
  4. The returned elements order does not matter.

题目大意

如果有个字符串能够由其他的至少两个字符串拼接构成,那么这个字符串符合要求。问总的有哪些字符串符合要求。

解题方法

动态规划

这个题的解题方法就是139. Word Break,如果不把139搞懂的话,这个题是做不出来的。

方法就是对每个字符串进行一次DP,判断这个字符串是不是能用其他的字符串拼接而成。为了加快判断的速度,使用的是字典保存的字符串。

代码如下:

class Solution {
public:
vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
if (words.size() <= 2) return {};
unordered_set<string> wordset(words.begin(), words.end());
vector<string> res;
for (string word : words) {
wordset.erase(word);
const int N = word.size();
if (N == 0) continue;
vector<bool> dp(N + 1, false);
dp[0] = true;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j] && wordset.count(word.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
if (dp.back())
res.push_back(word);
wordset.insert(word);
}
return res;
}
};

参考资料:http://www.cnblogs.com/grandyang/p/6254527.html

日期

2018 年 12 月 18 日 —— 改革开放40周年

【LeetCode】472. Concatenated Words 解题报告(C++)的更多相关文章

  1. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  2. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  3. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  4. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  7. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  8. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  9. Leetcode 115 Distinct Subsequences 解题报告

    Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...

随机推荐

  1. [R] read.table的check.names参数防止读入数据时列名前自动加上"X."

    最近用之前写的R脚本重新跑数据时,出现了报错.经检查,才发现是数据的列名读入R时发生了变化,列名前自动加上了X.符号. read.table系列函数有一个check.names参数,默认为 TRUE ...

  2. snakmake 小练习

    最近在学习snakemake 用于生信流程管理,现在用一个snakemake 来完成小任务:将在某一文件夹下的多个bam文件截取一部分,然后建立索引,在提取出fastq序列,最后比对回基因组. 需要两 ...

  3. 毕业设计之zabbix---自带模板监控mysql内容

    自带模板是不能直接建立连接就可以用的 必须经历一下几步: 建立用户权限: [root@mysql.quan.bbs lib]$mysql -u root -p Enter password: Welc ...

  4. 如何利用nrfjprog.exe读写nrf51的flash

    版权声明:本文为博主原创文章,未经博主允许不得转载.   1.目的 为了方便平时在开发中的调试,验证一些想法是否正确. 2.平台: Jlink version:v5.02c nrf51822硬件板等. ...

  5. (转载) Java多线程技术

    多线程编程一直是学员们比较头痛和心虚的地方,因为线程执行顺序的不可预知性和调试时候的困难,让不少人在面对多线程的情况下选择了逃避,采用单线程的方式,其实只要我们对线程有了明确的认识,再加上java内置 ...

  6. hbase参数调优

    @ 目录 HBase参数调优 hbase.regionserver.handler.count hbase.hregion.max.filesize hbase.hregion.majorcompac ...

  7. C语言把数字转换为字符串的函数

    博主原文 C语言itoa()函数和atoi()函数详解(整数转字符C实现) C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 1.int/float to st ...

  8. 【Git项目管理】分布式 Git - 分布式工作流程

    分布式 Git - 分布式工作流程 你现在拥有了一个远程 Git 版本库,能为所有开发者共享代码提供服务,在一个本地工作流程下,你也已经熟悉了基本 Git 命令.你现在可以学习如何利用 Git 提供的 ...

  9. Android Loader异步装载

    一.Loader简介: (一).Loader的概念: 装载器从android3.0开始引进.它使得在activity或fragment中异步加载数据变得简单. 当成批显示数据的时候,为了使用户体验更好 ...

  10. java多线程 并发编程

    一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...