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. 2.什么是composer与packgist,composer的安装

    目录 学习地址: composer与packgist关系图片 composer的安装; 配置composer 修改国内镜像 用composer安装与卸载插件 composer插件升级后报错 学习地址: ...

  2. 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能在一起工作的 那些类可以在一起工作了[DP] UML类图: 简单模拟一下代码: //已存在的.具有 ...

  3. Python9-网络编程-day30

    # 由于不同机器上的程序要通信,才产生了网络# server# client# 端口 找到的程序# 在计算机上,每一个需要网络通信的程序,都会开一个端口# 在同一时间只会有一个程序占用一个端口# 不可 ...

  4. Java项目引入eclipse注意事项

    我以前也搞过java,后转前端,接触html+css+js时间比较多,所以java后端也忘了差不多.最近负责公司的邮件系统项目,项目是java语言写,项目架构比较复杂,在部署项目的时候,遇到了很多问题 ...

  5. JAVA-两种后台页面跳转方式

    1.请求转发 RequestDispatcher rd = request.getRequestDispatcher("url"); rd.forward(request, res ...

  6. Python爬虫selenium、PhanmJs

    selenium:可以模拟鼠标进行一些操作 实例1:实现自动打开google浏览器,进行百度搜索,并关闭浏览器 from selenium import webdriver from time imp ...

  7. Wannafly挑战赛3

    B遇见 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 65536K,其他语言131072K64bit IO Format: %lld 题目描述 A和B在同一条路上,他们之间的距离为 k ...

  8. AtCoder Regular Contest 089

    这场一边吃饭一边打,确实还是很菜的 C - Traveling Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem ...

  9. 连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27531   Accepted: 11077 De ...

  10. pip install ImportError: cannot import name main

    在Ubuntu上用pip install装ansible时报错 root@user:~# pip install --no-cache-dir ansible -i http://mirrors.al ...