单词切分

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

样例

s = "lintcode"

dict = ["lint","code"]

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

解题

DFS

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
// write your code here
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Main m = new Main();
while(in.hasNext()){
String s = in.nextLine();
String[] str = in.nextLine().split(" ");
Set<String> dict = new TreeSet<String>();
for(int i = 0;i<str.length;i++)
dict.add(str[i]);
int start = 0;
boolean flag = m.wordBreak(s, dict, start);
System.out.println(flag);
}
}
public boolean wordBreak(String s,Set<String> dict,int start){

      if((s==null ||s.length() ==0) && (dict == null || dict.size()==0))
        return true;

        Iterator it = dict.iterator();
if(start == s.length())
return true;
while(it.hasNext()){
String t = (String)it.next();
int end = start + t.length();
if(end > s.length())
continue;
if(s.substring(start,end).equals( t )){
if(wordBreak(s,dict,end)){
return true;
}
}
}
return false;
}
}

95%数据运行超时

动态规划求解

定义数组dp dp[i] =true表示 字符串 s 子串0 - (i-1)在字典中存在

当dp[s.length()] == true 时候表示可以由字典内的单词组成s

至于为什么?

自己根据输出发现:当有一个字符没有出现,后面的将都会为false,一次没有找到,破坏了后面的继续查找字符串的长度,后面就再也找不到,最后一个位置为false

自己测试

    public static void main(String[] args){
Solution s = new Solution();
Set<String> dict = new HashSet<String>();
String str = "123";
dict.add("1");
dict.add("2"); dict.add("64");
System.out.println(s.wordBreak(str, dict));
dict.add("3");
System.out.println(s.wordBreak(str, dict));
}

输出

[true, false, false, false]
[true, true, false, false]
[true, true, true, false]
[true, true, true, false]
false
[true, false, false, false]
[true, true, false, false]
[true, true, true, false]
[true, true, true, true]
true
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()];
} }

lintcode:单词切分的更多相关文章

  1. Lintcode--009(单词切分)

    http://www.lintcode.com/zh-cn/problem/word-break/ 单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. ...

  2. lintcode :单词搜索

    题目 单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. 样 ...

  3. word break II(单词切分)

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

  4. lintcode 单词接龙II

    题意 给出两个单词(start和end)和一个字典,找出所有从start到end的最短转换序列 比如: 1.每次只能改变一个字母. 2.变换过程中的中间单词必须在字典中出现. 注意事项 所有单词具有相 ...

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

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

  6. leetcode & lintcode for bug-free

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

  7. leetcode & lintcode 题解

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

  8. lintcode 题目记录3

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

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

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

随机推荐

  1. ios coreData使用

    ios中的coredata的使用(转) 分类: ios2013-07-15 18:12 27288人阅读 评论(1) 收藏 举报 Core Data数据持久化是对SQLite的一个升级,它是ios集成 ...

  2. UITableView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  3. Integer对象

    数字格式的字符串转成基本数据类型的方法: 1:将该字符串封装成了Integer对象,并调用对象的方法intValue(); 2:使用Integer.parseInt(numstring):不用建立对象 ...

  4. C++中不可重载的5个运算符

    大多数运算符都是可以重载的,但是有5个运算符C++语言规定是不可以重载的. 1. .(点运算符),通常用于去对象的成员,但是->(箭头运算符),是可以重载的 2.::(域运算符),即类名+域运算 ...

  5. C++中的const关键字的用法

    1.const用于修饰普通变量,表示常量,不建议修改,某种程度上不允许修改(其实也是可以修改的) 指针常量 :指针(指向的变量的值)自身是一个常量,说明不能改变自身的指向  int* const p= ...

  6. camera render texture 游戏里的监控视角

    Camera里: 新建render texture并拖入到target texture里 新建材质球 拖入render texture      camera里的视角会在材质球上出现  新建一个pla ...

  7. 修改Input中Placeholder默认提示颜色(兼容)

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #f00; } input:-moz-pl ...

  8. [转]Similarities between Hibernate and JDBC objects

  9. AutoMap1.0发布

    去年就已经透漏了AutoMap的雏形,后面一段时间一直没有充裕的时间来完成,只能零星的进行完善.现在产品还有很多不足,基本架构已经完成,就先释放一个1.0版,希望大家多多支持. 一.服务端 服务端在I ...

  10. hibernate---CRUD

    delete @Test public void testDelete() { Teacher t = new Teacher(); t.setName("t1"); t.setT ...