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 };
  • 相关阅读:
    木马控制技术(二) -- 端口复用
    OSQL.EXE 命令行下脱裤mssql
    移动App中常见的Web漏洞
    phpcms前台任意代码执行漏洞(php<5.3)
    LeetCode OJ--Copy List with Random Pointer **
    LeetCode OJ-- Container With Most Water
    LeetCode OJ--Combinations *
    LeetCode OJ-- Combination Sum II **
    LeetCode OJ--Combination Sum **
    LeetCode OJ-- Candy **
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4756117.html
Copyright © 2011-2022 走看看