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 ...
随机推荐
- 精益创业之父Steve Blank: 怎样让企业内部创新获得50倍增速
编者注:本文英文版来自创新大师Steve Blank的个人博客,中文版由天地会珠海分舵进行编译.应用在初创企业打造上面的精益创业相信我们已经耳熟能详,可是假设我们面对的是一个已经发展起来的企业.或者是 ...
- Java 编程的动态性, 第4部分: 用 Javassist 进行类转换--转载
讲过了 Java 类格式和利用反射进行的运行时访问后,本系列到了进入更高级主题的时候了.本月我将开始本系列的第二部分,在这里 Java 类信息只不过是由应用程序操纵的另一种形式的数据结构而已.我将这个 ...
- mysql中判断字段为空
mysql中判断字段为null或者不为null 在mysql中,查询某字段为空时,切记不可用 = null, 而是 is null,不为空则是 is not null select nulco ...
- LOAD DATA INFILE Syntax--官方
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_n ...
- 数据库操作封装类 DBHelper.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- 华为S5300交换机配置基于接口的本地端口镜像
配置思路 1. 将Ethernet0/0/20接口配置为观察端口(监控端口) 2. 将Ethernet0/0/1----Ethernet0/0/10接口配置为镜像端口 配置步骤 1. 配置观察端 ...
- foreach遍历原理(一)
前言 要使用foreach的遍历的类首先要满足的条件 1. 类要实现公共方法 public IEnumerator GetEnumerator(){},还可以继承IEnumerable接口来实现这个方 ...
- ASP.net 中关于Session的存储信息及其它方式存储信息的讨论与总结
通过学习和实践笔者总结一下Session 的存储方式.虽然里面的理论众所周知,但是我还是想记录并整理一下.作为备忘录吧.除了ASP.net通过Web.config配置的方式,还有通过其它方式来存储的方 ...
- 微软office MIME类型
后缀 MIME 類型 .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document .docm app ...
- C程序设计语言练习题1-21
练习1-21 编写程序entab,将空格串替换为最少数量的制表符和空格,但要保持单词之间的间隔不变.假设制表符终止的位置与练习1-20的detab程序的情况相同.当使用一个制表符或者一个空格都可以打到 ...