【题目】

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. sublime下package control安装无效解决

    使用快捷键:ctrl+`打开控制台执行如下命令 sublime2: import urllib2,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7c0f' ...

  2. CenOs7安装oracle图文详细过程(01)

    原创作品,转载请在文章头部(显眼位置)注明出处:https://www.cnblogs.com/sunshine5683/p/10011441.html 1.检查必要的安装包是否安装 命令脚本: rp ...

  3. SQL修改表结构

    --(1)向数据库Student表中添加Name字段 use MR_NXT alter table student add Name char(20) --(2)将Student表中Name的字段的数 ...

  4. php 生成唯一id的几种解决方法(实例)

    php 生成唯一id,网上查了下,有很多的方法 1.md5(time() . mt_rand(1,1000000)); 这种方法有一定的概率会出现重复 2.php内置函数uniqid() uniqid ...

  5. sql 传入参数为逗号分隔的字符串处理方法

    写了个存储过程,中间用到了类似这种写法 Select * From User Where ID In('1,2,3') 其中'1,2,3'是从外面传进来的参数,就这样执行报错:'1,2,3'转换为in ...

  6. 【疑难杂症01】TypeError: alert is not a function

    一.背景 话说今天在调试js的时候,碰到一个很奇怪的问题,现记录一下.当使用alert()函数弹出提示时,总是报错,你没看错,alert函数报错了. 二.详细说明 当时正在做一个关于告警的页面展示功能 ...

  7. jQuery实现动态选中select

    // jquery实现动态选中select var active = $('.all_sla_title1 .active') var group_name = active.html(); var ...

  8. Intelij IDEA 配置Tomcat时找不到 “Application Server”

    由于公司突然断电,再打开idea的时候,tomcat就消失了.然后在网上搜了一下,没搜到自己乱点了一下. 如图 : plugins >>   application servers Vie ...

  9. Spring3实战第一章 Aop 切面 XML配置

    刚看spring3实战书籍第一章  切面以前没有关注过 现在看到了  随手试验一下 AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Objec ...

  10. AutoMapper6扩展

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...