2014-04-29 04:22

题目:给定一堆长度都相等的单词,和起点、终点两个单词,请从这堆单词中寻找一条变换路径,把起点词变成终点词,要求每次变换只能改一个字母。

解法:Leetcode中有Word Ladder,这题基本思路一致。

代码:

 // 18.10 Given a list of words, all of same length. Given a source and a destionation words, you have to check if there exists a path between the two. Every time you may change only one letter in the word.
// This is my code from leetcode problem set: word ladder
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std; class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
unordered_map<string, vector<string> > back_trace;
vector<unordered_set<string> > level(); dict.insert(start);
dict.insert(end); int flag, nflag;
flag = ;
nflag = !flag;
level[flag].insert(start); unordered_set<string>::iterator usit;
char ch, old_ch;
string word;
while (true) {
flag = !flag;
nflag = !nflag;
level[flag].clear();
for (usit = level[nflag].begin(); usit != level[nflag].end(); ++usit) {
dict.erase(*usit);
}
for (usit = level[nflag].begin(); usit != level[nflag].end(); ++usit) {
word = *usit;
for (size_t i = ; i < word.size(); ++i) {
old_ch = word[i];
for (ch = 'a'; ch <= 'z'; ++ch) {
if (ch == old_ch) {
continue;
}
word[i] = ch;
if (dict.find(word) != dict.end()) {
back_trace[word].push_back(*usit);
level[flag].insert(word);
}
}
word[i] = old_ch;
}
}
if (level[flag].empty() || level[flag].count(end) > ) {
// found or not found
break;
}
} single_result.clear();
for (size_t i = ; i < result.size(); ++i) {
result[i].clear();
}
result.clear(); if (!back_trace.empty()) {
recorverPath(back_trace, end);
} return result;
}
private:
vector<vector<string> > result;
vector<string> single_result; void recorverPath(unordered_map<string, vector<string> > &back_trace, string cur) {
if (back_trace.count(cur) == ) {
// this word has no back trace, it is unreachable.
vector<string> single_path(single_result); single_path.push_back(cur);
reverse(single_path.begin(), single_path.end());
result.push_back(single_path);
return;
} const vector<string> &v = back_trace[cur];
vector<string>::const_iterator usit; single_result.push_back(cur);
for (usit = v.begin(); usit != v.end(); ++usit) {
recorverPath(back_trace, *usit);
}
single_result.pop_back();
}
};

《Cracking the Coding Interview》——第18章:难题——题目10的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  5. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

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

  7. 二刷Cracking the Coding Interview(CC150第五版)

    第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...

  8. 《Cracking the Coding Interview》——第18章:难题——题目13

    2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...

  9. 《Cracking the Coding Interview》——第18章:难题——题目12

    2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...

  10. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

随机推荐

  1. js从入门到精通到深入到就业

    本篇博客是我参看人家代码做的总结,个人感觉非常非常好,简单.步步深入,不用花大量时间来学完正本js,只需要把其中的代码理解透彻,上班无压力(上班无压力是指js部分,包括查看框架源代码都有很大帮助) / ...

  2. RHEL/CentOS 6.x使用EPEL6与remi的yum源安装MySQL 5.5.x

    PS:如果既想获得 RHEL 的高质量.高性能.高可靠性,又需要方便易用(关键是免费)的软件包更新功能,那么 FedoraProject 推出的 EPEL(Extra Packages for Ent ...

  3. 【转】css行高line-height的一些深入理解及应用

    一.前言 前两天在腾讯ISD团队博客上看到一篇翻译的文章“深入理解css 行高”,是个不错的文章,学到了不少东西,建议您看看. 这里,我也要讲讲我对line-height的一些理解,所讲解的东西绝大多 ...

  4. HTML5 input date 移动端 IOS 不支持问题

    1.placeholder 问题解决方法 对 input type date 使用 placeholder 的目的是为了让用户更准确的输入日期格式,iOS 上会有 date 不会显示 placehol ...

  5. Spring 注解中@Resource 和 @Authwired 的区别

    @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...

  6. securecrt颜色设置

    https://blog.csdn.net/zq710727244/article/details/53909801

  7. 本地预览的vue项目,在githubpage静态展示

    本地项目github静态展示 前提 在本地npm run dev后能够在本地端口正常显示 githubpage为自己的静态页面 上线 config/index.js中设置assetsPublicPat ...

  8. 采坑笔记——mysql的order by和limit排序问题

    背景说明 今天写出一个十分弱智的bug,记录一下,提醒自己以后别这种犯错,不怕丢人哈~ 在写一个分页查询记录的sql时,要根据添加的时间逆序分页输出,之前的写法是酱紫 select record.a, ...

  9. 【word】html转doc的小研究

    html转doc,页眉页脚丢失 html 转 doc,是全屏铺满(缩放级别很高)

  10. Yii2 设计模式

    一. 单例模式 顾名思义, 单例模式就是只实例一次,通过一个接口去实现多处需要的同一类对象的需求. 例子: public function __construct($config = []) { Yi ...