leetcode 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 rootforming 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"
思路:

尝试用字典树来查找,但是memory limit,最后一个过不了,但是结果是对的,先记录一下。第一次写字典树。

struct node{
    bool flag; 
    map<int,node*> next1;  
    node() {
        flag = 0; 
    }
};
class Solution {
public:
    node * root;
    void f(vector<string>& dict) {  
        for(int i = 0; i < dict.size(); i++) {
            node * t = root;
            for(int j = 0; j < dict[i].size(); j++) {
                if(t->next1.find(dict[i][j]-'a') == t->next1.end()) {
                    t->next1[dict[i][j]-'a'] = new node;
                }
                t = t->next1[dict[i][j]-'a'];
            }
            t->flag = 1;  
        }
    }
    string g(string & str) {
        int j = 0;
        node* head = root;
        while(j<str.size()) {
            if(head->next1.find(str[j]-'a')!=head->next1.end()) {                                        
                if(head->next1[str[j]-'a']->flag == 1) return str.substr(0,j+1);
                head = head->next1[str[j]-'a'];
                j++;
            } else {
                break;
            }
        }
        return str;
    }
    string replaceWords(vector<string>& dict, string sentence) {
        root = new node;
        f(dict);
        char* spliter = " ";
        vector<string> index;
        char * pch;
        pch = strtok( (char*)sentence.c_str(), spliter );
        while( pch != NULL ) {
            index.emplace_back(string(pch));
            pch = strtok( NULL, spliter );
        }
        string re;
        for(auto val:index) {
            re = re + " " + g(val);
        }
        delete root;
        return re.substr(1);
    }
};



我不知道为什么别人用数组来存son节点都行,我这个就过不了。等在仔细看看。

class Solution {
public:
    struct TrieNode{
        bool _is_end;
        TrieNode * next[26];
        TrieNode(){
            _is_end = false;
            for (int i=0; i<26; i++) {
                next[i] = NULL;
            }
        };
    };

    TrieNode * root;

    void init(vector<string> & dict) {
        root = new TrieNode();
        TrieNode * temp=NULL;
        for (int i=0; i<dict.size(); i++) {
            temp = root;
            for (int j=0; j<dict[i].size(); j++) {
                int index = dict[i][j] - 'a';
                if (temp->next[index] == NULL) {
                    temp->next[index] = new TrieNode();
                }
                temp = temp->next[index];
            }
            temp->_is_end = true;
        }
    }

    vector<string> split_str(string sentence) {
        vector<string> ret;
        while(true) {
            int index = sentence.find(' ');
            if (index == string::npos) {
                ret.push_back(sentence);
                break;
            }
            ret.push_back(sentence.substr(0,index));
            sentence = sentence.substr(index+1);
        }
        return ret;
    }

    string process(string word) {
        int size = word.size();
        TrieNode * temp = root;
        for (int i=0; i<size; i++) {
            int index = word[i] - 'a';
            if (temp->next[index] == NULL) {

                return word;
            } else if (temp->next[index]->_is_end==true) {
                return word.substr(0,i+1);
            }
            temp = temp->next[index];
        }
        return word;
    }

    void replace(vector<string>& words) {
        for(int i=0; i<words.size(); i++) {
            words[i] = process(words[i]);
        }
    }

    string get_result_str(vector<string> & words) {
        string ret;
        for (int i=0; i<words.size(); i++) {
            if(i == words.size() - 1) {
                ret += words[i];
            } else {
                ret += words[i] + " ";
            }
        }
        return ret;
    }

    string replaceWords(vector<string>& dict, string sentence) {
        init(dict);
        vector<string> words = split_str(sentence);
        replace(words);
        string ret = get_result_str(words);
        return ret;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值