Word Break II 字符串分割 动态规划+DFS

实验四 基于IDEA+SpringBoot+Maven+Thymeleaf的系统实现实验目的 通过这次实验,我学习了如何成功进行IDEA+SpringBoot+maven的系统实现,搭建好IDEA+SpringBoot+maven的系统环境,成功地安装并配置了maven的运行环境,学会了创建springboot的三种方式,并且能将之前的实验结合起来运用。pom.xml中引入thymeleaf依赖,在classpath:/templates/下新建HTML页面(可采用实验一创建的页面),新建controller进行测试,访问页面后返回success成功数据。同时掌握和thymeleaf的基本语法。 阅读详情

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"].

一开始直接用DFS来做,发现超时,思考了一下发现存在重复判断,比如第一次通过两个单词来到位置s[7],DFS后面的字符串无解,第二次通过一个单词直接来到s[7],此时又对后面的字符串进行了DFS,显然是多余的操作。

于是改用动态规划来记录是否有解,再用DFS得出结果。

/*2015.9.2cyq*/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
using namespace std;

//直接DFS,超时
class Solution1{
public:
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
		vector<string> result;
		string path;
		dfs(s,0,wordDict,path,result);
		return result;
    }
private:
	void dfs(string &s,int start,unordered_set<string>& wordDict,string &path,vector<string> &result){
		if(start==s.size()){
			int m=path.size();
			result.push_back(path);
			result.back().resize(m-1);
			return ;
		}
		for(int i=start;i<s.size();i++){
			string tmp=s.substr(start,i-start+1);
			if(wordDict.find(tmp)!=wordDict.end()){
				path+=tmp+" ";
				dfs(s,i+1,wordDict,path,result);
				path.resize(path.size()-tmp.size()-1);
			}
		}
	}
};

//动态规划DP
class Solution{
public:
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
		int n=s.size();
		vector<bool> canSolve(n+1,false);//canSolve[i]表示s[i,n-1]可分割成若干个单词
		canSolve[n]=true;
		//f[i][j]表示s[i,j]是一个字典中的单词且canSolve[j+1]=true;
		vector<vector<bool>> f(n,vector<bool>(n,false));
		for(int i=n-1;i>=0;i--){
			for(int j=i;j<n;j++){
				if(wordDict.find(s.substr(i,j-i+1))!=wordDict.end()&&canSolve[j+1]){
					f[i][j]=true;
					canSolve[i]=true;
				}
			}
		}
		vector<string> result;
		string path;
		dfs(s,0,f,canSolve,path,result);
		return result;
    }
private:
	void dfs(string &s,int start,vector<vector<bool>> &f,vector<bool> &canSolve,string &path,vector<string> &result){
		if(start==s.size()){
			int m=path.size();
			result.push_back(path);
			result.back().resize(m-1);
			return ;
		}
		if(!canSolve[start])
			return ;//剪枝
		for(int i=start;i<s.size();i++){
			if(f[start][i]){
				string tmp=s.substr(start,i-start+1);
				path+=tmp+" ";
				dfs(s,i+1,f,canSolve,path,result);
				path.resize(path.size()-tmp.size()-1);
			}
		}
	}
};


int main(){
	string a[]={"cat","cats","and","sand","dog"};
	vector<string> dict(a,a+sizeof(a)/sizeof(string));
	unordered_set<string> wordDict;
	for(int i=0;i<dict.size();i++)
		wordDict.insert(dict[i]);
	Solution solu;
	string s="catsanddog";
	vector<string> ret=solu.wordBreak(s,wordDict);
	for(auto it=ret.begin();it!=ret.end();++it){
		cout<<*it<<endl;
	}
	return 0;
}


逆向解析M团外卖mtgsig与waimai_sign加密参数实战指南⊙ω⊙ 本文详细解析了M团外卖App中mtgsig与waimai_sign两个核心加密参数的逆向分析与生成方法。通过抓包定位、静态逆向与Frida动态调试,拆解了waimai_sign的RSA签名逻辑与mtgsig的复杂参数组装加密流程,并提供了Python模拟实现的框架与调试心得,为安全研究及合规数据分析提供实战指南。 阅读详情

相关推荐

别再只用STM32了!手把手教你用STM32F4+FPGA EP2C8搭建低成本多轴运动控制器(附S形加减速算法避坑)

本文详细介绍了如何利用STM32F4与FPGA EP2C8搭建低成本多轴运动控制器,解决传统方案在脉冲输出频率、多轴同步性和实时响应方面的瓶颈。通过硬件设计、软件架构和S形加减速算法的优化,实现商用级性能,成本控制在500元以内,适合创客和小型自动化设备开发。

weixin_28693683的博客 305

动态规划—分割回文串-ii 解析+代码

分割回文串

m0_72563041的博客 431

学习使用turtlebot2——ROS上安装turtlebot2

安装环境:    安装Ubuntu 14.04版本和ROS Indigo    参考:http://wiki.ros.org/turtlebot/Tutorials安装步骤    有两种安装方法,一种直接的安装命令,另外一种源码编译安装,区别在于:直接安装命令是安装在电脑默认路径/opt/ros/indigo/share里面不便于更改代码,因为默认路径下文件一般有访问权限,无法直接更改,需要使用s...

weixin_42670641的博客 2537

动态规划 - 字符串分割(Word Break) + 三角矩阵(Triangle)

动态规划 - 字符串分割(Word Break) + 三角矩阵(Triangle)

xaiobit_hl的博客 591

Matlab图像处理频域滤波实现——高斯低通、高通、带通带阻滤波器

如果你想要进一步了解更多的相关知识,

小嘤熊的博客 1万+

Cadence HDL原理图库避坑指南:从Excel导入引脚到解决‘V- V+’报错(附FPGA实例)

本文详细介绍了Cadence HDL原理图库的高效构建与排错方法,重点讲解如何从Excel智能导入引脚数据并优化FPGA封装设计。通过数据预处理、智能导入和深度排错三个关键阶段,帮助工程师避免常见错误如‘V- V+’报错,提升设计效率和准确性。

weixin_28739079的博客 242

Leetcode:给定一个字符串s和一个单词字典dict,确定是否可以将s分割成一个由一个或多个字典单词组成的空格分隔的序列。

Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words. For example, givens="leetcode",dict=["leet", "code"...

LeetCode:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum 2427

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

shuyisuisuinian的博客 2万+

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", dict = ["leet"

机器不学习 1309

R语言分段回归

1.利用虚拟变量进行线性回归 (MEdata.csv的数据来源:https://github.com/ChenQihome9/CSDN-Data-Library/blob/master/MEdata.csv) 问题13:针对TAX~GDP,使用分段回归方法,找到其回归方程。编程,并根据程序结果叙述如下几方面问题:(1)分几段最佳?(不超过5段内)(2)分段位置定在哪儿最佳?(3)写出最佳方...

ChenQihome9的博客 1万+

手把手教你学Simulink--基础储能管理场景实例:基于Simulink的锂电池健康状态(SOH)在线评估仿真

本文详细介绍了基于Simulink的锂电池健康状态(SOH)在线评估仿真方法。主要内容包括:1)SOH定义与量化指标(容量SOH、内阻SOH及综合SOH);2)基于Thevenin模型参数辨识和递推最小二乘法(RLS)的在线评估原理;3)100次充放电循环的仿真场景设置;4)Simulink建模步骤(主电路搭建、核心模块实现及信号流连接);5)仿真结果分析,显示SOH评估误差<2%,响应时间45秒,满足在线评估需求;6)提出多参数融合评估、深度学习辅助辨识等优化方向。该方法为储能系统寿命管理提供了有效

MHD0815的博客 184

leetcode之word-break(动态规划)

题目 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s =“leetcode”, dict =[“leet”, “c...

此心光明,亦复何言~ 399

车载摄像头EMC问题剖析

车载摄像头作为智能驾驶核心部件,其EMC问题日益突出。某案例中,摄像头在48MHz~175MHz频段辐射超标。通过控制变量法排查,发现网络变压器地环路和共模抑制不足是主因。整改措施包括优化接地隔离、增加共模滤波器和使用屏蔽网线,最终通过EMC测试。案例表明,EMC问题需从系统耦合角度解决,建议设计阶段预防地环路,在高速信号链中加入滤波隔离措施。全文150字。

硬核科技 279

leetcode python word-break-ii

第一篇博客word-break-ii题意思路暴力dfs python实现dp+dfs python实现 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. R...

clearsunshinel的博客 246

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

leetcode 140. Word Break II 深度优先搜索DFS + 很棒的动态规划DP 做法 + 记录前驱节点

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. You may assume the dictio

JackZhangNJU的专栏 848

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

[leetcode]Word Break II @ Python

原题地址:https://oj.leetcode.com/problems/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...

weixin_34010566的博客 144

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

leetcode word break I && II

Two dynamic programming problem problem II can use the code of problem I Word Break I class Solution { public: int longestWord(unordered_set dict) { unordered_set::iterator it;

gunxueqiucjw的专栏 854

【LeetCode】132.Word Break II

题目描述(Hard) Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all su...

YTea的博客 139

LeetCode Word Break II

原题链接在这里:https://leetcode.com/problems/word-break-ii/ 题目: Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each word is a valid dictionary word. Return ...

weixin_30449239的博客 74

[LeetCode] Word Break II

public List wordBreak(String s, Set dict) { int n=s.length(); boolean[] dp=new boolean[n+1]; dp[0]=true; List> words = new ArrayList>(); for (int i = 0; i <= n; i++)

jhbxlx的专栏 639

Word Break II (LeetCode)

Question: https://oj.leetcode.com/problems/word-break-ii/ 解答: 跟Word Break类似,只不过把bool变量变成vector<int>,保存所有能够跟当前点形成一个有效word的点,并且那个点本身的vector<int>不为空(等同于bool flag = true)。所以我们从点(n-1)开始回溯...

weixin_30535043的博客 71

LeetCode 140. Word Break II (DP+DFS)

直接递归求解TLE(Time Limit Exceeded), 用vector<vector<string>> ret(s.size()+1)保存wordBreak的解MLE(Memory Limit Exceeded), 采用DP+DFS通过测试。 在LeetCode 139. Word Break的中,用dp[i]标识s.substr(0, i)是否可以由dict里的字符串组成; 在此我们用dp[i]标识s.substr(0, i)的workBreak解的最后一个单词的集合。 如输入["abcde

Stephen Wong的专栏 3677

140. Word Break II

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible...

weixin_41704428的博客 148

LeetCode之“动态规划”:Word Break && Word Break II

 1. Word Break   题目链接   题目要求:   Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.   For exampl...

524

leetcode之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 =“catsa...

此心光明,亦复何言~ 451
上一篇: Word Ladder II 相邻字符串接龙 BFS
下一篇: Maximum Gap 寻找数组中排序后相邻两个数的最大差值,桶排序,O(n)
tuzigg123
博客等级 码龄15年 35粉丝 142原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值