题目:

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, 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.

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的更多相关文章

  1. 【Word Search】cpp

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  2. 【Word Break】cpp

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  3. 【Word Ladder II】cpp

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

  4. 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符

    1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...

  5. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  6. 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~ ...

  7. 【Text Justification】cpp

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  8. 【Edit Distance】cpp

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

  9. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

随机推荐

  1. swagger + springboot

    参考文章:  https://blog.csdn.net/xupeng874395012/article/details/68946676/ https://blog.csdn.net/hry2015 ...

  2. [opencv3.2cmake error ] sys/videoio.h no such file or directories

    I don't have /usr/include/sys/videoio.h at all Before that , I have ipp download question. So I down ...

  3. java 多线程的经验总结

    什么是线程? 线程是操作系统所能运算调度的最小单元,包含于进程之中,作为进程的实际运作单位:线程与进程的区别,线程是进程的子集,一个进程可以有多个线程,每个线程并行执行不同的任务,不同的进程使用不同的 ...

  4. JeeSite 工作流Activiti的应用实例

    新建流程模型 在线办公-流程管理-模型管理-新建模型  点击“提交”后会立即跳转到“流程在线设计器”页面,请看下一章节 在线流程设计器 在线办公流程管理模型管理模型管理编辑 整体流程图 mat ...

  5. linux 后渗透测试

    学习参考: http://weibo.com/1869235073/B9Seswf9R?type=comment http://weibo.com/p/1001603723521007220513 h ...

  6. pta 编程题15 列出连通集

    其它pta数据结构编程题请参见:pta 题目 题目要求分别以深度优先搜索和广度优先搜索输出图的连通集. 广度优先搜索要用到队列,先回顾一下循环队列: struct QNode { int* Data; ...

  7. php之基础深入---类与对象篇

    1.类的自动加载: spl_autoload_register()函数可以注册任意数量的自动加载器,当使用尚未被定义的类(class)和接口(interface)时自动去加载,这样可以避免includ ...

  8. Intel&amd

  9. UVA 12034 Race(递推)

    递推,f[i = i个名次][j = 共有j个人] = 方案数. 对于新加入的第j个人,如果并列之前的某个名次,那么i不变,有i个可供并列的名次选择,这部分是f[i][j-1]*i, 如果增加了一个名 ...

  10. 分析ELF的加载过程

    http://blog.chinaunix.net/uid-72446-id-2060538.html 对于可执行文件来说,段的加载位置是固定的,程序段表中如实反映了段的加载地址.对于共享库来?段的加 ...