[leetcode] 126. Word Ladder II 解题报告

来源:互联网 发布:终端写c语言 编辑:程序博客网 时间:2024/06/05 09:37

题目链接: https://leetcode.com/problems/word-ladder-ii/

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord toendWord, 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.

思路: 和Word Ladder相比多了要记录路径, 这样我们为每一个结点记录一下父结点, 这样最后的时候我们就从终点开始一个个往前找其父结点, 直到找到起始点, 然后翻转一下加入结果集合中即可.

大概过程差不多, 但是有点不同的是当我们将字典中的一个字符串删除的时候在另一条路径上可能还会用到这个字符. 也就是像这样:

A -> C -> D,    B->C->D

他们都会经过C, 并且两个都是最短的路径, 在A的时候搜索到C, 并且将C从字典中删除, 当B在搜索与其距离为1的字符串时, C已经不在字典中了, 那么怎么办呢? 我们设置一个hash表用来存储一个字符串的父结点集合, 这样C不在字典中再去查hash表看C是否在hash表中, 如果在的话并且C的父结点层次和B一样, 那么就将B也加入到C的父结点结合中去. 可以知道, 一个字符串的父结点集合的距离起点的距离必然是相等的, 也就是说他们都是最短距离.

最后遍历完所有的点之后, 再用DFS从终点往前找出所有集合即可.

不得不吐槽一句这是哪个变态的公司问的问题, 第二遍写的时候居然还不如第一次写的, 可能我已经昏了, 毕竟已经快凌晨4点了, 本来可以三点睡的, 可恶!

代码如下:

class Solution {public:    void DFS(set<pair<string, int>> st, vector<string> vec)    {        for(auto val: st)        {            vec.push_back(val.first);            if(hash.count(val.first)==0)            {                reverse(vec.begin(), vec.end());                return result.push_back(vec);            }            DFS(hash[val.first], vec);            vec.pop_back();        }    }        vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {        wordList.insert(endWord);        wordList.erase(beginWord);        que.push(make_pair(beginWord, 1));        while(!que.empty())        {            auto val = que.front();            que.pop();            for(int i = 0; i < val.first.size(); i++)            {                string tem = val.first;                for(int j =0; j < 26; j ++)                {                    tem[i] = 'a' + j;                    if(wordList.count(tem))                    {                        que.push(make_pair(tem, val.second+1));                        wordList.erase(tem);                        hash[tem].insert(val);                    }                    else if(hash.count(tem)&&hash[tem].begin()->second==val.second)                        hash[tem].insert(val);                }            }        }        DFS(hash[endWord], vector<string>{endWord});        return result;    }    vector<vector<string>> result;    unordered_map<string, set<pair<string, int>>> hash;    queue<pair<string, int>> que;};


1 0
原创粉丝点击