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. C盘空间太大,分区助手减小分区大小教程

    首先看一个需要缩小C盘或需要减少分区空间的一个例子:“我的电脑里C盘剩余空间为530GB,除了C盘外还有一个D盘,但D盘的空间不到30GB,另外还有两个隐藏分区,一个200MB,一个15GB.我想把C ...

  2. 使用Excel消费C4C的OData service

    步骤比较简单, 打开Excel的标签Data->From Other Sources->From OData Data Feed: 输入如下url: https://.c4c.saphyb ...

  3. 科大讯飞语音转文字以及中文分词的Java测试代码

    我录了一段音存储在这个test.m4a文件里,语音内容为"测试一下Netweaver对于并发请求的响应性能". 使用如下Java代码进行测试: package com.iflyte ...

  4. Sliding Window - The Smallest Window II(AIZU) && Leetcode 76

    http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_3_B For a given array a1,a2,a3,...,aNa1 ...

  5. 昂贵的聘礼,(最短路的应用),Poj(1063)

    题目链接:http://poj.org/problem?id=1062 很好的一道中文题. 思路: 把每种替换当做一条边,权重为交易优惠,就是求原点0到物品1的最短路. 这里有限制条件,每个节点还有等 ...

  6. python同时遍历数组的索引和元素

    1.一般要同时遍历数组的索引和元素需要先确定数组的长度length(元素个数),然后使用range函数来生成数组的索引,最后使用该索引来访问数组的元素. 具体做法如下: l = [2,7,11,15] ...

  7. cuda数组的拷贝

    原文链接 简单描述一下自己要做的事情:(1)CPU三维的vector--->(2)CPU三维数组--->(3)转换到GPU中的三维数组--->(4)转换到CPU中的三维数组,而其中问 ...

  8. 第25章 串行FLASH文件系统FatFs—零死角玩转STM32-F429系列

    第25章     串行FLASH文件系统FatFs 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.c ...

  9. 转:比较spring cloud和dubbo,各自的优缺点是什么

    原文:https://blog.csdn.net/u010664947/article/details/80007767 dubbo由于是二进制的传输,占用带宽会更少 springCloud是http ...

  10. SSH & 文件传输 & 远程桌面管理

    SSH   Windows Linux MacOS Android IOS                                     https://www.ssh.com http:/ ...