139-Word Break

问题描述

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

解释:

给定一个字符串s,一个字符串构成的字典wordDict, 问 能否有wordDict若干个word构成s

基本思想

穷举法

wordDict中假设有\(n\)个词,s从第一个词开始找wordDict中的词进行核对, 核对完毕之后,进行标记(dfs)。找到第一个词之后,接着找第二个词,wordDict中的所有词构成候选项(为了防止一个词重复出现多次)。只要有一条路径能遍历完s,那么就说明 wordDict中存在着可以构成s的词语。

穷举代码

C++

    bool isOk(string &s, int& i, string& t) {
        int j=0;
        while(i<s.size() && j<t.size() && s[i] == t[j]) {
            ++i;
            ++j;
        }
        if (j==t.size()) return true;
        return false;
    }
     bool dfs(string&s, int low, map<char, vector<string>>&hash) {
        if (low >= s.size()) return true;
        auto it = hash.find(s[low]);
        if (it==hash.end()) return false;
        vector<string> &vec = it->second;
        for(int i=0; i<vec.size();++i) {
            int temp = low;
            cout<<vec[i]<<endl;
            if (!isOk(s,temp,vec[i])) continue;
            if (dfs(s,temp,hash)) return true;
        }
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        if (n<=0) return true;
        map<char, vector<string>> hash;
        for(int i=0;i<wordDict.size();++i) {
            string& word = wordDict[i];
            if (word.size()<=0) continue;
            if (hash.find(word[0])==hash.end()) {
                vector<string> t;
                t.push_back(word);
                hash[word[0]] = t;
            } else {
                hash[word[0]].push_back(word);
            }
        }
        bool res = dfs(s,0,hash);
        
        return res;
    }

对穷举方法进行改进:
使用 set数据类型,使用string.substr(start, length) 截取字符串,使用set.count 判断集合中是否存在某元素

      bool dfs(string&s, int low, map<char, vector<string>>&hash, set<string>&strSet) {
        if (low >= s.size()) return true;
        auto it = hash.find(s[low]);
        if (it==hash.end()) return false;
        vector<string> &vec = it->second;
        for(int i=0; i<vec.size();++i) {
            cout<<vec[i]<<endl;
            if (!strSet.count(s.substr(low, vec[i].size()))) continue;
            if (dfs(s,low+vec[i].size(),hash, strSet)) return true;
        }
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        if (n<=0) return true;
        map<char, vector<string>> hash;
        for(int i=0;i<wordDict.size();++i) {
            string& word = wordDict[i];
            if (word.size()<=0) continue;
            if (hash.find(word[0])==hash.end()) {
                vector<string> t;
                t.push_back(word);
                hash[word[0]] = t;
            } else {
                hash[word[0]].push_back(word);
            }
        }
        set<string> strSet(wordDict.begin(), wordDict.end());
        bool res = dfs(s,0,hash,strSet);
        
        return res;
    }

python

动态规划

     bool dfs(string&s, int low, map<char, vector<string>>&hash, set<string>&strSet) {
        if (low >= s.size()) return true;
        auto it = hash.find(s[low]);
        if (it==hash.end()) return false;
        vector<string> &vec = it->second;
        for(int i=0; i<vec.size();++i) {
            cout<<vec[i]<<endl;
            if (!strSet.count(s.substr(low, vec[i].size()))) continue;
            if (dfs(s,low+vec[i].size(),hash, strSet)) return true;
        }
        return false;
    }

    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        if (n<=0) return true;
        map<char, vector<string>> hash;
        for(int i=0;i<wordDict.size();++i) {
            string& word = wordDict[i];
            if (word.size()<=0) continue;
            if (hash.find(word[0])==hash.end()) {
                vector<string> t;
                t.push_back(word);
                hash[word[0]] = t;
            } else {
                hash[word[0]].push_back(word);
            }
        }
        set<string> strSet(wordDict.begin(), wordDict.end());
        bool res = dfs(s,0,hash,strSet);
        
        return res;
    }
posted @ 2024-05-23 22:15  金字塔下的蜗牛  阅读(2)  评论(0编辑  收藏  举报