• leetcode 126 Word Ladder II


    题目连接

    https://leetcode.com/problems/word-ladder-ii/  

    Word Ladder II

    Description

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

    1. Only one letter can be changed at a time
    2. Each intermediate word must exist in the word list

    For example,

    Given:
    beginWord = "hit"
    endWord = "cog"
    wordList = ["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.

    跟127那道题差不多,只不过多了要保存路径(所有解)。
    一开始用dfs结果超时,一看要求最短路径马上就想到了bfs。
    于是胡乱一敲提交上去tle QAQ。
    bfs太慢好吧那么来个双向的吧,不幸的是经过一些奇怪的姿势,只能水到31组数据
    然后一直wa(总是少了几组解),越写越烦懒得改了。网上搜题解才搞明白了。
    具体做法是: 分层bfs,每遍历完一层再扩展下一层。在扩展过程中记得用map
    保存新节点和旧节点的父子关系,同时在dic中删除已出现的word防止重复。
    最后利用得到的这个map从end出发找start。好了终于过了 卒...
    talk is cheap show me the code

    class Solution {
        using ves = vector<string>;
        using vees = vector<ves>;
        using Hash = map<string, vector<string>>;
    public:
        vees findLadders(string start, string end, unordered_set<string> &dic) {
            if(!bfs(start, end, dic)) return ans;
            ves rec;
            dfs(start, end, rec);
            return ans;
        }
    private:
        void dfs(string &start, string &end, ves &rec) {
            rec.push_back(end);
            if(start == end) {
                ves temp(rec);
                reverse(temp.begin(), temp.end());
                ans.push_back(temp);
                rec.pop_back();
                return;
            }
            for(auto &r: hsh[end]) {
                dfs(start, r, rec);
            }
            rec.pop_back();
        }
        bool bfs(string start, string end, unordered_set<string> &dic) {
            int cur = 0;
            unordered_set<string> Q[2];
            Q[cur].insert(start);
            auto expandState = [&](unordered_set<string> &from, unordered_set<string> &to) {
                to.clear();
                for(auto &r: from) {
                    for(int i = 0; i < (int)r.size(); i++) {
                        string temp = r;
                        for(char j = 'a'; j <= 'z'; j++) {
                            char ch = temp[i];
                            if(ch == j) continue;
                            temp[i] = j;
                            if(dic.find(temp) != dic.end()) {
                                to.insert(temp);
                                hsh[temp].push_back(r);
                            }
                            temp[i] = ch;
                        }
                    }
                }
            };
            while(!Q[cur].empty()) {
                for(auto &r: Q[cur]) dic.erase(r);
                expandState(Q[cur], Q[cur ^ 1]);
                cur ^= 1;
                if(Q[cur].find(end) != Q[cur].end()) return true;
            }
            return false;
        }
    private:
        Hash hsh;
        vees ans;
    };
  • 相关阅读:
    python DB.fetchall()--获取数据库所有记录列表
    pybot/robot命令参数说明【dos下执行命令pybot.bat --help查看】
    win7 dos命令窗口内容显示不全解决办法--将命令执行结果输出到一个文件中
    【python cookbook】【数据结构与算法】2 从任意长度的可迭代对象中分解元素
    【python cookbook】【数据结构与算法】1将序列分解为单独的变量
    wxPython_Phoenix在线安装
    Python 进阶(五)定制类
    Python 进阶(四)类的继承
    墓型价格分析表
    用碑情况统计
  • 原文地址:https://www.cnblogs.com/GadyPu/p/5631344.html
Copyright © 2020-2023  润新知