Given two words (start and end), and a dictionary, find the length of shortest transformation sequence 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"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

思路:

找两点之间的最小距离,感觉只能用图,构建图的邻接矩阵,然后folyd 果断的超时了.....

int ladderLength1(string start, string end, unordered_set<string> &dict) {
if(start == end) return ;
int vexnum = dict.size() + ;
if(dict.find(start) != dict.end()) vexnum--;
if(dict.find(end) != dict.end()) vexnum--;
vector<string> s(vexnum); //记录graph中每行代表的单词
vector<vector<int>> graph(vexnum, vector<int>(vexnum, vexnum + )); //邻接矩阵
s[] = start;
s.back() = end;
int i = ;
unordered_set<string>::iterator it;
for(it = dict.begin(); it != dict.end(); it++)
{
if(*it != start && *it != end)
s[i++] = (*it);
} //记录有哪些单词可以通过一次变化相互转换
for(i = ; i < s.size(); i++)
{
string temp = s[i];
for(int j = ; j < start.size(); j++)
{
for(char c = 'a'; c <= 'z'; c++)
{
temp[j] = c;
if(dict.find(temp) != dict.end())
{
int edge = find(s.begin(), s.end(), temp) - s.begin();
if(edge == i)
{
graph[i][edge] = ;
graph[edge][i] = ;
}
else
{
graph[i][edge] = ;
graph[edge][i] = ;
}
}
}
}
} for(int k = ; k < graph.size(); k++)
{
for(int i = ; i < graph.size(); i++)
{
for(int j = ; j < graph.size(); j++)
{
if(graph[i][j] > graph[i][k] + graph[k][j])
{
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
} return (graph[][vexnum - ] == vexnum + ) ? : graph[][vexnum - ] + ;
}

来看大神的BFS算法,用dis记录每个点到start的距离。队列里开始只有start, 然后每次遇到新转化成的单词就进队列,更新距离。判断能否转化时用字符长度和26个字母,而不是对字典遍历,因为字典中单词的数量可能远大于26个。

int ladderLength(string start, string end, unordered_set<string> &dict)
{
unordered_map<string, int> dis;
queue<string> q;
dis[start] = ;
q.push(start);
while(!q.empty())
{
string word = q.front(); q.pop();
for(int i = ; i < start.size(); i++)
{
string temp = word;
for(char c = 'a'; c <= 'z'; c++)
{
temp[i] = c;
if(dict.count(temp) > && dis.count(temp) == )
{
dis[temp] = dis[word] + ;
q.push(temp);
}
}
}
}
if(dis.count(end) == ) return ;
return dis[end];
}

【leetcode】Word Ladder (hard) ★的更多相关文章

  1. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  2. 【leetcode】Word Ladder II

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

  3. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  4. 【leetcode】Word Ladder II(hard)★ 图 回头看

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  5. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. 【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 ...

  8. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

随机推荐

  1. bootstrap 分页样式代码

    bootstrap 分页样式代码,废话不多说,直接上源码 <!DOCTYPE html> <html> <head> <title>Bootstrap ...

  2. javascript 笔记——setTimeout的参数问题

    setTimeout("xxx",500) 双引号中的作用域不捕捉局部变量,因此会报错误 如果你需要在双引号中可以在外部定义一个变量 var now; window.onload ...

  3. poj 3046 Ant Counting

    Ant Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4982   Accepted: 1896 Desc ...

  4. ZigBee 入网详解

    本文将根据Sniffer来详细解释ZigBee终端设备入网的整个流程,原创博文. 当协调器建立好网络后,终端设备执行zb_startrequest函数,准备入网时,他们两者之间详细的流程如下.

  5. [转]mysql-5.6.17-win32免安装版配置

    1. 下载mysql-5.6.17-win32:官网下载地址百度 2. 解压到自定义目录,我这里演示的是D:\wamp\mysql\ 3. 复制根目录下的my-default.ini,改名为my.in ...

  6. 【C#高级编程(学习与理解)】1.1 C#与.NET的关系

    1.C#语言不能孤立使用,而必须和.NET Framework一起考虑.C#编译器专门用于.NET,这表示用C#编写的所有代码总是在.NET Framework中运行. 2.C#就其本身而言只是一种语 ...

  7. 让Ecshop网店系统用户自动登陆

    让Ecshop网店系统用户户自动登陆,打开ecshop includes/init.php文件,可以发现Ecshop系统判断用户的SESSION不存在的时候会去读取存储在COOKIES里面的值.如下代 ...

  8. @Html.TextBox 的使用

    @Html.TextBox( }); //限制 text 的最大输入字符数为 10个 @Html.TextBox("users","",new {@class= ...

  9. (转载)用SQL语句创建Access表

    <来源网址:http://www.delphifans.com/infoview/Article_220.html>用SQL语句创建Access表 很久以前弄的,用了一天的时间,没有什么技 ...

  10. Spark Streaming揭秘 Day32 WAL框架及实现

    Spark Streaming揭秘 Day32 WAL框架及实现 今天会聚焦于SparkStreaming中非常重要的数据安全机制WAL(预写日志). 设计要点 从本质点说,WAL框架是一个存储系统, ...