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"].

    Analyse:

    1. Recursion. Add one character at one time, continuously judge whether the remaining part can be found in the dictionary. If could, then add to the temporary string, if the size of temporary string is bigger than the original string, then we say the string can be segmented by the dictionary items. 

    Runtime: TIME LIMIT EXCEEDED. 

     1 class Solution {
     2 public:
     3     vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     4         vector<string> result;
     5         if(s.empty() || s.length() == 0) return result;
     6         
     7         helper(s, wordDict, 0, "", result);
     8         return result;
     9     }
    10     void helper(string s, unordered_set<string>& wordDict, int start, string temp, vector<string> result){
    11         if(start >= s.length()){//finish one test
    12             result.push_back(temp);
    13             return ;
    14         }
    15         string help;
    16         for(int i = start; i < s.length(); i++){
    17             help += s[i]; //append one word at a time
    18             if(wordDict.find(help) != wordDict.end()){
    19                 temp = (temp.length() == 0 ? help : temp + " " + help);
    20                 helper(s, wordDict, i + 1, temp, result);
    21             }
    22         }
    23     }
    24 };
    View Code

    2. Refer to this page

      Runtime: 8ms.

     1 class Solution {
     2 public:
     3     //2014-2-19 update
     4     vector<string> wordBreak(string s, unordered_set<string> &dict) 
     5     {
     6         vector<string> rs;
     7         string tmp;
     8         vector<vector<int> > tbl = genTable(s, dict);
     9         word(rs, tmp, s, tbl, dict);
    10         return rs;
    11     }
    12     void word(vector<string> &rs, string &tmp, string &s, vector<vector<int> > &tbl,
    13         unordered_set<string> &dict, int start=0)
    14     {
    15         if (start == s.length())
    16         {
    17             rs.push_back(tmp);
    18             return;
    19         }
    20         for (int i = 0; i < tbl[start].size(); i++)
    21         {
    22             string t = s.substr(start, tbl[start][i]-start+1);
    23             if (!tmp.empty()) tmp.push_back(' ');
    24             tmp.append(t);
    25             word(rs, tmp, s, tbl, dict, tbl[start][i]+1);
    26             while (!tmp.empty() && tmp.back() != ' ') tmp.pop_back();//tmp.empty()
    27             if (!tmp.empty()) tmp.pop_back();
    28         }
    29     }
    30     vector<vector<int> > genTable(string &s, unordered_set<string> &dict)
    31     {
    32         int n = s.length();
    33         vector<vector<int> > tbl(n);
    34         for (int i = n - 1; i >= 0; i--)
    35         {
    36             if(dict.count(s.substr(i))) tbl[i].push_back(n-1);
    37         }
    38         for (int i = n - 2; i >= 0; i--)
    39         {
    40             if (!tbl[i+1].empty())//if we can break i->n
    41             {
    42                 for (int j = i, d = 1; j >= 0 ; j--, d++)
    43                 {
    44                     if (dict.count(s.substr(j, d))) tbl[j].push_back(i);
    45                 }
    46             }
    47         }
    48         return tbl;
    49     }
    50 };
  • 相关阅读:
    Visual Studio 必备神器
    MVC中用Jpaginate分页 So easy!(兼容ie家族)
    仿花田:相亲网站 意中人 已在GitHub上开源
    仿花田:内部相亲网站 意中人(Asp.net MVC,Bootstrap2)
    Myeclipse最全快捷键
    SVG Viewer 3.0安装发现SVG Viewer License.txt无法介入写入,安装失败
    SVG报错error on line 39 at column 26: Namespace prefix xlink for href on script is not defined
    SVG可缩放矢量图形
    Google帮助IE浏览器实现对SVG支持
    JS代码判断IE6,IE7,IE8,IE9的函数代码
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4756117.html
Copyright © 2011-2022 走看看