LeetCode OJ——Word Ladder2
http://oj.leetcode.com/problems/word-ladder-ii/
class Solution {
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict)
{
//map <string,string> parentRecord;
multimap<string ,string> parentRecord;
queue<pair<string ,int > > wordQueue;
unordered_set<string> visited;
wordQueue.push(make_pair(start,));
visited.insert(start);
parentRecord.insert(make_pair(start,""));
int flag = ;
int length = ;
int flag2 = ;
while(!wordQueue.empty())
{
string curStr = wordQueue.front().first;
int curStep = wordQueue.front().second;
if(flag == )
{
if(curStep != length)
break;
}
wordQueue.pop();
for(int i = ;i<curStr.size();i++)
{
if(flag2 == )
{
flag2 = ;
break;
}
string tmp = curStr;
for(int j = ;j<;++j)
{
flag2 = ;
tmp[i] = j+'a';
if(tmp == end)
{
// return curStep+1;
parentRecord.insert(make_pair(tmp,curStr));
flag = ;
length = curStep;
flag2 = ;
wordQueue.push(make_pair(tmp,curStep+));
visited.insert(tmp);
break;
}
if(visited.find(tmp) == visited.end() && dict.find(tmp)!=dict.end())
{
wordQueue.push(make_pair(tmp,curStep+));
visited.insert(tmp);
parentRecord.insert(make_pair(tmp,curStr));
}
}
}
}
vector<vector<string> > ansVector;
ansVector.clear();
vector<string> onePiece;
string str1 = end,str2;
multimap<string,string>::iterator iter;
int ii = parentRecord.count(end);
vector<string> another;
another.clear();
for(int i = ;i<ii;i++)
{
str1 = end;
onePiece.clear();
another.clear();
while()
{
onePiece.push_back(str1);
iter = parentRecord.find(str1);
if( str1 == start)
{
for(int it = onePiece.size()-;it>=;it--)
another.push_back(onePiece[it]);
ansVector.push_back(another);
break;
}
str2 = (*iter).second;
if(parentRecord.count(str1)>)
parentRecord.erase(iter);
str1 = str2;
}
}
return ansVector;
}
};
在visited那的处理那里弄错了,应该是更广一些。暂时不想改,先这样。
用到了multimap.因为map的话,key是唯一的,只能存储(cog,dog)不能一起存储(cog,dog),(cog,log).
LeetCode OJ——Word Ladder2的更多相关文章
- LeetCode OJ——Word Ladder
http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...
- LeetCode OJ——Word Break
http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...
- [LeetCode OJ] Word Search 深度优先搜索DFS
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
随机推荐
- Spring Boot 应用 快速发布到linux服务器的脚本代码示例
前提说明:spring boot 应用打包成jar包之后要部署到Linux服务器上面运行,我用的nohup java -jar 命令,但是代码更新之后重新部署的时候覆盖原来的项目,又要手动运行ps - ...
- PHP四种序列化方案
原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...
- Linux异常体系之stubs_offset
转自 http://www.xuebuyuan.com/2208550.html 在ARM V4及V4T以后的大部分处理器中,中断向量表的位置可以有两个位置:一个是0x00000000,另一个是0xf ...
- poj 2229 拆数问题 dp算法
题意:一个n可以拆成 2的幂的和有多少种 思路:先看实例 1 1 2 1+1 2 3 1+1+1 1+2 4 1+1+1+1 1+1+2 2+2 4 5 ...
- list_for_each_entry()函数分析
list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...
- 【转】MySQL innodb_autoinc_lock_mode 详解 ,并发插入时主键冲突的解决方案
本文转载于 http://www.cnblogs.com/JiangLe/p/6362770.html innodb_autoinc_lock_mode这个参数控制着在向有auto_increment ...
- centos7 bond 和 网桥配置
rhel7系统bond配置(更新版本):https://www.cnblogs.com/zhangjianghua/p/9119808.html Bonding的模式一共有7种: 1.mode=0(b ...
- selenium2常用API介绍
我们模拟web操作都是基于元素来操作的,我们首先要先确定元素,然后这个元素下对应的方法就可以看WebElement的方法. 1.点击操作 WebElement button=driver.findEl ...
- python基础学习笔记——反射
对编程语言比较熟悉的朋友,应该知道“反射”这个机制.Python作为一门动态语言,当然不会缺少这一重要功能.然而,在网络上却很少见到有详细或者深刻的剖析论文.下面结合一个web路由的实例来阐述pyth ...
- js各种继承方式和优缺点的介绍
js各种继承方式和优缺点的介绍 作者: default 参考网址2 写在前面 本文讲解JavaScript各种继承方式和优缺点. 注意: 跟<JavaScript深入之创建对象>一样,更像 ...