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

  1. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...

  2. LeetCode OJ——Word Break

    http://oj.leetcode.com/problems/word-break/ 动态规划题目,重点是建立出模型来: fun(start,end) = fun(start,i)*fun(i+1, ...

  3. [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 ...

  4. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

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

  7. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  8. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  9. 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 ...

随机推荐

  1. matplotlib绘图股票走势图实践

    导入模块 import pandas as pdimport numpy as npfrom pandas import Series,DataFrameimport matplotlib.pyplo ...

  2. linux 下 docker-compose安装

    docker和dockers-compose的版本兼容对照 以下是我的服务器的相关信息 linux版本 [root@izbp16fm097gaw3tdaog2wz bin]# cat /proc/ve ...

  3. asp发送短信验证码 pst方式

    <script language="jscript" runat="server">  Array.prototype.get = function ...

  4. SimpleDateFormat优化写法

    在一个读取数据库数据导出到excel文件的例子当中,每次处理一个时间信息的时候,就需要创建一个SimpleDateFormat实例对象,然后再丢弃这个对象.大量的对象就这样被创建出来,占用大量的内存和 ...

  5. Mysql显示某个数据库的所有表

    显示表名: show tables; //先用use进入要查看表的库 mysql> use mysql; Database changed mysql> show tables; +--- ...

  6. 当列表推导式遇到lambda(匿名函数)

    Python这么优雅的语言,我也是醉了...... 事情由一段代码引发,请看: 上述的列表推导式+lambda表达式+for循环,他们碰撞出来的结果搞的人晕头转向,咱们逐步来分析一下他们到底是个什么鬼 ...

  7. python面试题解析(数据库和缓存)

    1.     答: 关系型数据库:Mysql,Oracel,Microsoft SQL Server 非关系型数据库:MongoDB,memcache,Redis. 2.     答:     MyI ...

  8. dubbo与zk注册中心如何对接,如何做到服务自动发现

    先看下consumer端发起调用时的链路流程: +---------------------------+ +---------------------------+ +--------------- ...

  9. X86保护模式 八操作系统类指令

    X86保护模式  八操作系统类指令 通常在操作系统代码中使用,应用程序中不应用这些指令 指令分为三种:实模式指令,任何权级下使用的指令.实模式权级0下可执行的指令和仅在保护模式下执行的指令 一  实模 ...

  10. 常用类--Date日期类,SimpleDateFormat日期格式类,Calendar日历类,Math数学工具类,Random随机数类

    Date日期类 Date表示特定的时间,精确到毫秒; 构造方法: public Data() public Date(long date) 常用方法: public long getTime() pu ...