当前位置: 首页 > article >正文

C++算法-动态规划2

第 4 题 字符串分割 (Word Break)

  • 难度: Medium
  • 备注:出自 leetcode
  • 题目描述
    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", "code"].

        Return true because"leetcode"can be segmented as"leet code".
        bool wordBreak (string s, unordered_set &dict)
        来源:牛客 - leetcode

题目描述
给定一个字符串和一个词典 dict,确定 s 是否可以根据词典中的词分成一个或多个单词。
比如,给定
s = "leetcode"
dict = ["leet", "code"]
返回 true,因为 "leetcode" 可以被分成 "leet code"

#include <bits/stdc++.h>
using namespace std;
bool wordBreak (string s, unordered_set<string> &dict) {//unordered_set &dict 哈希表 //边界判断 if(s.empty())return false;int len=s.size();//vector构造函数实现初始化 vector<bool> canBreak(len+1,false);//初始化 F(0)=truecanBreak[0]=true;for(int i=1;i<=len;i++){//转移方程 F(i)  j<i && F(j) && [j+1,i]for(int j=i-1;j>=0;j--){if(canBreak[j]&&dict.find(s.substr(j,i-j))!=dict.end()){canBreak[i]=true;break;}} }//返回结果 F(s.size()) return canBreak[len];
}
int main() {string s="leetcode";unordered_set<string> dict={"leet","code"};cout<<wordBreak(s,dict);return 0;
}

http://www.lryc.cn/news/2402586.html

相关文章:

  • 软信天成:数据驱动型背后的人工智能,基于机器学习的数据管理
  • MySQL提升
  • hbase资源和数据权限控制
  • VMWare下设置共享文件,/mnt/hgfs下却不显示共享文件的解决方法
  • go语言的锁
  • C++11完美转发
  • VUE解决页面请求接口大规模并发的问题(请求队列)
  • IDEA安装迁移IDEA配置数据位置
  • Blazor-表单提交的艺术:如何优雅地实现 (下)
  • 五子棋网络对战游戏的设计与实现设计与实现【源码+文档】
  • Vue基础(14)_列表过滤、列表排序
  • Spring Boot项目中JSON解析库的深度解析与应用实践
  • 我用Amazon Q写了一个Docker客户端,并上架了懒猫微服商店
  • Django CMS 的 Demo
  • 在 UE5 蓝图中配置Actor类型的Asset以作为位置和旋转设置目标
  • Android 之 kotlin 语言学习笔记四(Android KTX)
  • 适用于vue3的大屏数据展示组件库DataV(踩坑版)
  • mysql实现分页查询
  • Flink checkpoint
  • 【java】在springboot中实现证书双向验证
  • CppCon 2015 学习:Functional Design Explained
  • 基于3D对象体积与直径特征的筛选
  • GIT - 如何从某个分支的 commit创建一个新的分支?
  • Claude vs ChatGPT vs Gemini:功能对比、使用体验、适合人群
  • 线程基础编程
  • DJango项目
  • 深入了解JavaScript当中如何确定值的类型
  • excel数据对比找不同:6种方法核对两列数据差异
  • 基于智能代理人工智能(Agentic AI)对冲基金模拟系统:模范巴菲特、凯西·伍德的投资策略
  • MySQL数据库基础(二)———数据表管理