《Cracking the Coding Interview》——第18章:难题——题目7
2014-04-29 03:05
题目:给定一个词典,其中某些词可能能够通过词典里其他的词拼接而成。找出这样的组合词里最长的一个。
解法:Leetcode上有Word Break这道题,和这题基本思路一致。
代码:
// 18.7 Given a list of words, find out the longest word made of other words in the list.
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
string longestBreakableWord(unordered_set<string> &dict) {
unordered_set<string>::const_iterator usit;
string res = ""; for (usit = dict.begin(); usit != dict.end(); ++usit) {
if (wordBreak(*usit, dict) && usit->length() > res.length()) {
res = *usit;
}
} return res;
}
private:
bool wordBreak(string s, unordered_set<string> &dict) {
int n;
int i, j;
string str;
vector<int> dp; n = (int)s.length();
if (n == || dict.empty()) {
return false;
}
dp.resize(n);
for (i = ; i < n; ++i) {
str = s.substr(, i + );
if (dict.find(str) != dict.end()) {
dp[i] = ;
} else {
for (j = ; j < i; ++j) {
if (dp[j] && dict.find(s.substr(j + , i - j)) != dict.end()) {
dp[i] = ;
break;
}
}
if (j == i) {
dp[i] = ;
}
}
} i = dp[n - ];
dp.clear();
return i == ;
}
}; int main()
{
unordered_set<string> dict;
string s;
Solution sol;
int i, n; while (cin >> n && n > ) {
for (i = ; i < n; ++i) {
cin >> s;
dict.insert(s);
} cout << sol.longestBreakableWord(dict) << endl;
} return ;
}
《Cracking the Coding Interview》——第18章:难题——题目7的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- leetcode:栈
1. evaluate-reverse-polish-notation Evaluate the value of an arithmetic expression in Reverse Polish ...
- 【JavaScript 封装库】BETA 1.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- Poj(1789),最小生成树,Prim
题目链接:http://poj.org/problem?id=1789 还是套路. #include <stdio.h> #include <string.h> #define ...
- 【[SDOI2017]新生舞会】
题目 好题啊 我们要求的是 \[C=\frac{\sum_{i=1}^na_i}{\sum_{i=1}^nb_i}\] 使得\(C\)最大 显然 \[C\times \sum_{i=1}^nb_i=\ ...
- assert函数和捕获异常
assert函数: C语言和C++都有一个专为调试而准备的工具函数,就是 assert()函数. 这个函数是在C语言的 assert.h 库文件里定义的,所以包含到C++程序里我们用以下语句: #in ...
- Java 序列化对象工具类
SerializationUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.Byt ...
- C语言中%p,%u,%lu都有什么用处
%p表示输出这个指针, %d表示后面的输出类型为有符号的10进制整形, %u表示无符号10进制整型, %lu表示输出无符号长整型整数 (long unsigned)
- python-类对象以列表切片模式操作
#类对象以列表切片模式操作 class Person: def __init__(self): self.cache=[] def __setitem__(self, key, value): #修改 ...
- SpringBoot学习12:springboot异常处理方式2(使用@ExceptionHandle注解)
1.编写controller package com.bjsxt.controller; import org.springframework.stereotype.Controller; impor ...
- 短信状态监听 - iOS
当使用 App 时若短信介入需要对当前状态进行监听操作,根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后续功能铺路. #import <Messa ...