Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

Example

Given

s = "lintcode",

dict = ["lint", "code"].

Return true because "lintcode" can be segmented as "lint code".

Analysis:

It is a DP problem. However, we need to use charAt() instead of substring() to optimize speed. Also, we can first check whether each char in s has appeared in dict, if not, then directly return false. (This is used to pass the last test case in LintCode).

Solution:

 public class Solution {
/**
* @param s: A string s
* @param dict: A dictionary of words dict
*/
public boolean wordSegmentation(String s, Set<String> dict) {
if (s.length()==0) return true; char[] chars = new char[256];
for (String word : dict)
for (int i=0;i<word.length();i++)
chars[word.charAt(i)]++; for (int i = 0;i<s.length();i++)
if (chars[s.charAt(i)]==0) return false; boolean[] d = new boolean[s.length()+1];
Arrays.fill(d,false);
d[0] = true;
for (int i=1;i<=s.length();i++){
StringBuilder builder = new StringBuilder();
for (int j=i-1;j>=0;j--){
builder.insert(0,s.charAt(j));
String cur = builder.toString();
if (d[j] && dict.contains(cur)){
d[i]=true;
break;
}
}
} return d[s.length()]; }
}

LintCode-Word Segmentation的更多相关文章

  1. Solution for automatic update of Chinese word segmentation full-text index in NEO4J

    Solution for automatic update of Chinese word segmentation full-text index in NEO4J 1. Sample data 2 ...

  2. 长短时间记忆的中文分词 (LSTM for Chinese Word Segmentation)

    翻译学长的一片论文:Long Short-Term Memory Neural Networks for Chinese Word Segmentation 传统的neural Model for C ...

  3. [LintCode] Word Break

    Given a string s and a dictionary of words dict, determine if s can be break into a space-separated ...

  4. [Lintcode]Word Squares(DFS|字符串)

    题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...

  5. zpar使用方法之Chinese Word Segmentation

    第一步在这里: http://people.sutd.edu.sg/~yue_zhang/doc/doc/qs.html 你可以找到这句话, 所以在命令行中分别敲入 make zpar make zp ...

  6. 论文阅读及复现 | Effective Neural Solution for Multi-Criteria Word Segmentation

    主要思想 这篇文章主要是利用多个标准进行中文分词,和之前复旦的那篇文章比,它的方法更简洁,不需要复杂的结构,但比之前的方法更有效. 方法 堆叠的LSTM,最上层是CRF. 最底层是字符集的Bi-LST ...

  7. Java——word分词·自定义词库

    word: https://github.com/ysc/word word-1.3.1.jar 需要JDK8word-1.2.jar c语言给解析成了“语言”,自定义词库必须为UTF-8 程序一旦运 ...

  8. 【中文分词】最大熵马尔可夫模型MEMM

    Xue & Shen '2003 [2]用两种序列标注模型--MEMM (Maximum Entropy Markov Model)与CRF (Conditional Random Field ...

  9. 【中文分词】二阶隐马尔可夫模型2-HMM

    在前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显--对于词典中的(in-vocabulary, IV)词却未能很好地 ...

  10. 【中文分词】隐马尔可夫模型HMM

    Nianwen Xue在<Chinese Word Segmentation as Character Tagging>中将中文分词视作为序列标注问题(sequence labeling ...

随机推荐

  1. WPF 之 数据绑定

    数据绑定需要: 后台必须要有与控件对应的类和相关的属性 在后台new 一个类的对象 在后台把控件的数据上下文设置为该对象. 在XAML中把要绑定的控件中Text =      "{Bindi ...

  2. AngularJS简单用法

    一.数据绑定 AngularJS的双向数据绑定,意味着你可以在Mode(JS)中改变数据,而这些变动立刻就会自动出现在View上,反之亦然.即:一方面可以做到model变化驱动了DOM中元素变化,另一 ...

  3. list转换成DataTable

    list转换成DataTable类如下: public static DataTable ToDataTable<T>(this IList<T> datas) { DataT ...

  4. FusionCharts 相关知识

    FusionCharts1.平均线: <trendLines><line startValue='{0}' toolText='平均线' color='#FF0000' displa ...

  5. C++为了兼容,所以并不是纯面向对象编程语言

    理想如果不向现实做点妥协,理想就会归于尘土.面向对象怎能把一切传统抛开!

  6. JAVA多线程通信

    JAVA多线程通信 package com.frank.thread; /** * author:pengyan * date:Jun 16, 2011 * file:ProducerAndCusto ...

  7. vector C++ 详细用法

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  8. C++ 嵌入汇编 获取CPU信息

    #include "windows.h" #include "iostream" #include "string" using names ...

  9. 关于FileOutputStream的问题

    FileoutputStream在文件不存在的情况下会新建文件,但是有几个注意点: 1.有目录名(文件夹名)和文件名重复的话,会报错: 2.目录名不存在的情况下,也会报错

  10. 下载服务器dll文件并动态加载

    1.新加一个类库 namespace ClassLibrary1 { public class Class1 { public int Add(int a, int b) { return a + b ...