[itint5]单词变换
基本上就是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]单词变换的更多相关文章
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [itint5]单词游戏
http://www.itint5.com/oj/#36 此题在数据大些,而且全是A的情况下会超时(因为要匹配到很后面才false).通过利用数组本身作为visited标示,而且使用string引用, ...
- 127. Word Ladder(单词变换 广度优先)
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- 面试题目——《CC150》高等难题
面试题18.1:编写一个函数,将两个数字相加.不得使用+或其他算数运算符. package cc150.high; public class Add { public static void main ...
- Word Ladder——LeetCode
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- leetcode第一刷_Word Ladder II
这道题非常难. 之前的题目我提到过一次用两个vector来做层序遍历的,就是由于这道题.要想最后恢复出单词变换的路径,就须要事先保存,依据dp中路径恢复的启示,保存的应该是一个单词的前一个变换节点.可 ...
- Java数据结构和算法总结-字符串及高频面试题算法
前言:周末闲来无事,在七月在线上看了看字符串相关算法的讲解视频,收货颇丰,跟着视频讲解简单做了一下笔记,方便以后翻阅复习同时也很乐意分享给大家.什么字符串在算法中有多重要之类的大路边上的客套话就不多说 ...
- Java数据结构和算法总结-字符串相关高频面试题算法
前言:周末闲来无事,看了看字符串相关算法的讲解视频,收货颇丰,跟着视频讲解简单做了一下笔记,方便以后翻阅复习同时也很乐意分享给大家.什么字符串在算法中有多重要之类的大路边上的客套话就不多说了,直接上笔 ...
- leetcode:查找
1. word ladder 题目: Given two words (start and end), and a dictionary, find the length of shortest t ...
随机推荐
- 捕获异常 winform
可以捕获winform中的异常写到文本中 <p>可以捕获winform中的异常写到文本中</p> <div class="cnblogs_code" ...
- Javascript基本格式
Javascript基本格式 ① JavaScript区分大小写 只要一门语言是面向对象的,其都是区分大小写,所以在Javascript中,变量小i与变量I是两个完全不同的变量 ② JavaScrip ...
- Junit的最简单样例:Hello world!
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3824934.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Tomcat虚拟目录的设置
在学习JSP/Servlet的过程中,配置Tomcat的虚拟目录可能是我们遇到的第一个比较麻烦的问题,说是麻烦是针对我们初学者而言,对于高手那都不是问题.反正我是弄了一天才配置好,发现网上给出的很多配 ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...
- Class<Object>与Class<?>有何区别呢
1.? 和 Object 差不多,不过还是有差别.在这种情况下: class<? extends SomeClass> , Object就不能用了 Object是一个具体的类名,而?是一个 ...
- 代码动态创建checkbox
根据数据库的内容动态创建Checkbox控件并显示在Panel上 dataset ds=new dataset(); CheckBox[ ] cb=new CheckBox[ds.tables[0]. ...
- 解决Strict Standards: Only variables should be passed by reference
这个错误发生在大家php调试程序用到一段代码里,那就是格式化显示出变量的函数functionrdump($arr)的第5行, 这段代码出自ecmall团队之手,但是ecmall已经很古董了,在php5 ...
- WPF多线程演示
WPF中的几种处理线程的工作方式: 1.简单的DispatcherTimer类似Timer控件 2.需要处理UI同步时,Dispatcher DispatcherOpertion 3.增强的Threa ...
- OFBiz进阶之HelloWorld(五)创建新实体
参考文档 https://cwiki.apache.org/confluence/display/OFBIZ/OFBiz+Tutorial+-+A+Beginners+Development+Guid ...