LintCode-Word Segmentation
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.
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的更多相关文章
- 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 ...
- 长短时间记忆的中文分词 (LSTM for Chinese Word Segmentation)
翻译学长的一片论文:Long Short-Term Memory Neural Networks for Chinese Word Segmentation 传统的neural Model for C ...
- [LintCode] Word Break
Given a string s and a dictionary of words dict, determine if s can be break into a space-separated ...
- [Lintcode]Word Squares(DFS|字符串)
题意 略 分析 0.如果直接暴力1000^5会TLE,因此考虑剪枝 1.如果当前需要插入第i个单词,其剪枝如下 1.1 其前缀(0~i-1)已经知道,必定在前缀对应的集合中找 – 第一个词填了ball ...
- zpar使用方法之Chinese Word Segmentation
第一步在这里: http://people.sutd.edu.sg/~yue_zhang/doc/doc/qs.html 你可以找到这句话, 所以在命令行中分别敲入 make zpar make zp ...
- 论文阅读及复现 | Effective Neural Solution for Multi-Criteria Word Segmentation
主要思想 这篇文章主要是利用多个标准进行中文分词,和之前复旦的那篇文章比,它的方法更简洁,不需要复杂的结构,但比之前的方法更有效. 方法 堆叠的LSTM,最上层是CRF. 最底层是字符集的Bi-LST ...
- Java——word分词·自定义词库
word: https://github.com/ysc/word word-1.3.1.jar 需要JDK8word-1.2.jar c语言给解析成了“语言”,自定义词库必须为UTF-8 程序一旦运 ...
- 【中文分词】最大熵马尔可夫模型MEMM
Xue & Shen '2003 [2]用两种序列标注模型--MEMM (Maximum Entropy Markov Model)与CRF (Conditional Random Field ...
- 【中文分词】二阶隐马尔可夫模型2-HMM
在前一篇中介绍了用HMM做中文分词,对于未登录词(out-of-vocabulary, OOV)有良好的识别效果,但是缺点也十分明显--对于词典中的(in-vocabulary, IV)词却未能很好地 ...
- 【中文分词】隐马尔可夫模型HMM
Nianwen Xue在<Chinese Word Segmentation as Character Tagging>中将中文分词视作为序列标注问题(sequence labeling ...
随机推荐
- 纪念大一的日子,一个简单的C++
//Author:xtyang //记得大一学C语言,永远都不明白如何调用一个函数,真是好可爱呀. #include<iostream> using namespace std; //定义 ...
- 简单的MySQL数据库主从同步配置
Master配置: 代码如下 复制代码 log-bin = mysql-binserver-id = 1binlog-do-db = powerdns #要同步的数据库 Master新增slave账号 ...
- sql语句中charindex的用法 可用于截取字符串
使用CHARINDEX和PATINDEX函数来搜索文字列和字符串.这两个函数是如何运转的,解释他们的区别.同时提供一些例子,通过这些例子,你可以可以考虑使用这两个函数来解决很多不同的字符搜索的问题. ...
- Cocos2d-JS事件处理机制
在很多图形用户技术中,事件处理机制一般都有三个重要的角色:事件.事件源和事件处理者.事件源是事件发生的场所,通常就是各个视图或控件,事件处理者是接收事件并对其进行处理的一段程序.事件处理机制中三个角色 ...
- iOS-NSOperation多线程
NSOperation 一.简介 1.使用步骤 配合使用NSOperation和NSOperationQueue也能实现多线程编程 先将操作封装到一个NSOperation对象中 然后将NSOpera ...
- 20141017--循环语句whlie,do
int n = 0; ; ) {//n必须小于100,如果等于100则会再进来进行一次运算,变为101. //因为下面的语句中用到了continue,状态改变n++不能用到最后 n++; ) { m ...
- php_1
简介:(“PHP: Hypertext Preprocessor”,超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML中,尤其适合 web 开发,语言的风格有 ...
- jQuery 实现网页图片动态游走,碰到边框反弹
学学jQuery,实现个小功能练练手 需要用到定时器 html代码如下 <html> <head> <title></title> <script ...
- NDK jni 加载静态库
加载静态库到android,静态库的提供方式有2种, a. 通过源文件来编译静态库 b. 加载已经编译好的静态库 首先我们来看,通过源文件来编译静态库,工程目录如下 第一步:我们来看我们的jni目录, ...
- 《DNS服务缓存的建立》RHEL6
安装dns软件包: 启动dns服务器 配置本地服务器dns域名解析: 查看dns的服务的端口是否打开: 解析百度测试: 安装dns缓存包: 再次访问解析百度