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. TCP 的那些事儿(转载)

    无论是PC客户端开发还是移动开发,或是Web开发, 网络编程都是很重要的一块, 深入理解TCP/IP和HTTP协议是一个优秀程序员的必备技能.看到酷壳老大正好写了篇相关文章, 正好拿来学习, 转自 h ...

  2. Basic Printing Architecture

    https://blogs.technet.microsoft.com/askperf/2007/06/19/basic-printing-architecture/ Printer sharing, ...

  3. Smarty 插件开发

    插件包含了: functions modifiers block functions compiler functions prefilters postfilters outputfilters r ...

  4. 【转】adobe acrobat pro修改pdf文字

    原文网址:http://zhidao.baidu.com/link?url=7MTeEu5IM49lNIISNQMcZLyLAwMPsRQWF5WAwQPfvkPsbbZLHSQE43MWaIxxVm ...

  5. LINQPad 调试

    var ss=from o in Orders from od in OrderDetails.Where(od=>od.OrderId == od.OrderId) from c in Cou ...

  6. AOJ 0033 深度优先搜索

    题意:按给定顺序从A口放标号位1-10的10个球,利用挡板可以使球落到B或C,问能否使B和C里的球标号从下往上递增. 分析:对于第 i 个球,若a[i]大于B口上方的球,则可放入B口:若a[i]大于C ...

  7. ie11 selenium 报错org.openqa.selenium.NoSuchWindowException: Unable to get browser 处理方法

    selenium + ie11运行报错 org.openqa.selenium.NoSuchWindowException: Unable to get browser (WARNING: The s ...

  8. iOS单元测试(作用及入门提升)

    由于只是一些简单实用的东西,学学还是挺不错的.其实单元测试用的好,开发起来也会快很多.单元测试对于我目前来说,就是为了方便测试一些功能是否正常运行,还有调试接口是否能正常使用.有时候你可能是为了测试某 ...

  9. wpf下拉框不能多选的原因

    <dxe:ComboBoxEdit Margin="0"  Height="25" Width="65" VerticalAlignm ...

  10. 開始学习swift开发

    近期要開始学习swift开发了,接下来的日子,会记录学习swift的历程.