Lintcode--009(单词切分)
http://www.lintcode.com/zh-cn/problem/word-break/
单词切分
给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词。
给出
s = "lintcode"
dict = ["lint","code"]
返回 true 因为"lintcode"可以被空格切分成"lint code"
解题:
还是动态规划问题, 动态规划的本质:根据已知结论推理未知结论。
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(单词切分)的更多相关文章
- lintcode:单词切分
单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...
- word break II(单词切分)
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- lintcode 题目记录3
Expression Expand Word Break II Partition Equal Subset Sum Expression Expand 字符串展开问题,按照[]前的数字展开字符 ...
- Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- leetcode-884-两句话中的不常见单词
题目描述: 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回 ...
随机推荐
- 模糊系统架构和简单实现--AForge.NET框架的使用(四)
原文:模糊系统架构和简单实现--AForge.NET框架的使用(四) 先说一下,为什么题目是简单实现,因为我实在没有弄出好的例子. 我原来用AForge.net做的项目中的模糊系统融入了神经网络和向量 ...
- easyUI treeGrid 的小例子
今天由于业务的需要,于是采用了easyui的的treeGrid控件. <table title="Folder Browser" class="easyui-tre ...
- perl dbi 测试 mysql wait_timeout
The number of seconds the server waits for activity on a noninteractive connection before closing it ...
- BZOJ 1018 [SHOI2008]堵塞的交通traffic
1018: [SHOI2008]堵塞的交通traffic Time Limit: 3 Sec Memory Limit: 162 MBSubmit: 2247 Solved: 706[Submit ...
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
转自:http://blog.csdn.net/little_stars/article/details/8266262 前文讲述了 POI 读取的基本操作,但后期 经过试验,当写入数据量超过5万条以 ...
- iframe切换内容页仍然能自适应大小代码(含js)
function setIframeHeight(iframe) { if (iframe) { var iframeWin = iframe.contentWindow || iframe.cont ...
- 关于使用百度ueditor时的一些问题
本来这些问题直接在百度贴吧里回答不就完事了,可是好死不死的,百度贴吧里老出现 未知错误,错误号:230274 看来还是算了,自己做一个随笔记录一下好了 关于我们获取里面的内容时,老是会有一个<p ...
- [nagios监控] NRPE: Unable to read output 的原因及排除
nrpe被监控端运行定义命令正常,监控端运行 #/usr/local/nagios/libexec/check_nrpe -H 117.121.9.200 -c check_oracle_tables ...
- [React] React Router: hashHistory vs browserHistory
In this lesson we'll look at hashHistory which uses a hash hack to track our route changes vs browse ...
- [Angular 2] implements OnInit, OnDestory for fetching data from server
Link: https://angular.io/docs/js/latest/api/core/OnInit-interface.html, https://www.youtube.com/watc ...