http://www.lintcode.com/zh-cn/problem/word-break/

单词切分

给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。

您在真实的面试中是否遇到过这个题?

 
样例

给出

s = "lintcode"

dict = ["lint","code"]

返回 true 因为"lintcode"可以被空格切分成"lint code"

标签:动态规划

解题:

还是动态规划问题, 动态规划的本质:根据已知结论推理未知结论。

思路: s = lintcode 可以分为 l和intcode,若是l是可分割单词,并且intcode单词在dict中,则表示s也是可分割单词。
若不符合,s = lintcode 可以分为 li和ntcode,若是li是可分割单词,并且ntcode单词在dict中,则表示s也是可分割单词。
...
同理得出BWord[ n ]表示字符串S[0,n]是否是可分割单词,是则为true,否为false。
BWord[ n ]  = BWord[ i ] &&( S[ i+1 ,n ]在dict中 )
 
java代码1:(有些超时)
    public class Solution {
/**
* @param s: A string s
* @param dict: A dictionary of words dict
*/
public boolean wordBreak(String s, Set<String> dict) {
// write your code here
boolean BWord[] = new boolean[s.length()+1];
BWord[0] = true;
for(int i=1;i<s.length()+1;i++){
for(int j=i-1;j>=0;j--){
if(BWord[j]&&dict.contains(s.substring(j,i))){
BWord[i] = true;
break;
}
}
}
return BWord[s.length()]; }
}

java代码2:

public class Solution {
/**
* @param s: A string s
* @param dict: A dictionary of words dict
*/
public boolean wordBreak(String s, Set<String> dict) {
// write your code here
if((s==null ||s.length() ==0) && (dict == null || dict.size()==0))
return true;
return wordBreak(s,dict,0);
}
public boolean wordBreak(String s,Set<String> dict,int start){
boolean dp[] = new boolean[s.length() + 1];
dp[0] = true;//初始值
for(int i = 0;i<s.length();i++){
if(!dp[i])
continue;
for(String t:dict){
int len = t.length();
int end = i+ len;
if(end > s.length())
continue;
if(s.substring(i,end).equals(t)){
dp[end] = true;
}
}
}
return dp[s.length()];
} }

C++代码:

class Solution {
public:
/**
* @param s: A string s
* @param dict: A dictionary of words dict
*/
bool wordBreak(string s, unordered_set<string> &dict) {
// write your code here
//还是动态规划问题,先理解清楚题意;
//将问题转化为小规模的问题,先看子问题是不是,然后在扩展;
//程序思路很清楚; int slen=s.size();
int dlen=dict.size(); if(slen==&&dlen==){
return true;
}
if(slen==||dlen==){
return false;
}
bool* dp=new bool[slen+]; dp[]=true;
for(int i=;i<slen;i++){
if(!dp[i]){
continue;//若dp[i]为false,则执行下一个i;
}
unordered_set<string>::iterator it;
for(it=dict.begin();it!=dict.end();++it){
string value=*it;
int lv=value.size();//求出字典中字符串的长度;
if(i+lv>slen){
continue;//如果长度大于s,则执行字典中的下一个字符串;
}
string last=s.substr(i,lv); //以i为初始地址,偏移量为lv
if(last==value){
dp[i+lv]=true;
}
} }
return dp[slen];
}
};

实验证明,C++ 运行时间更短,效率更高。

注:str.substr(startpos, length);

  其中 startpos 是起始字符的序号,length 是[从 startpos 开始]取的字符串长度(包括startpos )

Lintcode--009(单词切分)的更多相关文章

  1. lintcode:单词切分

    单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...

  2. word break II(单词切分)

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  3. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  4. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  5. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  6. lintcode 题目记录3

    Expression Expand  Word Break II Partition Equal Subset Sum  Expression Expand  字符串展开问题,按照[]前的数字展开字符 ...

  7. Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  8. 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  9. leetcode-884-两句话中的不常见单词

    题目描述: 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回 ...

随机推荐

  1. 公网IP和私有IP

    IP地址是为了区分网络中不同主机所分配的一个地址,通过IP地址可以访问到每一台主机. IP地址分为公有地址和私有地址,公有地址由Internet NIC负责(比如中国互联网信息中心http://ip. ...

  2. JQ无法修改input的type属性的替代解决方法

    需要实现的效果:一个输入框,当输入框未获得焦点的时候,显示为 “请输入密码”:当输入内容并失去焦点的时候,输入内容显示为”*****”,如果没有输入仍然显示“请输入密码”: 方法一:使用text,隐藏 ...

  3. Android显示表情对话框

    final EditText editface=(EditText) this.findViewById(R.id.EditText); List<Map<String,Object> ...

  4. [LeetCode] 55. Jump Game 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. Java虚拟机内存优化实践

    前面一篇文章介绍了Java虚拟机的体系结构和内存模型,既然提到内存,就不得不说到内存泄露.众所周知,Java是从C++的基础上发展而来的,而C++程序的很大的一个问题就是内存泄露难以解决,尽管Java ...

  6. 对象拷贝类PropertyUtils,BeanUtils,BeanCopier的技术沉淀

    功能简介 对象拷贝的应用现状简介: 业务系统中经常需要两个对象进行属性的拷贝,不能否认逐个的对象拷贝是最快速最安全的做法,但是当数据对象的属性字段数量超过程序员的容忍的程度,代码因此变得臃肿不堪,使用 ...

  7. 创建多模块maven项目

    有两种方式: 1,new -->maven project-->maven-archetype-quickstart 建完多个独立的project后,手动修改pom文件的packing类型 ...

  8. 简单的新闻客户端APP开发(DCloud+thinkphp+scrapy)

    前端时间花了1个月左右,搞了个新闻APP,功能很简单,就是把页面版的新闻条目定时爬到后台数据库,然后用app显示出来. 1.客户端 使用了DCloud框架,js基本是个新手,从没写过像样的代码,htm ...

  9. ORACLE STUDY NOTES 01

    [JSU]LJDragon's Oracle course notes In the first semester, junior year DML数据操纵语言 DML指:update,delete, ...

  10. Django 内置分页--Paginator类

    官方文档 http://python.usyiyi.cn/django/topics/pagination.html 前端方法 http://www.tuicool.com/articles/RniU ...