zoukankan      html  css  js  c++  java
  • 每日一题 为了工作 2020 0421 第五十题

    /**
     *
     * 【问题】字符串转换路径问题
     *         给定两个字符串,记为start和to,再给定一个字符串列表list,list中一定包含
     *         to,list中没有重复的字符串。所有字符串都是小写的。规定start每次只可以改
     *         变一个字符,最终的目标是彻底变成to,但每次变成的字符串都必须在list中存在。
     *         请返回最短的变换路径。
     * 【举例】
     *         start = "abc"
     *         to = "cab"
     *         list = {"cab","acc","cbc","ccc","cac","cbb","aab","abb"}
     *         转换路径的方法有很多种,但是最短的转换路径如下:
     *         abc --> abb --> aab --> cab
     *         abc --> abb --> cbb --> cab
     *         abc --> cbc --> cac --> cab
     *         abc --> cbc --> cbb --> cab
     * 【分析】
     *         本题目难度较大,需要拆分成四个步骤进行实现,接下来完成第一步骤获取每一个
     *         字符串的nexts信息。
     *
     * 【第二步】
     *         有了每一个字符串的nexts信息之后,相当于我们有了一张图,每个字符串相当于
     *         图上面的一个点,所有的nexts信息相当于这个点的所有邻接节点。
     *         接下来从start字符串出发,利用nexts信息和宽度优先遍历的方式,求出每一个字符
     *         到start字符串的最短距离。
     *         从"abc"出发 生成的最短距离信息如下:
     *         字符串   到start的最短距离
     *         abb      1
     *         acc      1
     *         cbb      2
     *         ccc      2
     *         abc      0
     *         aab      2
     *         cac      2
     *         cbc      1
     *         cab      3
     *
     * @author 雪瞳
     * @Slogan 时钟尚且前行,人怎能再次止步!
     * @Function 返回每一个字符串的最短距离信息
     *
     */
    

      

    public class GetDistances {
    
        public HashMap<String,Integer> getDistances(String start, HashMap<String,ArrayList<String>> nexts){
            //返回一个map集合 k 为当前字符串可到达的字符串 value是需要的距离
            HashMap<String,Integer> distances = new HashMap<>(20);
            distances.put(start,0);
            //队列
            Queue<String> queue = new LinkedList<>();
            //起始元素
            queue.add(start);
            HashSet<String> set = new HashSet<>();
            set.add(start);
            //层层遍历找到每一个元素
            while (!queue.isEmpty()){
                String cur =queue.poll();
                //根据当前字符串拿到其对应的nexts的节点元素
                for (String str:nexts.get(cur)){
                    //避免重复
                    if (!set.contains(str)){
                        distances.put(str,distances.get(cur)+1);
                        queue.add(str);
                        set.add(str);
                    }
                }
            }
            return distances;
        }
    
        public static void main(String[] args) {
            String start = "abc";
            List<String> list = new ArrayList<>();
            String[] elements = new String[]{"cab","acc","cbc","ccc","cac","cbb","aab","abb"};
            for (String elem : elements){
                list.add(elem);
            }
            list.add(start);
    
            GetNexts get = new GetNexts();
            HashMap<String, ArrayList<String>> nexts = get.getNexts(list);
            GetDistances distances = new GetDistances();
            HashMap<String, Integer> result = distances.getDistances(start, nexts);
            Set<Map.Entry<String, Integer>> entries = result.entrySet();
            for (Map.Entry<String,Integer> entry : entries){
                System.out.println(entry.getKey()+"----"+entry.getValue());
            }
    
        }
    }
    

      

    *运行结果

  • 相关阅读:
    PTA —— 基础编程题目集 —— 函数题 —— 61 简单输出整数 (10 分)
    PTA —— 基础编程题目集 —— 函数题 —— 61 简单输出整数 (10 分)
    练习2.13 不用库函数,写一个高效计算ln N的C函数
    练习2.13 不用库函数,写一个高效计算ln N的C函数
    练习2.13 不用库函数,写一个高效计算ln N的C函数
    迷宫问题 POJ 3984
    UVA 820 Internet Bandwidth (因特网带宽)(最大流)
    UVA 1001 Say Cheese(奶酪里的老鼠)(flod)
    UVA 11105 Semiprime Hnumbers(H半素数)
    UVA 557 Burger(汉堡)(dp+概率)
  • 原文地址:https://www.cnblogs.com/walxt/p/12743829.html
Copyright © 2011-2022 走看看