单词切分

给出一个字符串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. [转]KDE/QT与GNOME/GTK比较

    [转]KDE/QT与GNOME/GTK比较 http://www.cnblogs.com/itech/archive/2009/08/18/1548964.html 虽然在商业方面存在竞争,GNOME ...

  2. [转]安装openoffice,并且配置为windows服务

    [转]安装openoffice,并且配置为windows服务 http://blog.csdn.net/zzzz3621/article/details/18400277 下载windows reso ...

  3. ubuntu下xampp的安装

    转载:http://blog.csdn.net/zhoushengchao/article/details/6006584 首先,请从www.xampp.org下载最新版XAMPP.然后,将安装文件服 ...

  4. Python实现ID3(信息增益)

    Python实现ID3(信息增益) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>start: ...

  5. LintCode-BackPack II

    Given n items with size A[i] and value V[i], and a backpack with size m. What's the maximum value ca ...

  6. 结队开发项目—NABC模型

    特点:可以避免食堂的用餐高峰,从而使使用者节约时间 need:很多学生中午下课在食堂吃饭会遭遇用餐高峰,使用这款软件,可以提前订饭,按时送达,从而避免食堂的用餐高峰期. approach:学生可以提前 ...

  7. Java 查询URL对应IP地址

    /** * @ClassName TestSocket1 * @Version 1.0 * @Date 2014-9-26 上午10:19:36 */ public class TestSocket1 ...

  8. SPOJ-SQRBR Square Brackets

    原题传送:http://www.spoj.pl/problems/SQRBR 动态规划. 设f[i][j]表示前i个位置在合法情况下缺少j个右括号的方案数. 转移方程为: f[i][j] = f[i- ...

  9. hdu 1392 Surround the Trees

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长. 解法:凸包ConvexH ...

  10. Java多线程——<八>多线程其他概念

    一.概述 到第八节,就把多线程基本的概念都说完了.把前面的所有文章加连接在此: Java多线程——<一>概述.定义任务 Java多线程——<二>将任务交给线程,线程声明及启动 ...