LeetCode-Word LadderII
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
- Only one letter can be changed at a time
- 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的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Squares 单词平方
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [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 ...
- [LeetCode] Word Frequency 单词频率
Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity ...
- [LeetCode] Word Break II 拆分词句之二
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- [LeetCode] Word Break 拆分词句
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
随机推荐
- lvs keepalived 安装配置详解【转】
lvs keepalived 安装配置详解 张映 发表于 2012-06-20 分类目录: 服务器相关 前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较 ...
- [转] gdb的基本工作原理
转自: http://www.spongeliu.com/linux/howgdbwork/ 还是面某M的时候,面试官问我:“用过gdb么?” 答:“用过,调了两年bug了”.“那好,给我解释下gdb ...
- Android 面试精华题目总结
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24015867 下面的题目都是楼主在android交流群大家面试时遇到的,如果大家 ...
- HTML5中class选择器属性的解释
设置有class属性值的元素,可以被css中的选择器调用,也可以在javascript中以getElementsByClassName()方法调用. 可以给各个元素添加class而且名称可以相同与id ...
- php按址传递bug
foreach ($product_info as $key=>&$value){ $value['bookCity'][]=array( ...
- Asp.net Repeater控件
Repeater控件和DataList控件,可以用来一次显示一组数据项.比如,可以用它们显示一个数据表中的所有行. Repeater控件完全由模板驱动,提供了最大的灵活性,可以任意设置它的输出 ...
- IO流(随机流,数组内存流
File file1=new File("test1.txt"); RandomAccessFile in2=new RandomAccessFile(file1,"rw ...
- ACM中常用的C/C++函数
只大概说明功能,具体用法请自行百度. C函数 memset:按字节填充地址空间 sscanf:从一个字符串中格式化读取变量 sprintf:将变量格式化写入字符串中 atoi:字符串转int atof ...
- 流Stream个人学习理解
1.Stream类 命名空间:System.IO 程序集:mscorlib 流是对字节序列的抽象,提供字节序列的一般视图. 流的操作包括三个方面: 1.读取(Read):将流数据传入到数据结构 2.写 ...
- combobox只读代码
public partial class Form1: Form { // combobox只读代码 [DllImport("user32.dll", CharSet = Char ...