【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 ...
随机推荐
- h5 本地存储
H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,接下来我就教你怎么使用H5本地存储,本文篇幅较大,JS代码较多, ...
- C#基础知识图谱
- jeesit 部署404
1.刷新项目 2.clean 项目 3.重新部署项目 4.Ran as maven build 后在重新部署 5.重新导入maven项目
- Mysql数据库学习总结(一)
数据库概念 数据库(Database)是按照数据结构来组织.存储和管理数据,建立在计算机存储设备上的仓库. 简单说,数据库就是存放数据的仓库.和图书馆存放书籍.粮仓存放粮食类似. 数据库分类 分为 关 ...
- pta 编程题8 Tree Traversals Again
其它pta数据结构编程题请参见:pta 这次的作业考察的是树的遍历. 题目的输入通过栈的pop给出了树的中序遍历的顺序.根据push和pop的顺序构造树的方法为:定义一个变量father来确定父节点, ...
- 【翻译】22款HTML & CSS3 UI工具包免费下载
下面盘点了22款适用于网页设计的HTML&CSS3 UI工具包,并且全部都是免费的哦!喜欢就赶紧 下载或收藏吧.这些免费工具可以加速你的网页开发进程,让你有更多时间专注于其他更重要的部分.由于 ...
- 黑箱中的 retain 和 release
https://github.com/Draveness/Analyze/blob/master/contents/objc/黑箱中的%20retain%20和%20release.md 写在前面 在 ...
- python 数据库操作 SQLite、MySQL 摘录
转自: http://www.cnblogs.com/windlaughing/p/3157531.html 不管使用什么后台数据库,代码所遵循的过程都是一样的:连接 -> 创建游标 -> ...
- Hybrid App开发之css样式使用
前言: 前面学习了html,今天学习一下css的基本使用,看下html与css之间是如何结合来编写前端网页的. CSS 是什么? CSS 是 Cascading Style Sheets(级联样式表) ...
- S运算符&&和|| 及其优先级
a && b : 将a, b转换为Boolean类型, 再执行逻辑与, true返回b, false返回aa || b : 将a, b转换为Boolean类型, 再执行逻辑或, tru ...