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.

和Word Ladder不同的地方在于可能多个父节点公用一个子节点。将父节点都记录下来即可。

class Solution {
public:
struct info{
info(){}
info(int level,int index){
m_level=level;
m_index=index;
}
int m_level;
int m_index;
};
void Path(vector<vector<string> >* ret,vector<string>* path,const vector<vector<int> >&father,const vector<string>& record,int index,int count){
(*path)[count]=record[index];
if(count==0){
ret->push_back(*path);
}
else{
for(int i=0;i<father[index].size();i++){
Path(ret,path,father,record,father[index][i],count-1);
}
}
}
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
map<string,info> m;
int qhead=0;
vector<string> record;
vector<int> tails;
vector<vector<int> >father;
int index=0;
m[start]=info(0,0);
record.push_back(start);
father.resize(father.size()+1);
int min_v=2147483647; while(qhead<record.size()!=0){
int currentIndex=index;
string s=record[qhead];
for(int i=0;i<s.length();i++){
char c=s[i];
for(int j='a';j<'z';j++){
if(j!=c){
s[i]=j;
if(s==end){
int level=m[record[qhead]].m_level+1;
if(level<min_v){
min_v=level;
tails.clear();
tails.push_back(qhead);
}
else if(level==min_v){
tails.push_back(qhead);
}
}
else if(dict.find(s)!=dict.end()){
if(m.find(s)==m.end()){
index++;
m[s]=info(m[record[qhead]].m_level+1,index);
father.resize(father.size()+1);
father[index].push_back(qhead);
record.push_back(s);
}
else{
info sinfo=m[s];
info tinfo=m[record[qhead]];
if(sinfo.m_level==tinfo.m_level+1){
father[sinfo.m_index].push_back(qhead);
}
}
}
}
s[i]=c;
}
}
qhead++;
}
if(min_v==2147483647){
return vector<vector<string> >(); } vector<vector<string> >ret;
vector<string>path; path.resize(min_v+1);
path[min_v]=end;
for(int i=0;i<tails.size();i++){
Path(&ret,&path,father,record,tails[i],min_v-1);
}
return ret;
}
};

LeetCode-Word LadderII的更多相关文章

  1. LeetCode:Word Ladder I II

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

  2. [leetcode]Word Ladder II @ Python

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

  3. [LeetCode] Word Squares 单词平方

    Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...

  4. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  6. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  7. [LeetCode] Word Frequency 单词频率

    Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...

  8. [LeetCode] Word Break II 拆分词句之二

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  9. [LeetCode] Word Break 拆分词句

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  10. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

随机推荐

  1. 初次使用cocoapods注意事项

    在仅仅用cocoapods时可能会遇到各种各样的错误和问题 这里中总结下: 1.首先使用cocoapods有非常多优点,在github上非常多优秀的开源项目都用到了它;假设你不会使用它,那么非常多优秀 ...

  2. QP01 BAPI、QP02 BDC

    近期在改动一个检验计划分配的一个程序.上网查了一些资料,分别对QP01检验计划创建.改动QP02.删除物料等操作.分享一下. 一.QP01 BAPI BAPI_INSPECTIONPLAN_CREAT ...

  3. 如何完全退出android应用程序

    当一个android应用程序包含多个activity时,要完全退出android应用程序,便要销毁掉所有的activity,下面是一种网上流传的比较经典完美的方法: 首先要定义一个继承Applicat ...

  4. mysql 创建数据库使用默认字符集(备忘)

    GBK: create database test2 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; UTF8: CREATE DATABASE ` ...

  5. 解决从源码编译ncurses6.0编译lib_gen.c报错的问题

    直接从官网的源码编译时,会提示报错: gcc -DHAVE_CONFIG_H -I. -I../include -D_GNU_SOURCE -DNDEBUG -O2 --param max-inlin ...

  6. css布局之负margin妙用及其他实现

    相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...

  7. sunny day

    初始学习记录 基于http://www.htmleaf.com/html5/html5muban/20141121552.html模板 <!DOCTYPE html> <html l ...

  8. Python 中 open()文件操作的方式

    Python的open方法用来打开一个文件.第一个参数是文件的位置和文件名,第二个参数是读写模式: f=open('/1.txt','w') 读写模式的类型有: rU 或 Ua 以读方式打开, 同时提 ...

  9. Entity Framework 新增实体,新增抽象实体

    抽象实体不能new 抽象类:人,实体类:学生 人 p_人= new 学生();   添加数据,学生和人都添加 抽象类可以提供一个抽象的方法,但是并没有实现,类似接口,但又不同于接口.子类继承父类时必须 ...

  10. 【转】深入理解Java内存模型(三)——顺序一致性

    数据竞争与顺序一致性保证 当程序未正确同步时,就会存在数据竞争.java内存模型规范对数据竞争的定义如下: 在一个线程中写一个变量, 在另一个线程读同一个变量, 而且写和读没有通过同步来排序. 当代码 ...