zoukankan      html  css  js  c++  java
  • 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
    public class Solution {
        public ArrayList<String> wordBreak(String s, Set<String> dict) {
            ArrayList<String> result = new ArrayList<String>();
            if(s == null || s.length() == 0)
                return result;
            
            int len = s.length();
            boolean[] dp = new boolean[len+1];
            dp[0] = true;
            for(int i = 0; i< len ; i++){
                if(!dp[i])
                    continue;
                
                for(String e : dict){
                    int end = i+ e.length();
                    if(end > len)
                        continue;
                    String sub = s.substring(i, end);
                    if(dict.contains(sub))
                        dp[end]  = true;
                }
            }
            
            if(dp[len] == false) return result;
            StringBuilder  sb = new StringBuilder();
            dfs(0, sb, s, dict, result);
            return result;
        }
        
        private void dfs(int start, StringBuilder sb, String s, Set<String> dict, ArrayList<String> result){
            int len = s.length();
            if(start >= len){
                result.add(new String(sb));
                return;
            }
            
            for(int i = start+1; i<= len; i++){
                String sub = s.substring(start, i);
                if(dict.contains(sub)){
                    int oldLen = sb.length();
                    if(oldLen != 0){
                        sb.append(" ");
                    }
                    sb.append(sub);
                    dfs(i, sb, s, dict, result);
                    sb.delete(oldLen, sb.length());
                }
            }
        }
    }

    DP + Backtrack

        // DP + back track
        public ArrayList<String> wordBreak2(String s, Set<String> dict) {
            int n = s.length();
            ArrayList<ArrayList<Integer>> pres = new ArrayList<ArrayList<Integer>>(
                    n);
            // initialize
            for (int i = 0; i < n; ++i)
                pres.add(new ArrayList<Integer>());
            // DP. pres[i] stores position j where should insert space
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j <= i; ++j) {
                    String suffix = s.substring(j, i + 1);
                    if ((j == 0 || pres.get(j - 1).size() > 0) && dict.contains(suffix))
                        pres.get(i).add(j);
                }
            }
            return getPath(s, n, pres);
        }
    
        public ArrayList<String> getPath(String s, int n, ArrayList<ArrayList<Integer>> pres) {
            ArrayList<String> res = new ArrayList<String>();
            for (int pre : pres.get(n - 1)) {
                if (pre == 0) {
                    res.add(s.substring(0, n));
                } else {
                    ArrayList<String> preres = getPath(s, pre, pres);
                    String sub = s.substring(pre, n);
                    for (String ss : preres)
                        res.add(ss + " " + sub);
                }
            }
            return res;
        }
  • 相关阅读:
    【C/C++开发】c++ 工具库 (zz)
    【机器学习】半监督学习
    【Python开发】Pycharm下的Anaconda配置
    【C/C++开发】emplace_back() 和 push_back 的区别
    【C/C++开发】容器set和multiset,C++11对vector成员函数的扩展(cbegin()、cend()、crbegin()、crend()、emplace()、data())
    【C/C++开发】C++11 并发指南三(std::mutex 详解)
    【C/C++开发】C++11 并发指南二(std::thread 详解)
    【C/C++开发】C++11 并发指南一(C++11 多线程初探)
    【C/C++开发】STL内嵌数据类型: value_type
    个股实时监控之综述
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3555217.html
Copyright © 2011-2022 走看看