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. 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭

    正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...

  2. python中的sort、sorted排序

    我们通常会遇到对数据库中的数据进行排序的问题,今天学习一下对列表和字典的排序方法. 列表 第一种:内建方法sort sort()对列表排序是永久性的排序. 用法:sort(*, key=None, r ...

  3. percona-toolkit工具使用介绍

    percona-toolkit工具使用介绍 1. pt-heartbeat 1.1 pt-heartbeat 原理 1.2 pt-heartbeat 主要参数介绍 1.3 pt-heartbeat 实 ...

  4. Eclipse上进行java web项目的打包

    以下是一个基于maven搭建的Spring Boot项目的目录结构 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/ ...

  5. Optimization & Map

  6. leetcode-4-basic

    解题思路:这道题比较简单,代码不贴了.需要注意的是: 数字与字符串之间的转换, char str[100]; sprintf(str, "%d", num); 解题思路: 这道题是 ...

  7. L2-006 树的遍历 RTA

    L2-006 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树 ...

  8. [解读REST] 0.REST 相关参考资料

    Web之父 Tim Berners Lee :https://en.wikipedia.org/wiki/Tim_Berners-Lee 世界上诞生的第一个网站:http://info.cern.ch ...

  9. Selenium WebDriver-模拟鼠标双击某个元素

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  10. python - 接口自动化测试实战 - case1 - 优化版

    题目: 基于以下两个接口和数据完成接口自动化测试,并生成测试报告: '''登录 login='http://47.107.168.87:8080/futureloan/mvc/api/member/l ...