Hard 单词变型成另一个单词 @CareerCup
来源:互联网 发布:怎样用阿里云建立网站 编辑:程序博客网 时间:2024/06/05 10:33
BFS的变形体。对起始单词的每一个字母都做替换,把替换后的单词加入到queue中,并同时维护一个Map来记录变化前单词和变化后单词的联系。用来回溯时能打印出路径。
package Hard;import java.util.HashSet;import java.util.LinkedList;import java.util.Map;import java.util.Queue;import java.util.Set;import java.util.TreeMap;import java.util.TreeSet;/** * Given two words of equal length that are in a dictionary, write a method to trans-form one word into another word by changing only one letter at a time. The newword you get in each step must be in the dictionary.给定两个有着相同长度且都在字典内的单词,要求写一个方法来把一个单词变型成另一个单词。一次只能转换一个字母,且每次生成的单词必须在字典内 * */public class S18_10 {public static LinkedList<String> transform(String startWord, String stopWord, Set<String> dictionary) {startWord = startWord.toUpperCase();stopWord = stopWord.toUpperCase();Queue<String> actionQueue = new LinkedList<String>();Set<String> visitedSet = new HashSet<String>();Map<String, String> backtrackMap = new TreeMap<String, String>();actionQueue.add(startWord);visitedSet.add(startWord);while (!actionQueue.isEmpty()) {String w = actionQueue.poll();// For each possible word v from w with one edit operationfor (String v : getOneEditWords(w)) {if (v.equals(stopWord)) {// Found our word! Now, back track.LinkedList<String> list = new LinkedList<String>();list.add(v);// Append v to listwhile (w != null) {list.add(0, w);w = backtrackMap.get(w);}return list;}// If v is a dictionary wordif (dictionary.contains(v)) {if (!visitedSet.contains(v)) {actionQueue.add(v);visitedSet.add(v); // mark visitedbacktrackMap.put(v, w);// w is the previous state of v}}}}return null;}// 改变word的某一个字母private static Set<String> getOneEditWords(String word) {Set<String> words = new TreeSet<String>();for (int i = 0; i < word.length(); i++) {// for every letterchar[] wordArray = word.toCharArray();// change that letter to something elsefor (char c = 'A'; c <= 'Z'; c++) {if (c != word.charAt(i)) {wordArray[i] = c;words.add(new String(wordArray));}}}return words;}public static HashSet<String> setupDictionary(String[] words) {HashSet<String> hash = new HashSet<String>();for (String word : words) {hash.add(word.toUpperCase());}return hash;}public static void main(String[] args) {String[] words = { "maps", "tan", "tree", "apple", "cans", "help","aped", "free", "apes", "flat", "trap", "fret", "trip", "trie","frat", "fril" };HashSet<String> dict = setupDictionary(words);LinkedList<String> list = transform("tree", "flat", dict);for (String word : list) {System.out.println(word);}}}
0 0
- Hard 单词变型成另一个单词 @CareerCup
- Hard 单词变型成另一个单词 @CareerCup
- Hard 找到由其它单词组成的最长单词 @CareerCup
- Hard 大文本找两个单词最短距离 @CareerCup
- Moderate 查单词 @CareerCup
- 单词。。
- 单词
- 单词
- 单词
- 单词:
- 单词
- 单词
- 单词
- 单词
- 单词
- 单词
- 单词
- 单词
- DB2 存储过程开发最佳实践
- angularJS学习小记(2)----制作简易计算器
- shapefile与GDB中不能允许存在的几何错误
- HTC G5恢复官方出厂ROM,去除ROOT的教程
- SQL的四种类型:DDL,DML,DCL,TCL
- Hard 单词变型成另一个单词 @CareerCup
- GCC objdump
- for 用法
- TexturePacker 3.0 使用教程
- SQL Server FOR XML PATH
- xml遇到的异常问题
- HTC G5 Nexus one解锁、刷Recovery与刷机教程
- jquery 获取Select option 选择的Text和Value
- Python操作SQL Server数据库