Leetcode 139. Word Break dp以及kmp的失败尝试

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Note:

  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.

Example 1:

Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode"can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple"can be segmented as "apple pen apple".
             Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false

题目链接:https://leetcode.com/problems/word-break/

class Solution {
public:
    bool find(string s,vector<string> word)
    {
        for(int i=0;i<word.size();i++)
        {
            if(s==word[i])
                return true;
        }
        return false;
    }
    bool wordBreak(string s, vector<string>& wordDict) {
        int len=s.length();
        vector<bool> dp(len+1,false);
        dp[0]=true;
        for(int i=1;i<=len;i++)
        {
            for(int j=i-1;j>=0;j--)
            {
                if(dp[j]&&find(s.substr(j,i-j),wordDict))
                {
                    dp[i]=true;
                    break;//只要dp[i]=true,就可以结束了
                }
            }
        }
        return dp[len];
    }
};

记录下的我的失败kmp尝试,超时了,但是增强了对kmp的应用

class Solution {
public:
    
void get_next(string s,vector<int>&next)
{
    next[0]=-1;
    int n=s.length();
    int i=0,j=-1;
    while(i<n)
    {
        if(j==-1||s[i]==s[j])
        {
            i++,j++;
            next[i]=j;
        }
        else
        {
            j=next[j];
        }
    }
}
    
    
bool kmp(string longs,string shorts)
{
    vector<int> next(shorts.length()+1,0);
    get_next(shorts,next);
    int i=0,j=0,n=longs.length();
    while(i<n)
    {
        if(longs[i]==shorts[j])
        {
            i++,j++;
            if(j==shorts.length())
            {
                return true;
            }
        }
        else
        {
            j=next[j];
            if(j==-1)
            {
                i++,j++;
            }
        }
    }
    return false;
}

void fun(string s,int start,vector<string>&word,bool &res)
{
    if(start>=s.length())
        res=true;
    else
    {
         int c=0;
       for(int i=0;i<word.size();i++)
        {
            if(s[start]==word[i][0]&&kmp(s.substr(start,word[i].length()/*s.length()-start*/),word[i]))
            {
                //cout<<"hello"<<start<<s.substr(start,s.length()-start)<<endl;
                fun(s,start+word[i].length(),word,res);
            }
            else
            {
               c++; 
            }
        }
    if(c==word.size()&&res==false)
        res=false;
    }
       
}
    
bool wordBreak(string s, vector<string>& wordDict) {
    bool res=false;
    fun(s,0,wordDict,res);
    return res;
    }
};

猜你喜欢

转载自blog.csdn.net/salmonwilliam/article/details/87645315