Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

[解题思路]

DFS + backtracking

在大数据:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]通不过。

 

C++实现代码:

#include<iostream>

#include<unordered_set>

#include<string>

#include<cstring>

using namespace std;



class Solution

{

public:

    bool wordBreak(string s, unordered_set<string> &dict)

    {

        int len=s.length();

        if(len==0)

            return true;

        bool dp[len+1];

        memset(dp,0,sizeof(dp));

        int i,j;

        for(i=1; i<=len; i++)

        {

            if(dp[i]==false&&dict.count(s.substr(0,i))>0)

                dp[i]=true;

            if(i==len&&dp[i]==true)

                break;

            if(dp[i]==true)

            {

                for(j=i+1; j<=len; j++)

                {

                    if(dp[j]==false&&dict.count(s.substr(i,j-i))>0)

                        dp[j]=true;

                    if(j==len&&dp[j]==true)

                        break;

                }

            }

        }

        if(i==len||j==len)

            return dp[i]||dp[j];

        return false;

    }

};



int main()

{

    unordered_set<string> dict= {"cat", "cats", "and", "sand", "dog"};

    Solution s;

    string ss = "catsanddog";

    cout<<s.wordBreak(ss,dict)<<endl;

}

 2.DP+DFS

使用Word Break对字符串进行处理,处理结束后使用DFS进行DFS。

#include<iostream>

#include<unordered_set>

#include<string>

#include<cstring>

#include<vector>

using namespace std;



class Solution

{

public:

    vector<string> ss;

    vector<string> wordBreak(string s, unordered_set<string> &dict)

    {

        // IMPORTANT: Please reset any member data you declared, as

        // the same Solution instance will be reused for each test case.

        int n=s.size();

        ss.clear();

        bool dp[n];

        memset(dp,false,sizeof(dp));

        for(int i=0; i<n; i++)

            dp[i]=false;

        for(int j=n-1; j>=0; j--)

        {

            for(int i=j; i<n; i++)

            {

                string t(s.begin()+j,s.begin()+i+1);

                if( dict.find(t)!=dict.end() )

                {

                    if( ( i+1<n && dp[i+1])|| i==n-1)

                        dp[j]=true;

                }

            }

        }

        dfs(0,n,s,string (""),dict,dp);

        return ss;

    }

    void dfs(int st,int n,string str,string cur,unordered_set<string> &dict,bool dp[])

    {

        if(st==n)

        {

            ss.push_back(cur);

            return ;

        }

        for(int i=st; i<n; i++)

        {

            string t(str.begin()+st,str.begin()+i+1);

            if((dp[i+1]||i+1==n) &&  (dict.find(t)!=dict.end() ))

            {

                int c=cur.size();

                cur+=t;

                if(i<n-1)

                    cur.push_back(' ');

                dfs(i+1,n,str,cur,dict,dp);

                cur.resize(c);

            }

        }

    }

};



int main()

{

    unordered_set<string> dict= {};

    Solution s;

    string ss = "a";

    vector<string> result=s.wordBreak(ss,dict);

    for(auto a:result)

        cout<<a<<endl;

}

参考:http://blog.csdn.net/cs_guoxiaozhu/article/details/14104789

你可能感兴趣的:(break)