LeetCode: Word Ladder II

本文介绍了一种使用图搜索算法解决最短单词变换路径问题的方法。通过构建单词图,并运用广度优先搜索来找到从起始单词到目标单词的最短路径。详细展示了如何利用队列、步骤记录和父节点记录等数据结构实现算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

看了别人的答案,这题的关键在于数据结构的选择,一开始选择的multimap肯定MLE

 1 class Solution {
 2 public:
 3     void getPath(vector<vector<string> > &ansSet, vector<string> &ans, int now, vector<vector<int> > &pa, vector<string> &que)
 4     {
 5         if (now == -1) 
 6         {
 7             ansSet.push_back(vector<string>(0));
 8             for (int i = ans.size() - 1; i >= 0; i--)
 9                 ansSet.back().push_back(ans[i]);
10             return;
11         }
12         for (int i = 0; i < pa[now].size(); i++)
13         {
14             ans.push_back(que[now]);
15             getPath(ansSet, ans, pa[now][i], pa, que);
16             ans.pop_back();
17         }
18         
19     }
20     vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
21         // Start typing your C/C++ solution below
22         // DO NOT write int main() function
23         vector<string> que;
24         vector<int> step;
25         vector<vector<int> > pa;
26         unordered_map<string, int> hash;
27         step.push_back(1);
28         que.push_back(start);
29         pa.push_back(vector<int>(0));
30         pa.back().push_back(-1);
31         int head = 0;
32         int bestStep = -1;
33         vector<vector<string> > ans;
34         while (head < que.size())
35         {
36             string now = que[head];
37             int nowStep = step[head];
38             if (bestStep > -1 && nowStep == bestStep) break;
39             for (int i = 0; i < now.size(); i++)
40                 for (char ch = 'a'; ch <= 'z'; ch++)
41                 {
42                     string next = now;
43                     if (next[i] == ch) continue;
44                     next[i] = ch;
45                     if (next == end)
46                     {
47                         bestStep = nowStep + 1;
48                         vector<string> single(0);
49                         single.push_back(end);
50                         getPath(ans, single, head, pa, que);
51                     } else if (dict.count(next))
52                     {
53                         if (hash[next] == 0)
54                         {
55                             que.push_back(next);
56                             step.push_back(nowStep + 1);
57                             pa.push_back(vector<int>(0));
58                             pa.back().push_back(head);
59                             hash[next] = pa.size() - 1;
60                         } else if (step[hash[next]] == nowStep + 1)
61                         {
62                             pa[hash[next]].push_back(head);
63                         }
64                     }
65                 }
66             head++;
67         }
68         return ans;
69     }
70 };

 

转载于:https://www.cnblogs.com/yingzhongwen/archive/2013/05/22/3092641.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值