648.replace words

Posted learning-c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

}

 

以上是关于648.replace words的主要内容,如果未能解决你的问题,请参考以下文章

648. Replace Words 替换单词

648. Replace Words(LeetCode)

648. Replace Words (Medium)

Trie-648. Replace Words

LeetCode 648. Replace Words (单词替换)

648. Replace Words 替换成为原来的单词