http://www.itint5.com/oj/#42

基本上就是word ladder。直接来BFS,记录前驱。

vector<string> transform(set<string> &dict, string from, string to) {
vector<string> ans;
if (from.length() != to.length()) {
return ans;
}
queue<string> que;
map<string, string> prev_map;
que.push(from);
prev_map[from] = "";
while (!que.empty()) {
string s = que.front();
que.pop();
if (s == to) break;
for (int i = 0; i < s.length(); i++) {
char tmp = s[i];
for (char c = 'A'; c <= 'Z'; c++) {
if (tmp == c) continue;
s[i] = c;
if (dict.find(s) != dict.end()
&& prev_map.find(s) == prev_map.end()) {
que.push(s);
prev_map[s] = s;
prev_map[s][i] = tmp;
}
}
s[i] = tmp;
}
}
if (prev_map.find(to) == prev_map.end()) return ans;
string last = to;
do {
ans.push_back(last);
last = prev_map[last];
} while (last != "");
for (int i = 0; i < ans.size() / 2; i++) {
int k = ans.size() - i - 1;
string tmp = ans[i];
ans[i] = ans[k];
ans[k] = tmp;
}
return ans;
}

  

[itint5]单词变换的更多相关文章

  1. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  2. [itint5]单词游戏

    http://www.itint5.com/oj/#36 此题在数据大些,而且全是A的情况下会超时(因为要匹配到很后面才false).通过利用数组本身作为visited标示,而且使用string引用, ...

  3. 127. Word Ladder(单词变换 广度优先)

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. 面试题目——《CC150》高等难题

    面试题18.1:编写一个函数,将两个数字相加.不得使用+或其他算数运算符. package cc150.high; public class Add { public static void main ...

  5. Word Ladder——LeetCode

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  6. leetcode第一刷_Word Ladder II

    这道题非常难. 之前的题目我提到过一次用两个vector来做层序遍历的,就是由于这道题.要想最后恢复出单词变换的路径,就须要事先保存,依据dp中路径恢复的启示,保存的应该是一个单词的前一个变换节点.可 ...

  7. Java数据结构和算法总结-字符串及高频面试题算法

    前言:周末闲来无事,在七月在线上看了看字符串相关算法的讲解视频,收货颇丰,跟着视频讲解简单做了一下笔记,方便以后翻阅复习同时也很乐意分享给大家.什么字符串在算法中有多重要之类的大路边上的客套话就不多说 ...

  8. Java数据结构和算法总结-字符串相关高频面试题算法

    前言:周末闲来无事,看了看字符串相关算法的讲解视频,收货颇丰,跟着视频讲解简单做了一下笔记,方便以后翻阅复习同时也很乐意分享给大家.什么字符串在算法中有多重要之类的大路边上的客套话就不多说了,直接上笔 ...

  9. leetcode:查找

    1.  word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...

随机推荐

  1. PHP的语言规范

    PHP的语言规范: 1.php中的变量名区分大小写,但是函数名,类名,方法名,不区分大小写,但建议区分大小写 2.php代码必须书写在<?php?>(php标签),开启标记(<?ph ...

  2. lex&yacc6 ---error

    类的检测 http://blog.csdn.net/pandaxcl/article/details/1536784

  3. makefile--编码修改-空格出现错误

    "makefile", line 40: make: 1254-055 Dependency line needs colon or double colon operator. ...

  4. 【转】winform带参数启动另一个exe

     启动EXE string arg1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; string arg2 = "bbbbbbbbbbbbbbbb ...

  5. STM32F10XXX 启动设置

    在STMF103XXX 里,可以通过Boot[1:0]引脚选择3种不同的启动模式:       启动模式选择引脚      启动模式                 说明   BOOT1   BOOT ...

  6. Clone table header and set as the first element, and replace header's th with td

    Clone table header and replace header's th with td var tableHeaderRow = '#tableId tbody tr:nth-child ...

  7. PHP缓冲区强制及时输出

    string '{"multicast_id":4917012850725514945,"success":0,"failure":38,& ...

  8. php判断服务器是否支持Gzip压缩功能

    Gzip可以压缩网页大小从而达到加速打开网页的速度,目前主流的浏览器几乎都支持这个功能,但开启Gzip是需要服务器支持的,在这里我们简单的使用php来判断服务器是否支持Gzip功能. 新建一个php类 ...

  9. CorelDRAW 二维码插件

    随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...

  10. Django设置

    运行 django-admin.py startproject [project-name] 命令会生成一系列文件,在Django 1.6版本以后的 settings.py 文件中有以下语句: # B ...