140. 单词拆分 II

Q:

给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,在字符串中增加空格来构建一个句子,使得句子中所有的单词都在词典中。返回所有这些可能的句子。

说明:

分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
示例 1:

输入:
s = “catsanddog”
wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]
输出:
[
“cats and dog”,
“cat sand dog”
]
示例 2:

输入:
s = “pineapplepenapple”
wordDict = [“apple”, “pen”, “applepen”, “pine”, “pineapple”]
输出:
[
“pine apple pen apple”,
“pineapple pen apple”,
“pine applepen apple”
]
解释: 注意你可以重复使用字典中的单词。
示例 3:

输入:
s = “catsandog”
wordDict = [“cats”, “dog”, “sand”, “and”, “cat”]
输出:
[]

A:

关键是要先用139的代码算一下给的字符串能不能被拆分为字典里的单词,不能直接返回。因为算是否能被拆分是O(N ^ 2),而本题要求的所有序列是O(N ^ 3),不先判断一下有一个全是’a’的用例是过不去的。。

C++:

 1 class Solution {
 2 public:
 3     vector<string> wordBreak(string s, vector<string>& wordDict) {
 4         vector<vector<string>> dp(s.size(),vector<string>());
 5         unordered_set<string> dic;
 6         for(auto& str:wordDict){
 7             dic.insert(str);
 8         }
 9         vector<bool> judge(s.size(),false);
10         for(int i=0;i<s.size();++i){
11             if(dic.count(s.substr(0,i+1))){judge[i]=true;}
12             else{
13                 for(int j=0;j<=i;++j){
14                     if(judge[j] and dic.count(s.substr(j+1,i-j))){
15                         judge[i]=true;
16                         break;
17                     }
18                 }
19             }
20         }
21         if(not judge.back()){return {};}
22         //dp[i]表示s截止到i的可能分割出的句子
23         for(int i=0;i<s.size();++i){
24             for(int j=0;j<=i;++j){
25                 if(dic.count(s.substr(j,i-j+1))){
26                     string str2=s.substr(j,i-j+1);
27                     if(j==0){
28                         dp[i].push_back(str2);
29                         continue;
30                     }
31                     if(not judge[j-1]){continue;}
32                     for(auto& str1:dp[j-1]){
33                         dp[i].push_back(str1+" "+str2);
34                     }
35                 }
36             }
37         }
38         return dp[s.size()-1];
39     }
40 };

python:

 1 class Solution:
 2     def wordBreak(self, s: str, WordDict):
 3         l=len(s)
 4         if not l:
 5             return 0
 6         dic={ x:1 for x in WordDict}
 7         #上一题代码拿来用
 8         judge=[0 for i in range(l)]
 9         judge[0]=int(s[0] in dic)  #i==0递推基础
10         for i in range(1,l):    #i==1开始遍历
11             if s[:i+1] in dic:
12                 judge[i]=1
13             else:
14                 for j in range(i):
15                     if judge[j] and s[j+1:i+1] in dic:
16                         judge[i]=1
17                         break
18         print(judge)
19         if not judge[-1]:
20             return []
21         #开始算本题所求的序列
22         dp=[[]for i in range(l)]  #dp[i]是一个列表,存储不同的s[1,i]闭区间的有效拆分序列
23         if s[0] in dic:
24             dp[0].append(s[0])  #递推起始状态
25         for i in range(1,l):    #从i==1开始自底向上递推
26             if s[:i+1] in dic: #s[0:i+1]本身就在字典里则加入解集
27                 dp[i].append(s[:i+1])
28             for cut in range(i):    #进行切分,符合条件的加入解集
29                 if s[cut+1:i+1] in dic and judge[cut]:
30                     #cut前面的子问题有解且cut之后到i的字符串在字典中
31                     for x in dp[cut]:   #x为字符串
32                         dp[i].append(x+' '+s[cut+1:i+1])  
33                         #把dp[cut]中的每个解加上cut后的字符串,插入dp[i]的解集
34         # print(dp)
35         return dp[-1]

猜你喜欢

转载自www.cnblogs.com/FdWzy/p/12288154.html