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 ...
随机推荐
- CSS - 实现文字显示过长时用省略
一.添加-文字显示超出范围时隐藏属性 overflow:hidden; 二.添加-超出文字省略号属性 text-overflow:ellipsis; 三.添加-文字不换行属性 white-space: ...
- CS加密算法
概述: 加密数据可以使用对称加密或非对称加密算法,使用对称加密比非对称密钥快得多,但对称密钥需要解决安全交换密钥的问题.在 .NET Framework中,可以使用System.Security.Cr ...
- Part 1 What is SSMS?
note that,SSMS is a client tool and not the server by itself,it is a developer machines connects to ...
- MVC4 图片上传
新增 new { enctype = "multipart/form-data" } 这个必须要有 @using (Html.BeginForm(Html.BeginForm(&q ...
- 页面多语系自动切换-.resx
实现这个需要应用到微软提供的本地资源文件Resources,实际上就是一种key value的形式. 语言自动切换原理有两种 读取浏览器的语言首选项 根据IP网段进行识别.比如有的人通过VPN连接到 ...
- cocos2d-x一些核心概念截杀
Cocos2d-x中有很多概念,这些概念很多来源于动画.动漫和电影等行业,例如:导演.场景和层等概念,当然也有些有传统的游戏的概念.Cocos2d-x中核心概念:导演, 场景,层,节点,精灵,菜单动作 ...
- 移动端边框1px的实现
查看京东的移动端1px实现原理,用的是:after和css3的scale(0.5)缩放. border-right fr:after{ height:100%; content:' '; width: ...
- css3 画x图形
.m-act-del{ right:; top:; margin-top: -6px; position: absolute; display: inline-block; width: 20px; ...
- 使用DbVisualizer 8 连接Oracle数据库
1. 网上下载一个驱动包ojdbc14.jar,放到oracle目录下:...\DbVisualizer-8.0.1\jdbc\oracle\ojdbc14.jar 2. 打开 DbVisualize ...
- Ant打jar包指定MainClass
一般用ant打jar的时候不用指定程序的入口!这个jar一般是给其他app引用的. 但是如果该jar就是程序的启动jar.例如: java -jar abc.jar 这个时候需要指定jar的入口类! ...