【word ladder】cpp
题目:
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time
- 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
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
代码:
class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
queue<string> que;
que.push(beginWord); que.push("");
int len = ;
while ( !que.empty() )
{
string curr = que.front();
que.pop();
if (curr!="")
{
for ( size_t i = ; i < curr.size(); ++i )
{
char curr_c = curr[i];
for ( char c='a'; c <= 'z'; ++c )
{
if (c==curr_c) continue;
curr[i] = c;
if (curr==endWord) return len+;
if ( wordDict.find(curr)!=wordDict.end() )
{
que.push(curr);
wordDict.erase(curr);
}
}
curr[i] = curr_c;
}
}
else if ( !que.empty() )
{
len++;
que.push("");
}
}
return ;
}
};
tips:
学习了BFS的思路。
维护一个queue;存放当前word在dict中的所有邻居;末尾加一个空字符""来标示深入一层。
http://www.cnblogs.com/TenosDoIt/p/3443512.html
http://blog.csdn.net/niaokedaoren/article/details/8884938
=============================================
第二次过这道题,上来就打着bfs的幌子写了一个dfs的算法,结果是超时。但也想了一下为什么不能用dfs,dfs会超时的原因是啥:
比如:beginWord = "ab" wordDict{"cb, db"}
如果用dfs的话,就可能会建立出来ab→cb→db 这样即走了冤枉路,也不是最短。
class Solution {
public:
int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
queue<string> curr;
queue<string> next;
int len = ;
curr.push(beginWord);
while ( !curr.empty() )
{
while ( !curr.empty() )
{
string word = curr.front();
curr.pop();
for ( int i=; i<word.size(); ++i )
{
char ori = word[i];
for ( char c='a'; c<='z'; ++c )
{
if ( c==ori ) continue;
word[i] = c;
if ( word==endWord ) return len+;
if ( wordDict.find(word)!=wordDict.end() )
{
next.push(word);
wordDict.erase(word);
}
}
word[i] = ori;
}
}
if ( next.empty() ) return ;
len++;
swap(next, curr);
}
return ;
}
};
【word ladder】cpp的更多相关文章
- 【Word Search】cpp
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...
- 【Word Break】cpp
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符
1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
随机推荐
- 返回mapcontrol上的已被选择的element
IGraphicsContainerSelect.SelectedElement
- GitLab-CE-8.9.4 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: gitlab-8.9.4 bug tracking collaboration commercial development devops git ...
- 定时器new Timer().schedule()的使用
Timer是一种工具,线程用其安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行.实际上是个线程,定时调度所拥有的TimerTasks. TimerTask是一个抽象类,它的子类由 ...
- 浅谈Scrum敏捷开发:4个输入/输出、3个关键物、3个会议
文章对Scrum敏捷开发流程进行系统的分析,希望借此文能够加深你对敏捷开发的认知,更好的展开产品工作. Scrum敏捷开发,是一种敏捷开发框架,是一个增量的.迭代的开发过程,具备可视.可集成和可运行使 ...
- Windows 服务快捷启动命令,可以直接在运行处运行电脑的功能。
gpedit.msc-----组策略 sndrec32-----录音机 nslookup----- ip地址侦测器 explorer------ 打开资源管理器 logoff-------注销命令 t ...
- PPII打不开 更改I.bat
http://jingyan.baidu.com/article/3a2f7c2e7d277126afd6118d.html
- Django疑难问题
1页面出现中文报错 :Non-ASCII character '\xe9' in file E:\CPaas\cpaas\views.py 解决:在页面顶部加入#coding=utf-8 2执行syn ...
- JAVA设计模式初探之桥接模式
生活中的一个例子: 拿汽车在路上行驶的来说.既有小汽车又有公共汽车,它们都不但能在市区中的公路上行驶,也能在高速公路上行驶.这你会发现,对于交通工具(汽车)有不同的类型,它们所行驶的环境(路)也 ...
- 模块化Java简介
什么是模块化? 模块化是个一般概念,这一概念也适用于软件开发,可以让软件按模块单独开发,各模块通常都用一个标准化的接口来进行通信.实际上,除了规模大小有区别外,面向对象语言中对象之间的关注点分离与 ...
- Android(java)学习笔记154:采用HttpClient提交数据(qq登录案例)
1.Apache -Httpclient HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包 ...