zoukankan      html  css  js  c++  java
  • leetcode 面试题 17.13. 恢复空格

    package com.example.lettcode.dailyexercises;
    
    import java.util.Arrays;
    
    /**
     * @Class Respace
     * @Description 面试题 17.13. 恢复空格
     * 哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。
     * 像句子"I reset the computer. It still didn’t boot!"
     * 已经变成了"iresetthecomputeritstilldidntboot"。
     * 在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典dictionary,不过,有些词没在词典里。
     * 假设文章用sentence表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。
     * 注意:本题相对原题稍作改动,只需返回未识别的字符数
     * <p>
     * 示例:
     * 输入:
     * dictionary = ["looked","just","like","her","brother"]
     * sentence = "jesslookedjustliketimherbrother"
     * 输出: 7
     * 解释: 断句后为"jess looked just like tim her brother",共7个未识别字符。
     * 提示:
     * 0 <= len(sentence) <= 1000
     * dictionary中总字符数不超过 150000。
     * @Author
     * @Date 2020/7/9
     **/
    public class Respace {
        // 字典树
        class Trie {
            public Trie[] next;             // 表示子节点
            public boolean isEnd;
    
            Trie() {
                next = new Trie[26];        // 最多26个字符,也就是最多26个子节点
                isEnd = false;
            }
    
            /**
             * 插入新字符串
             */
            public void insert(String s) {
                Trie curPos = this;
                // 字典树增加string时为啥从字符串尾部向前遍历,
                // 因为在删除标点符号的字符串中匹配字典的字符串时是从后向前遍历的
                for (int i = s.length() - 1; i >= 0; i--) {
                    int tmp = s.charAt(i) - 'a';
                    // cur.next[tmp] 为下一个子节点
                    if (curPos.next[tmp] == null) {
                        // 该字符对应的子节点不存在,新建一个子节点
                        curPos.next[tmp] = new Trie();
                    }
                    // 跳到子节点
                    curPos = curPos.next[tmp];
                }
                // 最后一个节点,也就是字符串的第一个字符所对应的节点
                curPos.isEnd = true;
            }
    
        }
    
        public int respace(String[] dictionary, String sentence) {
            int n = sentence.length();
            Trie root = new Trie();
            // 利用字典构建字典树
            for (String word : dictionary) {
                root.insert(word);
            }
    
            //  dp[i] 表示字符串的前 i 个字符的最少未匹配数
            int[] dp = new int[n + 1];
            Arrays.fill(dp, Integer.MAX_VALUE);
            dp[0] = 0;
            for (int i = 1; i <= n; ++i) {
                // 假设第i个字符不匹配
                dp[i] = dp[i - 1] + 1;
                Trie curPos = root;
                // 遍历前 i 个字符,若以其中某一个下标 idx 为开头、以第 i + 1 个字符为结尾的字符串正好在词典里
                for (int j = i; j >= 1; --j) {
                    int tmp = sentence.charAt(j - 1) - 'a';
                    if (curPos.next[tmp] == null) {
                        // 字典中沿着某个字符串搜索完毕,仍没有匹配
                        break;
                    } else if (curPos.next[tmp].isEnd) {
                        // 遍历前 i 个字符,若以其中某一个下标 idx 为开头、
                        // 以第 i + 1 个字符为结尾的字符串正好在词典里
                        // 说明sentence[j-1..i]是个字典中的某个字符串
                        dp[i] = Math.min(dp[i], dp[j - 1]);
                    }
                    // dp[i]=0 说明前i个字符串中没有未识别的字符
                    if (dp[i] == 0) {
                        break;
                    }
                    // 找到前一个字符
                    curPos = curPos.next[tmp];
                }
            }
            return dp[n];
        }
    }
    
    // 测试用例
    @Test
    public void respaceTest(){
    	Respace respace = new Respace();
    	String[] dictionary = new String[]{"looked","just","like","her","brother"};
    	String sentence = "jesslookedjustliketimherbrother";
    	int ans = respace.respace(dictionary, sentence);
    	System.out.println("Respace demo01 result:" + ans);
    }
    
  • 相关阅读:
    使用Java实现简单的局域网设备扫描
    Spring集成ElasticSearch搜索引擎
    Mysql 自定义函数示例
    Ehcache缓存配置以及基本使用
    SpringBoot-学习笔记
    【年度总结】2017年年度总结
    Android权限之动态权限
    Android沉浸式状态栏的简单实现
    Android自定义多宫格解锁控件
    Android For OpenCV的环境搭建
  • 原文地址:https://www.cnblogs.com/fyusac/p/13276344.html
Copyright © 2011-2022 走看看