LintCode: Word Break

简介:

给定字符串序列和一个字典,问给定的字符串能否用字典中的单词拼出来?

图:

  节点:字符串的前缀长度

  边:前缀x如果加一个字典里边的单词能形成新前缀x',则有一条边

    例如:字符串IAMxxxxx,字典里有I和AM

    则有(0,1)一条边,(1,3)一条边

解:从(0,?)到(?,n)找一条路径

这是一个判断是否有解的问题

BFS/DFS

  从当前前缀开始加一个单词

其他

  能否dp?bool dp[x]表示能否连到x位置

  dp[x] = dp[y] && (y..x是一个单词)

思考:

  如何求一组解?全部解?

C++ 

DFS

(lintcode: s太长的话,会溢出。64bit mac OS上字符串长度超过8881就溢出。)

(leetcode: Accepted)

复制代码
 1 class Solution {
 2 public:
 3     bool help(string &s, int now, unordered_set<string> &d, vector<bool> &have) {
 4         if (now >= s.length()) {
 5             return true;
 6         }
 7         if (have[now]) {
 8             return false;
 9         }
10         have[now] = true;
11         for (int i = now; i < s.length(); ++i) {
12             if ((d.find(s.substr(now, i - now + 1)) != d.end()) && (help(s, i + 1, d, have))) {
13                 return true;
14             }
15         }
16         return false;
17     }
18     /**
19      * @param s: A string s
20      * @param dict: A dictionary of words dict
21      */
22     bool wordBreak(string s, unordered_set<string> &dict) {
23         // write your code here
24         vector<bool> have(s.length(), false);
25         return help(s, 0, dict, have);
26     }
27 };
复制代码

C++

BFS

(lintcode:字符串太长,例如超过10000个字符,会超时)

(leetcode: Accepted)

复制代码
 1 class Solution {
 2 public:
 3     bool help(string &s, unordered_set<string> &d) {
 4         if (0 == s.length()) {
 5             return true;
 6         }
 7         vector<bool> have(s.length(), false);
 8         have[0] = true;
 9         queue<int> q;
10         for (q.push(0); !q.empty(); q.pop()) {
11             int now = q.front();
12             for (int i = now; i < s.length(); ++i) {
13                 if ((d.find(s.substr(now, i - now + 1)) != d.end())) {
14                     if (i + 1 >= s.length()) {
15                         return true;
16                     }
17                     if (!have[i + 1]) {
18                         have[i + 1] = true;
19                         q.push(i + 1);
20                     }
21                 }
22             }
23         }
24         
25         return false;
26     }
27     /**
28      * @param s: A string s
29      * @param dict: A dictionary of words dict
30      */
31     bool wordBreak(string s, unordered_set<string> &dict) {
32         // write your code here
33         return help(s, dict);
34     }
35 };
复制代码

C++

DP

(lintcode:字符串太长,例如超过10000个字符,会超时)

(leetcode: Accepted)

复制代码
 1 class Solution {
 2 public:
 3     bool help(string &s, unordered_set<string> &d) {
 4         if (0 == s.length()) {
 5             return true;
 6         }
 7         vector<bool> dp(s.length(), false);
 8         dp[0] = true;
 9         for (int now = 0; now < s.length(); ++now) {
10             if (!dp[now]) {
11                 continue;
12             }
13             for (int i = now; i < s.length(); ++i) {
14                 if ((d.find(s.substr(now, i - now + 1)) != d.end())) {
15                     if (i + 1 >= s.length()) {
16                         return true;
17                     }
18                     dp[i + 1] = true;
19                 }
20             }
21         }
22         
23         return false;
24     }
25     /**
26      * @param s: A string s
27      * @param dict: A dictionary of words dict
28      */
29     bool wordBreak(string s, unordered_set<string> &dict) {
30         // write your code here
31         return help(s, dict);
32     }
33 };
复制代码

 Python

DP

复制代码
 1 class Solution:
 2     # @param s: A string s
 3     # @param dict: A dictionary of words dict
 4     def wordBreak(self, s, dict):
 5         if len(dict) == 0:
 6             return len(s) == 0
 7             
 8         n = len(s)
 9         f = [False] * (n + 1)
10         f[0] = True
11         
12         maxLength = max([len(w) for w in dict])
13         for i in xrange(1, n + 1):
14             for j in range(1, min(i, maxLength) + 1):
15                 if not f[i - j]:
16                     continue
17                 if s[i - j:i] in dict:
18                     f[i] = True
19                     break
20         
21         return f[n]
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5012413.html,如需转载请自行联系原作者

相关文章
|
3天前
|
云安全 人工智能 自然语言处理
|
7天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
755 17
|
11天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
783 59
Meta SAM3开源:让图像分割,听懂你的话
|
1天前
|
人工智能 安全 小程序
阿里云无影云电脑是什么?最新收费价格个人版、企业版和商业版无影云电脑收费价格
阿里云无影云电脑是运行在云端的虚拟电脑,分企业版和个人版。企业版适用于办公、设计等场景,4核8G配置低至199元/年;个人版适合游戏、娱乐,黄金款14元/月起。支持多端接入,灵活按需使用。
231 164
|
8天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
330 116
|
2天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
300 3
|
6天前
|
弹性计算 搜索推荐 应用服务中间件
阿里云服务器租用价格:一年、1小时及一个月收费标准及优惠活动参考
阿里云服务器优惠汇总:轻量应用服务器200M带宽38元/年起,ECS云服务器2核2G 99元/年、2核4G 199元/年,4核16G 89元/月,8核32G 160元/月,香港轻量服务器25元/月起,支持按小时计费,新老用户同享,续费同价,限时秒杀低至1折。
402 166

热门文章

最新文章