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分词 ...
随机推荐
- Bdsyn百度手机助手是何物,它是怎样神不知鬼不觉地安装到你的电脑里的?
[电脑软件管理中Bdsyn手机助手的问题]Bdsyn手机助手 is developed by Baidu, Inc. and is used by 10 users of Software Infor ...
- 贪心算法-找零钱(C#实现)
找零钱这个问题很清楚,无非就是始终拿可以取的最大面值来找,最后就使得张数最小了,这个实现是在假设各种面值足够多的情况下. 首先拖出一个界面来,最下面是一个listbox控件 对应的代码:问题比较简单, ...
- junit测试时,出现java.lang.IllegalStateException: Failed to load ApplicationContext
课程设计要求进行junit测试,我是在已经做好的ssh项目上做的测试,测试类代码如下 package com.zhang.web.services; import static org.junit.A ...
- Java Swing界面编程(28)---复选框:JCheckBox
程序能够通过JRadioButton实现单选button的功能,那么要实现复选框的功能,则必须使用JCheckBox完毕. package com.beyole.util; import java.a ...
- [转]解决get方法传递URL参数中文乱码问题
来自:http://www.javaeye.com/topic/483158 应用一:解决tomcat下中文乱码问题(先来个简单的) 在tomcat下,我们通常这样来解决中文乱码问题: 过滤器代码: ...
- POJ 4003 Bob’s Race && HDU4123 Bob’s Race (dfs+rmq)
Bob’s Race Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 378 Accepted: 119 Descript ...
- POJ1611 The Suspects (并查集)
本文出自:http://blog.csdn.net/svitter 题意:0号学生染病,有n个学生,m个小组.和0号学生同组的学生染病,病能够传染. 输入格式:n,m 数量 学生编号1,2,3,4 ...
- [Java聊天室server]实战之五 读写循环(服务端)
前言 学习不论什么一个稍有难度的技术,要对其有充分理性的分析,之后果断做出决定---->也就是人们常说的"多谋善断":本系列尽管涉及的是socket相关的知识,但学习之前,更 ...
- poj3414(bfs)
题目链接:http://poj.org/problem?id=3414 题意:给你两个容器 A B 问是否能够经过有限的步骤倒水,得到容量为 C 的水,输出最小的步数,同时输出每一步的操作.如果不能 ...
- hdu 1849 (尼姆博弈)
http://acm.hdu.edu.cn/showproblem.php? pid=1849 简单的尼姆博弈: 代码例如以下: #include <iostream> #include ...