LeetCode 648. 單詞替換 (字典樹、字符串分割)

單詞替換
思路:將詞根單詞插入字符串、然後檢索每個單詞即可。
vector<char>記錄下路徑上的字符,再用string(首迭代器,尾迭代器)即可。
手打分割字符串

class Solution {
    struct Node{
        bool isEnd=false;
        Node* children[26] = {0};
    };
    Node *root = new Node;

    void insert(const string &s){
        Node *p = root;
        for(char c:s){
            if(!p->children[c-'a']){
                p->children[c-'a'] = new Node;
            }
            p = p->children[c-'a'];
        }
        p->isEnd = true;
    }

    string ask(const string &s){
        Node *p = root;
        vector<char> cs;
        for(char c:s){
            if(!p->children[c-'a']){
                return "";
            }
            cs.push_back(c);
            p = p->children[c-'a'];
            if(p->isEnd){
                return string(cs.begin(),cs.end());
            }
            
        }
        return "";
    }
public:
    string replaceWords(vector<string>& dict, string sentence) {
        vector<string> wordList;
        int l=0,r=0;
        while(r<sentence.size()){
            while(sentence[r]!=' ' && r<sentence.size()){
                r++;
            }
            wordList.push_back(sentence.substr(l,r-l));
            l = r;
            while(sentence[l]==' '){
                l++;
            }
            r = l;
        }
        for(string &s:dict){
            insert(s);
        }
        for(string &s:wordList){
            string ns = ask(s);
            if(ns.size())
                s = ns;
        }
        string ans="";
        for(int i=0;i<wordList.size();i++){
            if(!i){
                ans+=wordList[i];
            }else{
                ans+=" "+wordList[i];
            }
        }
        return ans;
    }
};

主要使用一下strtok函數,注意使用之後原來的字符串被更改掉了,如果仍需使用,要預先拷貝一份。

//const char* delim = ...; 
//char* str = ...;
char* token = strtok(str,delim);
while(token){
//…………………………
    token = strtok(nullptr," ");
}
class Solution {
    struct Node{
        bool isEnd=false;
        Node* children[26] = {0};
    };
    Node *root = new Node;

    void insert(const string &s){
        Node *p = root;
        for(char c:s){
            if(!p->children[c-'a']){
                p->children[c-'a'] = new Node;
            }
            p = p->children[c-'a'];
        }
        p->isEnd = true;
    }

    string ask(const string &s){
        Node *p = root;
        vector<char> cs;
        for(char c:s){
            if(!p->children[c-'a']){
                return "";
            }
            cs.push_back(c);
            p = p->children[c-'a'];
            if(p->isEnd){
                return string(cs.begin(),cs.end());
            }
            
        }
        return "";
    }
public:
    string replaceWords(vector<string>& dict, string sentence) {
        vector<string> wordList;
        char* token = strtok((char*)sentence.c_str()," ");
        while(token){
            wordList.push_back(token);
            token = strtok(nullptr," ");
        }
        for(string &s:dict){
            insert(s);
        }
        for(string &s:wordList){
            string ns = ask(s);
            if(ns.size())
                s = ns;
        }
        string ans="";
        for(int i=0;i<wordList.size();i++){
            if(!i){
                ans+=wordList[i];
            }else{
                ans+=" "+wordList[i];
            }
        }
        return ans;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章