trie + 长度优先匹配,生成串
import com.google.common.collect.Maps;
import java.util.Map;
/**
* tree 节点
* Created by shuly on 16-7-18.
*/
public class Node {
boolean isRoot;
boolean isEnd;
int cnt; Map<Character,Node> childrens; public Node(){
if (childrens == null) childrens = Maps.newHashMap();
this.cnt = 0;
}
public Node(boolean _isRoot,boolean _isEnd){
if (childrens == null) childrens = Maps.newHashMap();
setEnd(_isEnd);
setRoot(_isRoot);
this.cnt = 0;
}
public boolean isRoot() {
return isRoot;
} public void setRoot(boolean root) {
isRoot = root;
} public boolean isEnd() {
return isEnd;
} public void setEnd(boolean end) {
isEnd = end;
} public int getCnt() {
return cnt;
} public void setCnt(int cnt) {
this.cnt = cnt;
} public Map<Character, Node> getChildrens() {
return childrens;
} public void setChildrens(Map<Character, Node> childrens) {
this.childrens = childrens;
}
}
import com.google.common.collect.Lists;
import java.util.Stack;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List; /**
* 树的主类
* Created by shuly on 16-7-18.
*/
public class PyTreeIT {
Node ROOT; public PyTreeIT() {
ROOT = new Node(true,false);
numberLimit = new Integer(20);
URL url = PyTreeIT.class.getClassLoader().getResource("pyDic");
String dicFilePath = url.getPath();
File dicFile = new File(dicFilePath);
BufferedReader br = null ;
try {
String line;
br = new BufferedReader(new InputStreamReader(new FileInputStream(dicFile), "UTF-8"));
while((line = br.readLine()) != null)
{
String word = line.trim();
this.insert(word);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if(br != null){
try {
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void insert(String word){
Node root= ROOT;
char [] words = word.toCharArray();
for(int i = 0 ; i< words.length ; ++i){
Character key = words[i];
if(!root.getChildrens().containsKey(key)){
root.getChildrens().put(key,new Node(false,false));
}
root = root.getChildrens().get(key);
root.setCnt(root.getCnt()+1);
}
root.setEnd(true);
}
public int had(String word){
Node root = ROOT;
char[] words = word.toCharArray();
for(int i = 0 ; i< words.length ; ++i){
Character key = words[i];
if(root.getChildrens().containsKey(key)){
root = root.getChildrens().get(key);
}else
return -1;
}
return root.isEnd()? root.getCnt(): 0;
}
static class InStack{
public String key,left;
public InStack(String _key,String _left){
this.key = _key;
this.left = _left;
}
@Override
public String toString(){
return key +"--" + left;
}
}
protected Integer numberLimit;
private List<List<String>> dfs(String word) {
if(word != null && word.equals("")){
return new ArrayList<List<String>>();
}
List<List<String>> ans = Lists.newArrayList();
Stack<InStack> stack = new Stack<InStack>();
int pos = 0;
stack.clear();
Node root = ROOT;
while( pos < word.length() && root.getChildrens().containsKey(word.charAt(pos))) {
root = root.getChildrens().get(word.charAt(pos));
if (root.isEnd()) {
stack.push(new InStack(word.substring(0, pos + 1), word.substring(pos + 1)));
}
if (root.getCnt() == 1) {
break;
}
++ pos ;
}
while(!stack.empty()){
InStack now = stack.pop();
//末尾
if(now.left.equals("")){
List<String> inList = Lists.newArrayList();
inList.add(now.key);
ans.add(inList);
if(ans.size() == numberLimit){
return ans;
}
continue;
}
//非末尾
List<List<String>> leftStringList = dfs(now.left);
for(List<String> item : leftStringList){
List<String> inList = Lists.newArrayList();
inList.add(now.key);
inList.addAll(item);
ans.add(inList);
if(ans.size() == numberLimit){
return ans;
}
}
}
return ans;
} public List<List<String>> pySplit(String word,Integer number){
numberLimit = number==null? 20:number;
if(word.length() >= 60){
return new ArrayList< List<String> >();
}
else
return dfs(word);
}
public static void main(String[] args){
String it = "xiangangtiananmen";
PyTreeIT pyTree = new PyTreeIT();
List<List<String>>ans = pyTree.pySplit(it,20);
if(ans == null){
System.out.println("TOT");
}
else {
for (List<String> item : ans) {
for (String key : item) {
System.out.print(key);
System.out.print(" ");
}
System.out.println("");
}
}
System.out.println("over");
}
}
trie + 长度优先匹配,生成串的更多相关文章
- 【EF 3】浅谈ADO数据模型生成串(一):csdl,ssdl,msl分析
导读:这段经历,真的是难以忘怀.恨得我牙痒痒,就一个字符串拼接,前前后后尽然报了不下30个错.有的错很快就能调出来,有的错调一天.两天,是真的可以的.最终总结了一下,这些错很大一部分原因是对于EF生成 ...
- 【EF 2】浅谈ADO数据模型生成串(二):数据库连接串分析
导读:上篇博客中介绍了ADO生成串的前一部分,本篇博客结合报错,接着介绍剩下的部分. 一.代码展示 <span style="font-family:KaiTi_GB2312;font ...
- hiho#1449 重复旋律6 求长度为k的串最大次数 后缀自动机
题目传送门 题目大意:求长度为k的串的最大次数,把k从1到length的所有答案全部输出. 思路: 这道题放在$SAM$里就是求长度$k$对应的所有$right$集中最大的大小. 我们以$aabab$ ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- 用正则匹配一串字符串中的ip地址
IP地址有4段组成,每一段数字的范围为0-255,在一段文本中提取ip地址可以这样 $src = 'src = alsdlk ks sdf2.3.3.4 234.193.1.120.1232 d.23 ...
- HDU 1711 Number Sequence(KMP匹配数字串)
这个就是kmp的数组形式,不用来处理字符串还真有点不习惯呢... #include<iostream> using namespace std; ,MAXM = ; int T[MAXN] ...
- php 分词 —— PHPAnalysis无组件分词系统
分词,顾名思义就是把词语分开,从哪里分开?当然是一大堆词语里了,一大堆词语是什么?是废话或者名言.这在数据库搜索时非常有用. 官方网站 http://www.phpbone.com/phpanalys ...
- 一个很好的php分词类库
PHPAnalysis源程序下载与演示: PHP分词系统 V2.0 版下载 | PHP分词系统演示 | PHPAnalysis类API文档 原文连接地址:http://www.phpbone.co ...
- php 分词
发现了一个很好的分词类库phpanalysis2.0. 原文连接地址:http://www.phpbone.com/phpanalysis/ 分 词系统简介:PHPAnalysis分词 ...
随机推荐
- H264 Decoder
http://www.cnblogs.com/mcodec/category/213433.html
- iOS8开发~Swift(一)入门
一.概论及Swift介绍 iOS7刚公布多时候,苹果引入了JavaScriptCore.framework用来处理JavaScript,看到了能够接触其它编程语言的契机,使iOS程序猿不用吊死在OC这 ...
- uva-211-The Domino Effect
http://uva.onlinejudge.org/external/2/211.html http://uva.onlinejudge.org/external/2/211.pdf 题意:每一种骨 ...
- Java-WebSocket 项目的研究(三) WebSocketClient 类 具体解释
通过之前两篇文章 Java-WebSocket 项目的研究(一) Java-WebSocket类图描写叙述 Java-WebSocket 项目的研究(二) 小试身手:client连接server并发送 ...
- Android大图片导致内存问题小结
在网上看了部分Android中OOM的问题,现在根据理解,做一下笔记. Android OOM 产生的几种原因 1. 程序中使用了太多自己创建的Bitmap. 这种情况通常是最好解决的. 因为你明白你 ...
- 不知道的JavaScript
你不知道的JavaScript上卷笔记 前言 You don't know JavaScript是github上一个系列文章 初看到这一标题的时候,感觉怎么老外也搞标题党,用这种冲突性比较强的题目吸 ...
- [品质生活] 舒适 Schick HYDRO 5剃须刀
[品质生活] 舒适 Schick HYDRO 5剃须刀 [品质生活] 舒适 Schick HYDRO 5剃须刀
- uva 1510 - Neon Sign(计数)
题目链接:uva 1510 - Neon Sign 题目大意:给定n个点,随意三点不共线,而且两两点之间有一条线,给定线的颜色.问说有多少个三角形三边同色. 解题思路:对于每一个点.记录该点黑色边的数 ...
- GridView的RowDataBound事件中获取当前行内容的几种方法
1. Cells[x].Txt. 从列单元格的文本值获取.这种方法简单高率,最为常用,但是功能单纯.此法存在几个缺点: (1)无法获取到设置了隐藏属性的数据列的值,所取到的值为“”(空). ...
- xml(3)--dom4j实现crud操作
1.XML解析技术概述 (1)XML解析方式分为两种:dom和sax dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种标准方 ...