给定一个字符串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]; | |
} | |
牛客网未完全通过
原因是顺序的问题