LintCode-单词接龙II

LintCode-单词接龙II

给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列 
比如: 每次只能改变一个字母。 变换过程中的中间单词必须在字典中出现。

注意事项 
所有单词具有相同的长度。 
所有单词都只包含小写字母。

样例
给出数据如下:

start = "hit"

end = "cog"

dict = ["hot","dot","dog","lot","log"]

返回

[

    ["hit","hot","dot","dog","cog"],

    ["hit","hot","lot","log","cog"]

  ]

先直接写了个图+BFS
遍历每两个单词算different,等于1的放进邻接矩阵里面。
然后用没做优化的BFS找end

class Solution {
public:
    /**
    * @param start, a string
    * @param end, a string
    * @param dict, a set of string
    * @return a list of lists of string
    */
    struct Destination {
        int num = 0;
        vector<string> str;
    };
    struct Path {
        string str;
        vector<string> path;
    };
    map<string,bool> isPassed;
    void BFS(queue<Path> &order, vector<vector<string>> &result, map<string, Destination> m, const string end) {
        int num = order.size();
        for (int i = 0; i < num;i++) {
            Path s = order.front();
            isPassed[s.str]=true;
            order.pop();
            if (s.str == end) {
                result.push_back(s.path);
            }
            if (m[s.str].num != 0) {
                for (auto i : m[s.str].str) {
                    if(!isPassed[i])
                    {
                        Path p;
                        p.str = i;
                        p.path = s.path;
                        p.path.push_back(i);
                        order.push(p);
                    }
                }
            }
        }
        if (!order.empty() && result.empty())
            BFS(order, result, m, end);
        return;
    }
    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        // write your code here
        dict.insert(start);
        dict.insert(end);
        map<string, Destination> m;
        for (
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单词接龙是一个游戏,旨在通过更改一个字母来逐步转换一个单词成为另一个指定的单词。规则包括: 1. 可用于接龙单词首字母必须与前一个单词的尾字母相同。 2. 当存在多个首字母相同的单词时,取长度最长的单词。如果长度也相等,则取词典序最小的单词。 3. 已经参与接龙单词不能重复使用。 对于给定的一组由小写字母组成的单词数组和指定的起始单词和结束单词,我们需要进行单词接龙,并输出最长的单词串,其中单词串是由单词拼接而成,中间没有空格。如果不存在这样的转换序列,则返回0。 例如,对于输入的例子 beginWord = "hit",endWord = "cog",wordList = ["hot", "dot", "dog", "lot", "log"],我们可以进行以下单词接龙序列: "hit" -> "hot" -> "dot" -> "dog" -> "cog"。在这个例子中,最长的单词串为"hit" -> "hot" -> "dot" -> "dog" -> "cog"。 请注意,以上例子只是为了说明单词接龙的概念和规则,并不是针对Python编程的具体实现。具体的实现方法可以使用广度优先搜索 (BFS) 或双向BFS等算法来解决。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python题目55:单词接龙](https://blog.csdn.net/m0_60741207/article/details/121528418)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [127. 单词接龙(Python)](https://blog.csdn.net/JulyLi2019/article/details/106610034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值