当前位置:网站首页>leetcode:140. 单词拆分 II
leetcode:140. 单词拆分 II
2022-08-03 02:04:00 【OceanStar的学习笔记】
题目来源
题目描述


题目解析
暴力递归
class Solution {
void process(string &s, std::set<std::string> &dict, int start, std::string &path, vector<string> &ans){
int N = s.size();
if(start == N){
ans.push_back(path);
return;
}
for (int end = start; end < N; ++end) {
std::string prefix = s.substr(start, end - start + 1);
if(dict.count(prefix)){
std::string tmp = path;
if(tmp.empty()){
path = prefix;
}else{
path = path.append(" ").append(prefix);
}
process(s, dict, end + 1, path, ans);
path = tmp;
}
}
}
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
std::set<std::string> dict(wordDict.begin(), wordDict.end());
vector<string> ans;
std::string path;
process(s, dict, 0, path, ans);
return ans;
}
};
优化:动态规划求是否有解,回溯算法搜索所有符合条件的解
class Solution {
struct Node{
std::string path;
bool end;
std::vector<Node *> nexts;
Node(){
end = false;
nexts.resize(26);
}
};
Node * getTrie(vector<string>& wordDict){
Node * root = new Node;
for(auto & word : wordDict){
Node *node = root;
for(char ch : word){
int idx = ch - 'a';
if(node->nexts[idx] == nullptr){
node->nexts[idx] = new Node;
}
node = node->nexts[idx];
}
node->path = word;
node->end = true;
}
return root;
}
std::vector<bool> getDP(string &s, Node *root){
int N = s.size();
std::vector<bool> dp(N + 1, false);
dp[N] = true;
for (int start = N - 1; start >= 0; --start) {
Node *curr = root;
for (int end = start; end < N; ++end) {
int idx = s[end] - 'a';
curr = curr->nexts[idx];
if(curr == nullptr){
break;
}
if(curr->end && dp[end + 1]){
dp[start] = true;
break;
}
}
}
return dp;
}
// str[index.....] 是要搞定的字符串
// dp[0...N-1] 0... 1.... 2... N-1... 在dp里
// root 单词表所有单词生成的前缀树头节点
// path str[0..index-1]做过决定了,做的决定放在path里
void process(string str, int idx, Node *root,
std::vector<bool> &dp, std::vector<string>& path,
std::vector<string> & ans){
if(idx == str.size()){
string builder;
for (int i = 0; i < path.size() - 1; ++i) {
builder = builder.append(path[i] + " ");
}
builder.append(path[path.size() - 1]);
ans.push_back(builder);
}else{
Node *curr = root;
for (int end = idx; end < str.size(); ++end) {
// str[i..end] (能不能拆出来)
int road = str[end] - 'a';
curr = curr->nexts[road];
if(curr == nullptr){
break;
}
if(curr->end && dp[end + 1]){
path.push_back(curr->path);
process(str, end + 1, root, dp, path, ans);
path.pop_back();
}
}
}
}
public:
vector<string> wordBreak(string s, vector<string>& wordDict) {
Node * tree = getTrie(wordDict);
std::vector<bool> dp = getDP(s, tree);
std::vector<string> path;
std::vector<string> ans;
process(s, 0, tree, dp, path, ans);
return ans;
}
};
边栏推荐
猜你喜欢
随机推荐
什么样的存储服务,才能成为企业数字化创新“加速器”?
超级复杂可贴图布局的初级智能文本提示器
[Arduino] Reborn Arduino Monk (2)----Arduino Language
容联云发送验证码
rancher集成ldap,实现统一账号登录
韦东山 数码相框 项目学习(五)libjpeg-turbo的移植
复杂多层布局的初级智能文本提示器
lombok 下的@Builder和@EqualsAndHashCode(callSuper = true)注解
EasyGBS播放器优化:设备通道视频播放出现跳屏问题的修复
Kook机器人开发日志01
【社媒营销】Facebook速推帖子如何运作?值得吗?
禁用token及无感知更新token功能实现
PHICOMM(斐讯)N1盒子 - Armbian5.77(Debian 9)配置自动连接WIFI无线网络
面试题整理1
44LVS负载均衡群集-NAT
DJI内推码(2022年8月2日更新)
flask-socketio实现websocket通信
常用工具链和虚拟环境-Cygwin
[QNX Hypervisor 2.2用户手册]10 虚拟设备参考
vs studio install opencv environment









