码迷,mamicode.com
首页 > 其他好文 > 详细

648.replace words

时间:2018-11-02 23:56:30      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:name   his   color   which   public   word   orm   ict   als   

 

 

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 << "\t\t\t";
                }
                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

标签:name   his   color   which   public   word   orm   ict   als   

原文地址:https://www.cnblogs.com/learning-c/p/9898473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!