【题目】

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

【题意】

    给定两个单词start和end, 一个词典,找到全部的最短转换序列。

几个注意事项:
    1. 每次变换仅仅能改变一个字符
    2. 变换的中间单词必须在词典中
    3. 全部单词长度同样
    4. 全部单词字符都小写

【思路】

     思路和word Ladder是同样的,仅仅只是本题须要把左右的序列输出。

     为了恢复转换序列在搜索的过程中,我们须要记录每一个可达单词的前继单词(所谓单词可达,就是start通过若干次字符变换后能够转换成当前单词)。

     一旦我们找到end, 我们就能够通过前继恢复路径。这跟用dijkstra找最短路径的方法事实上非常相似。

    

【代码】

class Solution {
public:
void getSequences(vector<vector<string> >&result, vector<string>&sequence, string&start, string end, map<string, vector<string> >&percursors){
sequence.push_back(end);
if(start==end){
vector<string> v=sequence;
reverse(v.begin(), v.end());
result.push_back(v);
return;
} //找end的前驱
vector<string> pres = percursors[end];
for(int i=0; i<pres.size(); i++){
getSequences(result, sequence, start, pres[i], percursors);
sequence.pop_back();
}
} vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
vector<vector<string> > result;
if(start==end)return result; //记录前驱的map
map<string, vector<string> > percursors;
//标记是否已经找到最短序列
bool isFind=false;
//交替存储相邻
queue<string> q1;
queue<string> q2;
q1.push(start);
//找前驱
while(!q1.empty() || !q2.empty()){
//存放当前层单词
set<string> words;
if(!q1.empty()){
while(!q1.empty()){
string curword=q1.front(); q1.pop();
for(int i=0; i<curword.length(); i++){
string tword=curword;
for(char c='a'; c<='z'; c++){
if(c!=curword[i]){
tword[i]=c;
//推断是否是end
if(tword==end){
isFind=true;
//保存前驱
percursors[tword].push_back(curword);
//保存当前层单词
words.insert(tword);
}
else if(dict.find(tword)!=dict.end()){
//假设tword在词典中。则保存它的前驱
percursors[tword].push_back(curword);
//保存当前层单词
words.insert(tword);
}
}
}
}
}
//将当前层的单词保存到q2
for(set<string>::iterator it=words.begin(); it!=words.end(); it++){
q2.push(*it);
dict.erase(*it);
}
}
else{
while(!q2.empty()){
string curword=q2.front(); q2.pop();
for(int i=0; i<curword.length(); i++){
string tword=curword;
for(char c='a'; c<='z'; c++){
if(c!=curword[i]){
tword[i]=c;
//推断是否是end
if(tword==end){
isFind=true;
//保存前驱
percursors[tword].push_back(curword);
//保存当前层单词
words.insert(tword);
}
else if(dict.find(tword)!=dict.end()){
//假设tword在词典中,则保存它的前驱
percursors[tword].push_back(curword);
//保存当前层单词
words.insert(tword);
}
}
}
}
}
//将当前层的单词保存到q1
for(set<string>::iterator it=words.begin(); it!=words.end(); it++){
q1.push(*it);
dict.erase(*it);
}
}
if(isFind)break;
}
//生成全部序列
vector<string>sequence;
getSequences(result, sequence, start, end, percursors);
return result;
}
};

LeetCode: Word Ladder II [127]的更多相关文章

  1. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  2. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  3. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  4. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. [LeetCode] Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. leetcode—word ladder II

    1.题目描述 Given two words (start and end), and a dictionary, find all shortest transformation sequence( ...

  7. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  8. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  9. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

随机推荐

  1. 使用PowerShell为SSAS Role添加Membership

    PowerShell, SSAS, Role, Membership 上篇随笔使用PowerShell创建SSAS Role贴出了如何使用PowerShell批量创建Role,由于个人项目需求,创建R ...

  2. HTML5--(3)过渡+动画+颜色+文本

    一.过渡transition transition-property指定属性名称 (如width.height.background-color.内外边距) all 所有属性都将获得过渡效果(默认) ...

  3. Spring 学习(五)--- 事务(未完成)

    问题 : Spring 事务传播机制是怎么样的,在什么应用场景使用 事务是什么 我们使用的框架可能是Hibernate/JPA或者是Mybatis,都知道的底层是需要一个session/connect ...

  4. Chrome , Firfox 不支持fireEvent的方法

    转自:http://bossdai.iteye.com/blog/2111458 Chrome , Firfox 不支持fireEvent的方法 可以使用dispatchEvent的方法替代, 直接给 ...

  5. Hadoop学习笔记(9) ——源码初窥

    Hadoop学习笔记(9) ——源码初窥 之前我们把Hadoop算是入了门,下载的源码,写了HelloWorld,简要分析了其编程要点,然后也编了个较复杂的示例.接下来其实就有两条路可走了,一条是继续 ...

  6. shell条件测试和流程控制

    一.条件测试操作 1.test 用途:测试特定的表达式是否成立,当条件成立时,命令执行后的返回值为0,否则为其他数值 格式:test 表达式 2.常见的测试类型 ①测试文件状态 格式:[ 操作符 文件 ...

  7. golang 编码转化

    在网上搜索golang编码转化时,我们经常看到的文章是使用下面一些第三方库: https://github.com/djimenez/iconv-go https://github.com/qiniu ...

  8. HBase入门教程

    # 背景 最近看到公司一个项目用到hbase, 之前也一直想看下hbase.个人理解Hbase作为一个nosql数据库,逻辑模型感觉跟关系型数据库有点类似.一个table,有row即行,列.不过列是一 ...

  9. python gif动态图的合成

    1.确保imageio已经安装 pip install imageio 2.函数准备 def create_gif(image_list, gif_name): import imageio fram ...

  10. SQL Server 字符串合并

    应用场景:多条数据某列数据以某种样式合并成一条数据,例如: 1    1,0    小赵    0012    2,0    小钱    002                        ==== ...