原题地址:

https://oj.leetcode.com/submissions/detail/19446353/

题目内容:

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

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

For example,

Given:
start = "hit"
end = "cog"
dict = ["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.

方法:

在这里我分享下做这道题的心路,不,思路历程。

首先第一时间想到BFS,打算正常BFS,但是跟一般的BFS标记结点时不一样,由于可能通过不同路径到达,所以入队前不能标记结点,而是出队时扩展子结点前标记。

结果:TLE

然后考虑到,A、B、C、D到能某个结点,比如说F时,可能会重复计算F到终点的路径。考虑先BFS一次求出最短路径作为fullstep的参数,然后以类似动态规划的思路来记录下子问题的结果。其中具体的实现非常的复杂,同样的点,必须在固定的步数内才算有解。

结果:TLE

又想到,用unordered_set或者unordered_map装字符串,计算hash值时要遍历整个字符串,这个时候不如整数快。于是,就想到不记录字符串,而记录字符串的地址。字符串在从输入获得的set中。由于int损失了精度,就用long long来存。

结果:TLE

没办法了,想到返回子问题解的时候返回的是整个vector,有复制过程,那就换,换成指针,不复制整个vector了。

结果:TLE

干,那就提前建图!提前建好图,在图上做这一坨玩意,就不用老是重复拿26个字母穷举所有位置的情况了!

结果:TLE

最后的最后的最后,有点想放弃了,在意识模糊的情况下,就采用了提前建图 + BFS的思路。

结果居然TM AC了!

全部代码:

class Solution {

public:
unordered_map<long long,vector<long long>> map; // 提前建图
unordered_set<long long> mark; // 标记某个点是否已经访问过
struct node {
int len;
node *father;
string *self;
}; void generateMap(unordered_set<string> &dict) {
unordered_set<string> :: iterator it1;
unordered_set<string> :: iterator it2;
unordered_set<long long> :: iterator it3;
unordered_set<long long> used;
for (it1 = dict.begin(); it1 != dict.end(); ++ it1) {
vector<long long> tmp;
map[(long long) & (*it1)] = tmp;
}
for (it1 = dict.begin(); it1 != dict.end(); ++ it1) {
string *now = (string *) & (*it1);
used.insert((long long) now);
for (int i = 0; i < (*now).size(); ++ i) {
char pre = (*now)[i];
for (int j = 'a'; j <= 'z'; ++ j) {
if (j == pre)
continue;
(*now)[i] = j;
it2 = dict.find(*now);
if (it2 != dict.end()) {
it3 = used.find((long long) & (*it2));
if (it3 == used.end()) {
map[(long long) now].push_back((long long) & (*it2));
map[(long long) & (*it2)].push_back((long long) now);
}
}
}
(*now)[i] = pre;
}
}
} vector<string> generateVector(node *tmp) {
vector<string> res;
while (tmp) {
res.push_back(*(tmp->self));
tmp = tmp->father;
}
return res;
} vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
generateMap(dict);
queue<node> myque;
unordered_set<string> :: iterator it1;
vector<vector<string>> res;
vector<node> tmp;
node startnode;
it1 = dict.find(end);
startnode.len = 1;
startnode.father = NULL;
startnode.self = (string *) & (*it1);
myque.push(startnode);
it1 = dict.find(start);
string *fin = (string *)&(*it1);
while (!myque.empty()) {
node now = myque.front();
node *fa = new node;
*fa = now;
if (now.self == fin) {
vector<string> lst = generateVector(fa);
res.push_back(lst);
int limit = now.len;
myque.pop();
while (!myque.empty()) {
node last = myque.front();
if (last.self == fin && last.len == limit) {
lst = generateVector(&last);
res.push_back(lst);
}
myque.pop();
}
break;
}
mark.insert((long long)(now.self));
vector<long long> *vecptr = &map[(long long)(now.self)];
for (int i = 0; i < (*vecptr).size(); ++ i) {
unordered_set<long long> :: iterator it2 = mark.find((*vecptr)[i]);
if (it2 == mark.end()) {
node child;
child.len = now.len + 1;
child.father = fa;
child.self = (string *)(*vecptr)[i];
myque.push(child);
}
}
myque.pop();
}
return res;
}
};

  

后记:

之前那个BFS + 回溯 + DP的方法没能AC太可惜了,我那里指针玩的6得自己都怕,唉。。

【原创】leetCodeOj --- Word Ladder II 解题报告 (迄今为止最痛苦的一道题)的更多相关文章

  1. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  2. LeetCode: Word Break II 解题报告

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  3. 【原创】leetCodeOj --- Jump Game II 解题报告

    原题地址: https://oj.leetcode.com/problems/jump-game-ii/ 题目内容: Given an array of non-negative integers, ...

  4. 【LeetCode】212. Word Search II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀树 日期 题目地址:https://leetco ...

  5. 【LeetCode】140. Word Break II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归求解 日期 题目地址:https://leetc ...

  6. 【LeetCode】244. Shortest Word Distance II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存出现位置 日期 题目地址:https://le ...

  7. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  8. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  9. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

随机推荐

  1. Citrix 服务器虚拟化之三十一 XenApp 6.5负载均衡

    Citrix 服务器虚拟化之三十一 XenApp 6.5负载均衡 说明:       环境基于实验三十 1.准备一台Windows Server 2008 R2的虚拟机名为XenAPP2,然后加入域k ...

  2. thinkPHP框架介绍(一)

    原文:thinkPHP框架介绍(一) 一.ThinkPHP的介绍 期间有对ThinkPHP框架在学习上的问题欢迎大家交流:QQ:812231134 MVC M - Model 模型           ...

  3. MySQL触发器 trigger之for each row

    for each row 每行受影响,触发器都运行.叫行级触发器. oracle 触发器中分行级触发器和语句级触发器,可不写for each row,不管影响多少行都仅仅运行一次. mysql不支持语 ...

  4. 第二十次codeforces竞技结束 #276 Div 2

    真是状况百出的一次CF啊-- 终于还Unrated了,你让半夜打cf 的我们怎样释怀(中途茫茫多的人都退场了)--虽说打得也不好-- 在这里写一下这一场codeforces的解题报告.A-E的 题目及 ...

  5. HDU1754_I Hate It(线段树/单点更新)

    解题报告 题意: 略 思路: 单点替换,区间最值 #include <iostream> #include <cstring> #include <cstdio> ...

  6. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  7. zend framework将zip格式的压缩文件导入并解压到指定文件

    html代码 <pre class="php" name="code"><fieldset> <legend>批量导入学生照 ...

  8. Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢

    1.1       现象描写叙述 数据库节点响应缓慢,部分用户业务受到影响. 查看数据库告警日志,開始显示ORA-07445错误,然后是大量的ORA-04031错误和ORA-00600错误. 检查数据 ...

  9. Debian7.6安装过程中遇到的问题

    一 sudo命令不能用 1 使用su切换到root用户,命令: su 2 使用名:vim /etc/sudoers加入sudoer用户,命令: vim /etc/sudoers 找到root=(ALL ...

  10. 在mysql数据库中关于日期时间字段的处理

    在mysql数据库中关于日期时间字段的处理 在开发中,日期时间字段一般有如下几种设计 假设要获取2013-08-15日到2013-08-16日之间的记录 1. 直接使用日期时间类字段 相关sql语句如 ...