• 面试题 17.13. 恢复空格. dp,字典树


    哦,不!你不小心把一个长篇文章中的空格、标点都删掉了,并且大写也弄成了小写。像句子"I reset the computer. It still didn’t boot!"已经变成了"iresetthecomputeritstilldidntboot"。在处理标点符号和大小写之前,你得先把它断成词语。当然了,你有一本厚厚的词典dictionary,不过,有些词没在词典里。假设文章用sentence表示,设计一个算法,把文章断开,要求未识别的字符最少,返回未识别的字符数。

    注意:本题相对原题稍作改动,只需返回未识别的字符数

    示例:

    输入:
    dictionary = ["looked","just","like","her","brother"]
    sentence = "jesslookedjustliketimherbrother"
    输出: 7
    解释: 断句后为"jess looked just like tim her brother",共7个未识别字符。
    提示:

    0 <= len(sentence) <= 1000
    dictionary中总字符数不超过 150000。
    你可以认为dictionary和sentence中只包含小写字母。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/re-space-lcci
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    class Trie{
    public:
        Trie* next[26];
        bool isEnd = false;
    
        void insert(string s){
            Trie* cur = this;
            for (int i = s.size() - 1; i >= 0; i--){
                int k = s[i] - 'a';
                if (cur->next[k] == nullptr){
                    cur->next[k] = new Trie();
                }
                cur = cur->next[k];
            }
            cur->isEnd = true;
        }
    };
    
    class Solution {
    public:
        int respace(vector<string>& dictionary, string sentence) {
            int n = sentence.size();
            //生成字典树,将dictionary中的词语插入字典树中
            Trie* root = new Trie();
            for (auto word : dictionary){
                root->insert(word);
            }
            //dp[i]表示在前i个字符里,未被识别的字符数最少是多少
            vector <int> dp(n + 1,INT_MAX);
            //初始化
            dp[0] = 0;
            for (int i = 1; i <= n; i++){
                //初次更新
                dp[i] = dp[i - 1] + 1;
                //字典树查找,从当前位置i向1搜索
                Trie* cur = root;
                for (int j = i; j >= 1; j--){
                    int  k = sentence[j - 1] - 'a';
                    //若本字母在字典树中不存在,break
                    if (cur->next[k] == nullptr) break;
                    else
                    //若本字母在字典树中是某个单词的结尾,则进行更新。
                    if (cur->next[k]->isEnd){
                        dp[i] = min(dp[i], dp[j - 1]);
                    }
                    if (dp[i] == 0) break;
                    cur = cur->next[k];
                }
            }
    
            return dp[n];
    
            
        }
    };
    
  • 相关阅读:
    Mozilla Prism v0.9 For Windows/Linux/Mac
    Firefox 3.0十大年夜新特征(1)
    刊行版:Epidemic GNU/Linux 2.1发布
    斥地版:Red Hat Enterprise Linux 4.7 Beta公布公布
    linux下安装drcom1.3.7心得
    Oracle老手艺对Linux意味着什么?
    学Linux要火山式的驾御还是垂垂来
    Firefox 3.0 RC2本周颁发
    net命令详解 **net accounts /maxpwage:unlimited
    学习官方示例 TApplication.OnDeactivate
  • 原文地址:https://www.cnblogs.com/xgbt/p/13281962.html
Copyright © 2020-2023  润新知