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 ...
随机推荐
- Android BroadcastReceiver实例Demo(有序广播的发送)
上一篇简介了广播的发送,这篇主要介绍下,有序广播的发送. 设置完相关属性的时候,广播就会依照有序的方式进行发送: 发送顺序: 先发送第二条广播: 再发送第一条广播: 最后发送第三条广播. 代码例如以下 ...
- C++11 tuple
tuple元组定义了一个有固定数目元素的容器,其中的每个元素类型都可以不相同,这与其他容器有着本质的区别.是对pair的泛化. 首先来介绍元组的创建和元组元素的访问.通过make_tuple()创建元 ...
- js判断访问者是否来自移动端代码
<script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...
- 一个好用的Python备份mysql的脚本
前几天打算用Python写一个mysql脚本,上Google看了下老外写的,写的挺好的,原地址在http://tecadmin.net/python-script-for-mysql-database ...
- spring05配置文件之间的关系
一:配置文件包含关系 1.创建对应的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; ...
- 遍历页面上所有TextBox,并赋值为String.Empty
//不含母板页 foreach (System.Web.UI.Control txtobj in this.Page.Controls) { if (txtobj.GetType().Na ...
- CentOS 7 之几个新特性(转)
上篇我们讲到默认没有ifconfig是centos7的新特性,所以我特意上网搜索了一下其新特性,找到一篇文章,现转过来. centos最小好化安装没有ifconfig命令 刚安装了centos7.0, ...
- C++ Primer 5th 第9章 顺序容器
练习9.1:对于下面的程序任务,vector.deque和list哪种容器最为适合?解释你的选择的理由.如果没有哪一种容器优于其他容器,也请解释理由.(a) 读取固定数量的单词,将它们按字典序插入到容 ...
- Oracle数据库之PL/SQL流程控制语句
Oracle数据库之PL/SQL流程控制语句 在任何计算机编程语言(如C,Java,C#等)都有各种流程控制语句,同样,在PL/SQL中也存在这样的流程控制结构. 几种常见的流程控制结构: 一.条件结 ...
- C# .NET 使用第三方类库DotNetZip解压/压缩Zip rar文件
DotNetZip on CodePlex: http://dotnetzip.codeplex.com/ 详细的可以看源代码……总之感觉比SharpZipLib好用.而且DotNetZip支持VB, ...