zoukankan      html  css  js  c++  java
  • 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;
    
    }
  • 相关阅读:
    627. Swap Salary
    176. Second Highest Salary
    596. Classes More Than 5 Students
    183. Customers Who Never Order
    181. Employees Earning More Than Their Managers
    182. Duplicate Emails
    175. Combine Two Tables
    620. Not Boring Movies
    595. Big Countries
    HDU 6034 Balala Power! (贪心+坑题)
  • 原文地址:https://www.cnblogs.com/learning-c/p/9898473.html
Copyright © 2011-2022 走看看