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. 【AC自动机】bzoj4327: JSOI2012 玄武密码

    题目思路没话讲:主要是做题时候的细节和经验 Description 在美丽的玄武湖畔,鸡鸣寺边,鸡笼山前,有一块富饶而秀美的土地,人们唤作进香河.相传一日,一缕紫气从天而至,只一瞬间便消失在了进香河中 ...

  2. numpy学习(一)

    numpy数据类型 # numpy创建对象,numpy创建的对象是n阶矩阵,类似python中列表的嵌套 nd = np.array([[1,2,3,4,5],[2,3,4,6,5]])nd 结果: ...

  3. 批量保存云盘链接的demo

    写在前面的声明: 作为一个正在自学爬虫的小白,用爬虫爬了八千本书的云盘链接,然后就想把这写链接的资源都转存到自己的云盘里,以防某一天资源失效.本来想在网上找个能够批量保存的软件,哪知道找到几个都不能用 ...

  4. python 中 * 与**的使用

    1.参数中使用如果是函数定义中参数前的*表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中 如定义以下函数 def func(*args): print(args) ...

  5. JAVA基础篇—继承

    父类Vehicle package com.car; public class Vehicle { final String brand;// String color;// double speed ...

  6. PAT Basic 1079

    1079 延迟的回文数(20 分) 给定一个 k+1 位的正整数 N,写成 a​k​​⋯a​1​​a​0​​ 的形式,其中对所有 i 有 0≤a​i​​<10 且 a​k​​>0.N 被称 ...

  7. canvas 动画库 CreateJs 之 EaselJS(下篇)

    本文来自网易云社区 作者:田亚楠 继承 对应原文:Inheritance 我们可以继承已有的「显示对象」,创建新的自定义类.实现方法有很多种,下面介绍其中之一. 举例:实现一个继承于 Containe ...

  8. alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!

    大家好! 多语言实现的案例:http://alidoing.com/或者http://www.alidoing.com/ 图:切换语言界面 JS代码实现: 1.首先新建一个对象langobj,当然对象 ...

  9. adb devices 找不到夜神模拟器解决方法

    先打开命令行窗口,输入adb devices,查看连接信息,大致意思是sdk的adb版本与夜神的adb版本不一致,导致. C:\Users\cz9025>adb devices List of ...

  10. 初学-BeautifulSoup爬取豆瓣页面

    # -*- coding: utf-8 -*-import osimport urllibimport urllib2from bs4 import BeautifulSoup headers = { ...