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分词 ...
随机推荐
- KMP算法及KMP算法的应用(POJ2406)
///KMP算法#include<bits/stdc++.h> using namespace std; ]; void makeNext(const char P[],int next[ ...
- Java线程面试题 Top 50(转)
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- Flex与Java交互(Flex调用java类展示数据)解析xml展示数据
Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869 Flex与java通信 ...
- java中clone的深入理解
Java中Clone的概念大家应该都很熟悉了,它可以让我们很方便的“制造”出一个对象的副本来,下面来具体看看java中的Clone机制是如何工作的? 1. Clone和Copy 假 ...
- JavaScript实战
JavaScript之单例实战 一.概述 所谓单例模式,顾名思义即一个类只有一个实例. 所以,当我们创建一个实例时,就必须判断其是否已经存在了这个实例,如果已经存在了这个实例,那么就返回这个已经存在的 ...
- Android 动态生成布局 (多层嵌套)
Android 除了能够载入xml文件,显示布局外,也能够代码生成布局,并通过setContentView(View view)方法显示布局.单独的一层布局,如一个主布局加一个控件(如Button\i ...
- Juniper srx防火墙NAT配置
一.基础操作说明: 1. 设备恢复出厂化 root# load factory-default root# set system root-authentication plain-text-pas ...
- VSTO学习笔记(七)基于WPF的Excel分析、转换小程序
原文:VSTO学习笔记(七)基于WPF的Excel分析.转换小程序 近期因为工作的需要,要批量处理Excel文件,于是写了一个小程序,来提升工作效率. 小程序的功能是对Excel进行一些分析.验证,然 ...
- Deep Learning(深度学习) 学习笔记(四)
神经概率语言模型,内容分为三块:问题,模型与准则,实验结果.[此节内容未完待续...] 1,语言模型问题 语言模型问题就是给定一个语言词典包括v个单词,对一个字串做出二元推断,推断其是否符合该语言表达 ...
- Java_并发线程_CompletionService
1.CompletionService源代码分析 CompletionService内部实现还是维护了一个可堵塞的队列,通过代理设计模式.从而操作队列. /** * Creates an Execut ...