单词接龙 II-LintCode

给出两个单词(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值