单词接龙 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
原创粉丝点击