【广度优先遍历】leetcode - Word Ladder II
来源:互联网 发布:bigemap地图下载器mac 编辑:程序博客网 时间:2024/06/15 05:09
题目:leetcode
Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) fromstart 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.
class Solution {public: vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string>> res; if(start==end && dict.count(start)) { res.push_back(vector<string>(1,start)); return res; } unordered_set<string> cur,next,used; unordered_map<string,unordered_set<string>> father;//键是单词,值是父亲,记录单词的父子关系 cur.emplace(start); bool should_continue=true; while(should_continue && !cur.empty()) { //mark临时记录父子关系 //之所以设置mark记录本轮产生的父子关系,是因为同一个单词,可能有多个父亲,不能只记录一个,而且只能记录距离最近的父亲 unordered_map<string,unordered_set<string>> mark; for(auto &i:next) { used.emplace(i); } //记得清空next! next.clear(); for(auto word:cur) { set<string> r=NewWords(word,dict,used); for(auto i:r) { if(i==end) should_continue=false; next.emplace(i); if(father.count(i)==0) { mark[i].emplace(word); } } } //把mark的内容导入father中 for(auto &j:mark) { father[j.first]=j.second; } swap(cur,next); } //根据父子关系,重建路径 find_path(father,start,end,res); return res; } void find_path(unordered_map<string, unordered_set<string>> &father, string start, string end, vector<vector<string>> &res){vector<string> path;path.push_back(end);find_path_core(father, start, end, res, path);}void find_path_core(unordered_map<string, unordered_set<string>> &father, string start, string end, vector<vector<string>> &res, vector<string> &path){if (end == start){reverse(path.begin(), path.end());res.push_back(path);reverse(path.begin(), path.end());//注意!再反转回来!!!!return;}for (auto &i : father[end]){ if(find(path.begin(),path.end(),i)!=path.end()) continue;path.push_back(i);find_path_core(father, start, i, res, path);path.pop_back();}} set<string> NewWords(string &word,unordered_set<string> &dict,unordered_set<string> &used) { set<string> res; for(int i=0;i<word.size();++i) { char c=word[i]; for(int j=0;j<26;++j) { char tmp='a'+j; if(tmp==c) continue; word[i]=tmp; if(dict.count(word)>0 && used.count(word)==0) res.emplace(word); } word[i]=c;//注意还原单词 } return res; } };
0 0
- 【广度优先遍历】leetcode - Word Ladder II
- 【广度优先遍历】Word Ladder II
- leetcode 127. Word Ladder BFS广度优先遍历
- Word Ladder问题 BFS广度优先遍历
- LeetCode 127 Word Ladder (广度优先搜索)
- 【leetcode】Word Ladder II
- [LeetCode]Word Ladder II
- [leetcode] Word Ladder II
- LeetCode - Word Ladder II
- Leetcode: Word Ladder II
- leetcode Word Ladder II
- Leetcode Word Ladder II
- LeetCode | Word Ladder II
- leetcode word ladder II
- 【Leetcode】Word Ladder II
- [LeetCode] Word Ladder II
- Word Ladder II -- LeetCode
- Leetcode: Word Ladder II
- shell中单引号、双引号的区别
- 50个最常用的UNIX/Linux命令
- Eclipse添加已经下载好的SVN插件
- 每天学点android就开心
- VC如何将资源中包含的位图释放成文件
- 【广度优先遍历】leetcode - Word Ladder II
- java 计算方法执行时间
- nodejs中的模块定义
- 开通CSDN博客
- 微信按住说话HTML实现
- rtmp 和 http 协议在播放 flv 流媒体的区别
- Html5 学习系列(六)Html5本地存储和本地数据库
- Anroid布局文件之FrameLayout
- xxxx