当前位置:   article > 正文

(每日一练C++)126. 单词接龙 II_单词接龙c+

单词接龙c+

按字典 wordList 完成从单词 beginWord 到单词 endWord 转化,一个表示此过程的 转换序列 是形式上像 beginWord -> s1 -> s2 -> ... -> sk 这样的单词序列,并满足:

每对相邻的单词之间仅有单个字母不同。
转换过程中的每个单词 si(1 <= i <= k)必须是字典 wordList 中的单词。注意,beginWord 不必是字典 wordList 中的单词。
sk == endWord
给你两个单词 beginWord 和 endWord ,以及一个字典 wordList 。请你找出并返回所有从 beginWord 到 endWord 的 最短转换序列 ,如果不存在这样的转换序列,返回一个空列表。每个序列都应该以单词列表 [beginWord, s1, s2, ..., sk] 的形式返回。

示例 1:

输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
输出:[["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
解释:存在 2 种最短的转换序列:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"
示例 2:

输入:beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
输出:[]
解释:endWord "cog" 不在字典 wordList 中,所以不存在符合要求的转换序列。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/word-ladder-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <set>
  5. #include <unordered_set>
  6. #include <unordered_map>
  7. #include <queue>
  8. using namespace std;
  9. class Solution {
  10. public:
  11. vector<vector<string>> findLadders(string beginWord, string endWord, vector<string> &wordList) {
  12. vector<vector<string>> res;
  13. // 因为需要快速判断扩展出的单词是否在 wordList 里,因此需要将 wordList 存入哈希表,这里命名为「字典」
  14. unordered_set<string> dict = {wordList.begin(), wordList.end()};
  15. // 修改以后看一下,如果根本就不在 dict 里面,跳过
  16. if (dict.find(endWord) == dict.end()) {
  17. return res;
  18. }
  19. // 特殊用例处理
  20. dict.erase(beginWord);
  21. // 第 1 步:广度优先遍历建图
  22. // 记录扩展出的单词是在第几次扩展的时候得到的,key:单词,value:在广度优先遍历的第几层
  23. unordered_map<string, int> steps = {{beginWord, 0}};
  24. // 记录了单词是从哪些单词扩展而来,key:单词,value:单词列表,这些单词可以变换到 key ,它们是一对多关系
  25. unordered_map<string, set<string>> from = {{beginWord, {}}};
  26. int step = 0;
  27. bool found = false;
  28. queue<string> q = queue<string>{{beginWord}};
  29. int wordLen = beginWord.length();
  30. while (!q.empty()) {
  31. step++;
  32. int size = q.size();
  33. for (int i = 0; i < size; i++) {
  34. const string currWord = move(q.front());
  35. string nextWord = currWord;
  36. q.pop();
  37. // 将每一位替换成 26 个小写英文字母
  38. for (int j = 0; j < wordLen; ++j) {
  39. const char origin = nextWord[j];
  40. for (char c = 'a'; c <= 'z'; ++c) {
  41. nextWord[j] = c;
  42. if (steps[nextWord] == step) {
  43. from[nextWord].insert(currWord);
  44. }
  45. if (dict.find(nextWord) == dict.end()) {
  46. continue;
  47. }
  48. // 如果从一个单词扩展出来的单词以前遍历过,距离一定更远,为了避免搜索到已经遍历到,且距离更远的单词,需要将它从 dict 中删除
  49. dict.erase(nextWord);
  50. // 这一层扩展出的单词进入队列
  51. q.push(nextWord);
  52. // 记录 nextWord 从 currWord 而来
  53. from[nextWord].insert(currWord);
  54. // 记录 nextWord 的 step
  55. steps[nextWord] = step;
  56. if (nextWord == endWord) {
  57. found = true;
  58. }
  59. }
  60. nextWord[j] = origin;
  61. }
  62. }
  63. if (found) {
  64. break;
  65. }
  66. }
  67. // 第 2 步:深度优先遍历找到所有解,从 endWord 恢复到 beginWord ,所以每次尝试操作 path 列表的头部
  68. if (found) {
  69. vector<string> Path = {endWord};
  70. dfs(res, endWord, from, Path);
  71. }
  72. return res;
  73. }
  74. void dfs(vector<vector<string>> &res, const string &Node, unordered_map<string, set<string>> &from,
  75. vector<string> &path) {
  76. if (from[Node].empty()) {
  77. res.push_back({path.rbegin(), path.rend()});
  78. return;
  79. }
  80. for (const string &Parent: from[Node]) {
  81. path.push_back(Parent);
  82. dfs(res, Parent, from, path);
  83. path.pop_back();
  84. }
  85. }
  86. };
  87. 作者:LeetCode-Solution
  88. 链接:https://leetcode-cn.com/problems/word-ladder-ii/solution/dan-ci-jie-long-ii-by-leetcode-solution/
  89. 来源:力扣(LeetCode)
  90. 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/144394?site
推荐阅读
相关标签
  

闽ICP备14008679号