【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 ...
随机推荐
- PHP函数:mysql_fetch_assoc指针重置
本文目前主要讨论mysql_fetch_assoc“指针”如何重置的问题 要了解mysql_fetch_assoc,先看看它与mysql_fetch_row和mysql_fetch_array的关系. ...
- C#问题记录-CallbackOnCollectedDelegate
做项目的时候遇到了这个问题: 检测到:CallbackOnCollectedDelegate 对“xx.HookProc::Invoke”类型的已垃圾回收委托进行了回调.这可能会导致应用程序崩溃.损坏 ...
- MyDebugeer 一个简单调试器的实现
学习的是网上的帖子,所以就不贴源码了. 整个程序以调试循环为主体,实现了启动调试,继续执行,内存查看,读取寄存器值,显示源代码,断点的设置.查看.删除,三种单步执行:StepIn.StepOver.S ...
- javascript:理解DOM事件
首先,此文不讨论繁琐细节,但是考虑到读者的心灵感受,本着以积极向上的心态,在此还是会列举示例说明. 标题为理解DOM事件,那么在此拿一个简单的点击事件为例,希望大家看到这个例子后能触类旁通. DOM ...
- POJ 2184 Cow Exhibition(背包)
希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...
- 在ListView控件中实现修改功能
实现效果: 知识运用: ListView控件的LabelEdit属性 //指示用户是否可以编辑控件中数据项的标签 public bool LabelEdit{get;set;} 实现代码: priva ...
- java编程基础——从上往下打印二叉树
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 题目代码 /** * 从上往下打印出二叉树的每个节点,同层节点从左至右打印. * Created by YuKai Fan on 20 ...
- js日期类型date
javascript语言核心包括Date()构造函数,用来创建表示日期和时间的函数 //返回当前的日期和时间 var today = new Date(); //2011年1月1日 ...
- for in 和 for of的区别详解
for in 和 for of 相对于大家肯定都不陌生,都是用来遍历属性的没错.那么先看下面的一个例子: 例1 const obj = { a: 1, b: 2, c: 3 } for (let i ...
- JS位运算和遍历
JS位运算符 整数 有符号整数:允许使用正数和负数,第32位作为符号位,前31位才是存储位 无符号整数:只允许用正数 如果用n代表位 位数 = 2^n-1 由于位数(1.2.4.8.16...)中只有 ...