• 648.replace words


    In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.

    Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.

    You need to output the sentence after the replacement.

    Example 1:

    Input: dict = ["cat", "bat", "rat"]
    sentence = "the cattle was rattled by the battery"
    Output: "the cat was rat by the bat"
    

    Note:

    1. The input will only have lower-case letters.
    2. 1 <= dict words number <= 1000
    3. 1 <= sentence words number <= 1000
    4. 1 <= root length <= 100
    5. 1 <= sentence words length <= 1000

    方法: 前缀树

    //In English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.
    //
    //Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.
    //
    //You need to output the sentence after the replacement.
    //
    //Example 1:
    //Input: dict = ["cat", "bat", "rat"]
    //sentence = "the cattle was rattled by the battery"
    //Output: "the cat was rat by the bat"
    //Note:
    //The input will only have lower-case letters.
    //1 <= dict words number <= 1000
    //1 <= sentence words number <= 1000
    //1 <= root length <= 100
    //1 <= sentence words length <= 1000
    #include<string>
    #include<vector>
    #include <sstream>
    #include<stack>
    #include<queue>
    #include<iostream>
    
    using namespace std;
    
    class Solution {
        class trieNode {
            bool isword = false;
            vector<trieNode *> children = vector<trieNode *>(26, NULL);
        public:
            trieNode() = default;
    
            void insert(const string &word) {
                trieNode *p = this;
                for (int i = 0; i < word.size(); ++i) {
                    int idx = (int) word[i] - (int) 'a';
                    if (p->children[idx] == NULL)
                        p->children[idx] = new trieNode();
                    p = p->children[idx];
                }
                p->isword = true;
            }
    
            int find_matched_prefix_size(const string &word) {
                trieNode *p = this;
                for (int i = 0; i < word.size(); ++i) {
                    int idx = (int) word[i] - (int) 'a';
                    if (!p->children[idx]) break;
                    p = p->children[idx];
                    if (p->isword)
                        return i + 1;
                }
                return 0;
            }
    
            void show_trie(trieNode *root) {
                queue<trieNode *> qu;
                qu.push(root);
                while (qu.size() != 0) {
                    int size = qu.size();
                    for (int i = 0; i < size; i++) {
                        trieNode *node = qu.front();
                        qu.pop();
                        for (int j = 0; j < 26; ++j) {
                            if (node->children[j]) {
                                cout << (char) (j + 'a') << " ";
                                qu.push(node->children[j]);
                            } else
                                cout << "NIL ";
                        }
                        cout << "			";
                    }
                    cout << endl;
                }
            }
        };
    
    public:
        string replaceWords(vector<string> &dict, string sentence) {
            trieNode trie = trieNode();
            for (const string &s:dict) {
                trie.insert(s);
            }
    //        trie.show_trie(&trie);
            string s;
            string result;
            istringstream in(sentence);
            while (in >> s) {
                int match_result = trie.find_matched_prefix_size(s);
                if (match_result) {
                    result += s.substr(0, match_result) + " ";
                } else {
                    result += s + " ";
                }
            }
            if (!result.empty())
                result.pop_back();//删除字符串尾部的空格
            return result;
        }
    };
    
    int main() {
        Solution solution;
        vector<string> dict = {"cat", "bat", "rat"};
        string sentence = "the cat was rat by the bat";
        string result = solution.replaceWords(dict, sentence);
        cout << "result = " << result << endl;
        return 0;
    
    }
  • 相关阅读:
    企业要主动淘汰五种人
    人力管理的核心:选、用、育、留
    张瑞敏:正确路线确定后,干部就是决定因素
    西点军校如何培养学员
    董事长如何找合适的搭档
    企业家何时应该放权
    会计基础视频
    同样劳动,为何结果不同?
    什么是真正的工作到位
    中国的人口和经济周期
  • 原文地址:https://www.cnblogs.com/learning-c/p/9898473.html
Copyright © 2020-2023  润新知