单词接龙 II-LintCode
来源:互联网 发布:航海家软件免费版 编辑:程序博客网 时间:2024/06/05 11:31
给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列
比如:
每次只能改变一个字母。
变换过程中的中间单词必须在字典中出现。
注意事项
所有单词具有相同的长度。
所有单词都只包含小写字母。
样例
给出数据如下:
start = “hit”
end = “cog”
dict = [“hot”,”dot”,”dog”,”lot”,”log”]
返回
[
["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]
]
#ifndef C121_H#define C121_H#include<iostream>#include<vector>#include<string>#include<unordered_set>#include<unordered_map>#include<queue>using namespace std;class Solution{public: vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string> > paths; vector<string> path(1, start); if (start == end) //corner case; { paths.push_back(path); return paths; } unordered_set<string> forward, backward; forward.insert(start); backward.insert(end); unordered_map<string, vector<string> > tree; bool reversed = false; //make sure the tree generating direction is consistent, since we have to start from the smaller set to accelerate; if (buildTree(forward, backward, dict, tree, reversed)) getPath(start, end, tree, path, paths); return paths; }private: bool buildTree(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, unordered_map<string, vector<string> > &tree, bool reversed) { if (forward.empty()) return false; if (forward.size() > backward.size()) return buildTree(backward, forward, dict, tree, !reversed); for (auto &word : forward) dict.erase(word); for (auto &word : backward) dict.erase(word); unordered_set<string> nextLevel; bool done = false; //in case of invalid further searching; for (auto &it : forward) //traverse each word in the forward -> the current level of the tree; { string word = it; for (auto &c : word) { char c0 = c; //store the original; for (c = 'a'; c <= 'z'; ++c) //try each case; { if (c != c0) //avoid futile checking; { if (backward.count(word)) //using count is an accelerating method; { done = true; !reversed ? tree[it].push_back(word) : tree[word].push_back(it); //keep the tree generation direction consistent; } else if (!done && dict.count(word)) { nextLevel.insert(word); !reversed ? tree[it].push_back(word) : tree[word].push_back(it); } } } c = c0; //restore the word; } } return done || buildTree(nextLevel, backward, dict, tree, reversed); } void getPath(string &start, string &end, unordered_map<string, vector<string> > &tree, vector<string> &path, vector<vector<string> > &paths) //using reference can accelerate; { if (start == end) paths.push_back(path); //till the end; else { for (auto &it : tree[start]) { path.push_back(it); getPath(it, end, tree, path, paths); //DFS retrieving the path; path.pop_back(); } } }};#endif
阅读全文
0 0
- LintCode-单词接龙II
- 单词接龙 II-LintCode
- lintcode-单词接龙-120
- lintcode,单词接龙
- 单词接龙-LintCode
- 单词接龙 II
- ***[Lintcode]Word Ladder 单词接龙
- LINTCODE——单词接龙
- Arithmetic problem | 单词接龙 II
- LintCode 120-单词接龙 广度优先搜索
- LintCode 单词搜索 II
- LintCode : 单词搜索 II
- 单词搜索 II-LintCode
- 单词拆分II-LintCode
- 单词接龙
- 单词接龙
- 单词接龙
- 单词接龙
- 大数据-配置PATH环境变量
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- poj1316 0ms水过
- lintcode -- 丢失的第一个正整数
- windows下创建.jshintrc文件
- 单词接龙 II-LintCode
- 动态规划
- Qt中根据不同版本号使用不同源码
- Choose the best route 【最短路】+【反向建图】
- Codeforces-158B
- Android 为什么监听不到开机广播action.BOOT_COMPLETED
- cat /proc/meminfo 详细
- H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
- AS gradle 理解