字符串断开问题(word break II) (LeetCode--递归篇) 难

给定一个字符串s和一个单词词典,在s中添加空格来构造一个句子,每个单词都是一个有效的字典单词。返回所有可能的句子。例如,给定s ="catsanddog", dict =["cat", "cats", "and", "sand", "dog"]。解决办法是“cats and dog”、“cat sand dog”。

1、暴力递归

解题思路:

如果从0到i的字符串是一个单词,则继续使用rest子字符串进行计算

递归过程中,若当前已到最后一个字母,则压入结果容器中

超时!!!

cpp文件

#include "stdafx.h";
#include "restoreIPAddresses.h";
using namespace std;

vector<string> Solution::wordBreak(string s, unordered_set<string> &dict) {
vector<string> res;
string list = "";
subbreak(s, list, dict, 0, res);
return res;
};
void Solution::subbreak(string s, string list, unordered_set<string>& dict, int len, vector<string>& res){
if (len == s.length()) {
res.push_back(list);
}
for (int j = 1; j <= s.length() - len; j++){
string s1 = s.substr(len, j);
unordered_set<string>::const_iterator got = dict.find(s1);
if (got == dict.end())
continue;
if (list == "")
list = s1;
else{
list = list + " ";
list += s1;
}
subbreak(s, list, dict, len + j, res);
list = "";
}


}

h文件

#include "stdafx.h";
using namespace std;
class Solution {
public:
static vector<string> wordBreak(string s, unordered_set<string> &dict);
static void subbreak(string s, string list, unordered_set<string>& dict, int len, vector<string>& res);
};

main文件

#include "stdafx.h"
#include "restoreIPAddresses.h";
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
string s = "catsanddog";
unordered_set<string> dict = { "cat", "cats", "and", "sand", "dog" };
Solution::wordBreak(s, dict);


return 0;

}

stdafx.h文件

#pragma once


#include "targetver.h"


#include <stdio.h>
#include <tchar.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include <hash_set>
#include <iostream>
#include <algorithm>
#include <sstream>

#include <unordered_set>


2、动态规划

参考:https://github.com/summer00072/leetcode/blob/master/algorithms/cpp/wordBreak/wordBreak.II.cpp

//---------------------
 // Dynamic Programming
 //---------------------
 //
 // Define substring[i, j] is the sub string from i to j.
 //
 // (substring[i,j] == word) : result[i] = substring[i,j] + {result[j]}
 //
 // So, it's better to evaluate it backword.
 //
 // For example:
 //
 // s = "catsanddog",
 // dict = ["cat", "cats", "and", "sand", "dog"].
 //
 // 0 c "cat" -- word[0,2] + {result[3]} ==> "cat sand dog"
 // "cats" -- word[0,3] + {result[4]} ==> "cats and dog"
 // 1 a ""
 // 2 t ""
 // 3 s "sand" -- word[3,6] + {result[7]} ==> "sand dog"
 // 4 a "and" -- word[4,6] + {result[7]} ==> "and dog"
 // 5 n ""
 // 6 d ""
 // 7 d "dog"
 // 8 o ""
 // 9 g ""
  
 vector<string> wordBreak_dp(string s, set<string> &dict) {
 vector< vector<string> > result(s.size());
  
 for(int i=s.size()-1; i>=0; i--) {
 vector<string> v;
 result[i] = v;
 for(int j=i+1; j<=s.size(); j++) {
 string word = s.substr(i, j-i);
 if (dict.find(word) != dict.end()){
 if (j==s.size()){
 result[i].push_back(word);
 }else{
 for(int k=0; k<result[j].size(); k++){
 result[i].push_back(word + " " + result[j][k]);
 }
 }
 }
 }
 }
  
 return result[0];
 }
 

牛客网未完全通过

原因是顺序的问题




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值